You are on page 1of 21

28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

nixCraft
Linux / Unix tutorials for new and seasoned sysadmin || developers

↔ Home Linux Shell Scripting Tutorial RSS Donate Search

How to install Ansible on Fedora 32 for IT


and server automation
Author: Vivek Gite
Last updated: September 29, 2020
0 comments

H
ow do I install Ansible on Fedora 32 workstation? How can I set up and
test Ansible playbooks using my Fedora Linux desktop?

Introduction – Ansible is a free and open source configuration management


tool. It is similar to Chef or Puppet. It works over SSH based session and does
not need any software or client/agent on remote servers. One can use Ansible to
manage Linux, Unix, macOS, and *BSD family of operating systems. This page shows
how to install ansible and set up your first Ansible playbook on Fedora Linux
29/30/31/32.

https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 1/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

Procedure to install Ansible on Fedora 32


. Update your Fedora 32 system, run: sudo dnf update

. Install Ansible on Fedora 32, run: sudo dnf install ansible

. Upgrade Ansible in Fedora 32, run: sudo dnf upgrade ansible

. Set up ssh key-based authentication

. Test Ansible

Step 1. Fedora Linux install Ansible


Type the following dnf command to update Fedora box:

$ sudo dnf update


$ dnf search ansible

Find out information about the Ansible package, run:

$ dnf info ansible


https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 2/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

Sample outputs:

Last metadata expiration check: 0:00:41 ago on Tue Sep 29 09:29:00 2020.
Available Packages
Name : ansible
Version : 2.9.13
Release : 1.fc32
Architecture : noarch
Size : 15 M
Source : ansible-2.9.13-1.fc32.src.rpm
Repository : updates
Summary : SSH-based configuration management, deployment, and task
: execution system
URL : http://ansible.com
License : GPLv3+
Description : Ansible is a radically simple model-driven configuration
: management, multi-node deployment, and remote task execution
: system. Ansible works over SSH and does not require any
: software or daemons to be installed on remote nodes. Extension
: modules can be written in any language and are transferred to
: managed machines automatically.

Installing Ansbile on Fedora Linux


Finally, type the following dnf command:

$ sudo dnf install ansible

https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 3/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 4/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

Find the Ansible version


We can verify the Ansible version by running the following command:

$ ansible --version

Sample outputs:

ansible 2.9.13
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugin
ansible python module location = /usr/lib/python3.8/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.5 (default, Aug 12 2020, 00:00:00) [GCC 10.2.1 20200723 (Red Hat 10.2.1-

Step 2. Set up ssh keys on a Linux or Unix


First, create the key pair using the ssh-keygen command on your Fedora Linux
desktop/workstation:

$ ssh-keygen -t ed25519 -C "Desktop ssh key"

Next, copy and install the public key in remote Linux/Unix/BSD servers using the ssh-
copy-id command:

$ ssh-copy-id -i $HOME/.ssh/id_ed25519.pub user@ubuntu-server-ec2


$ ssh-copy-id -i $HOME/.ssh/id_ed25519.pub ec2-user@freebsd-server-
lightsail
$ ssh-copy-id -i $HOME/.ssh/id_ed25519.pub vivek@centos-server-
linode

Test password less log in using the ssh command:

https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 5/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

$ ssh vivek@centos-server-linode
$ ssh ec2-user@freebsd-server-lightsail

Faca pa e
Assine o clube de
Editora Intrínseca

Step 3. Test the Ansible

Our sample Ansible setup

First create an inventory file as follows on a control machine:

$ vi inventory

https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 6/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

Add hostnames/IP address of all remote Linux/*BSD servers:

## my vms/server hosted locally ##


[lanhosts]
192.168.2.203
192.168.2.207

## my vms/servers hosted by AWS (EC2/Lightsail) ##


[awshosts]
vm1.cyberciti.biz

## my Linode VMs ##
[linodehosts]
vm2.cyberciti.biz

Next run the uptime command command and lsb_release command on two hosts
located in my LAN i.e. lanhosts group as user vivek:

$ ansible -u vivek -i inventory -m raw -a 'uptime' lanhosts


$ ansible -u vivek -i inventory -m raw -a 'lsb_release -a' lanhosts

https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 7/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

Ultra Fino Relógio Criativo Quartzo... Relógio Unisex Bi


R$137.43 R$150.15 R$98

https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 8/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

Step 4. Writing your first Ansible playbook to manage


Linux/Unix servers
First, update your inventory file to indicate user name and method to become sudo on
the remote server. Here is my updated hosts file displayed with the cat command:

cat inventory

Sample config file:

[all:vars]
ansible_user='vivek' # Username for ssh connection
ansible_become='yes' # Run commands as root user?
ansible_become_pass='PasswordForVivekUser' # Password for sudo user i.e. ansible_user password
ansible_become_method='sudo' # How do I become root user? Use sudo.
 
## my vms/server hosted locally ##
[lanhosts]
192.168.2.203 ansible_python_interpreter='/usr/bin/python2'
192.168.2.207 ansible_python_interpreter='/usr/bin/python3'
 
## my vms/servers hosted by AWS (EC2/Lightsail) ##
[awshosts]
vm1.cyberciti.biz
 
## my Linode VMs ##
[linodehosts]
vm2.cyberciti.biz

A playbook is nothing but scripts/commands that executed on the remote box. Create a
playbook named test.yml as follows using a text editor such as vim command/nano
command:

vim test.yml

Append the following code:

https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 9/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

Relógio Cosmograph Ultra Fino - Preto Relógio Ultra-Fino


R$117.70 R$199 R$103.99 R$159

---
- hosts: lanhosts
 
tasks:
- name: Get hostname for testing purpose
command: /bin/hostname
changed_when: False
register: hostname
 
- debug: var={{ item }}
with_items:
- hostname.stdout

Playbooks in Ansible use Yaml. Next, run it as follows from Fedora Linux
workstation/control machine:

$ ansible-playbook -i inventory test.yml

https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 10/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

A note about password stored in an insecure format


Take a close look at the following config directory in inventory file:

ansible_become_pass='PasswordForVivekUser'

It is a bad idea to store password and other sensitive information in clear text format. Let
us fix this:

$ vim inventory

Find:

ansible_become_pass='PasswordForVivekUser'

https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 11/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

Replace:

ansible_become_pass='{{ my_user_password }}'

Save and close the file. Next create a new encrypted data file named passwords.yml, run
the following command:

$ ansible-vault create passwords.yml

Set the password for vault. After providing a password, the tool will start whatever
editor you have defined with $EDITOR. Append the following:

my_user_password: your_password_for_ansible_user

Save and close the file. Run it as follows:

$ ansible-playbook -i inventory --ask-vault-pass --extra-vars


'@passwords.yml' test.yml

Click to enlarge
https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 12/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

For more information read: How to set and use sudo password for Ansible Vault.

Adding user using the Ansible playbook


Say you need to add a new user named wwwjobs all hosts in lanhosts group. Create a
new playbook named add-user.yml:

---
- hosts: lanhosts
tasks:
- name: Add a new user to my Linux VMs with password disabled but allow ssh log in
user:
name: wwwjobs
comment: "Account to run jobs for our web server"
shell: /bin/bash
groups: sudo
append: yes
password: *
- name: Upload ssh key for user wwwjobs for log in purpose
authorized_key:
user: vivek
state: present
manage_dir: yes
key: "{{ lookup('file', '/home/vivek/.ssh/id_ed25519.pub') }}"

Run it as follows:

$ ansible-playbook -i inventory --ask-vault-pass --extra-vars


'@passwords.yml' add-user.yml

How to add and remove packages


In this example, we are going to add and remove packages using the apt command for
all hosts located in linodehosts group. Create a file named ubuntu-software.yml:

---
- hosts: linodehosts

https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 13/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

tasks:
- name: Add a list of software on Linode VMs ...
apt:
name: "{{ packages }}"
state: present
vars:
packages:
- vim
- unzip
- htop
- atop
- iftop
- nmon
- sysstat
- iotop
- nicstat
- vnstat
- name: Delete a list of software from Linode VMs ...
apt:
name: "{{ packages }}"
state: absent
vars:
packages:
- nano

Again run it as follows:

$ ansible-playbook -i inventory --ask-vault-pass --extra-vars


'@passwords.yml' ubuntu-software.yml

Conclusion
And there you have it, Ansible set up and tested to manage Linux or Unix boxes. Ansible
works very fast for repeated tasks such as adding users in bulk, installing software,
configuring *BSD/Linux/Unix boxes. YAML takes a little time to master but easy to learn.
See Ansible documentation for more info:

Ansible documents

Linux user module document


https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 14/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

Debian/Ubuntu apt module document

How to use Ansible vault to keep sensitive data such as passwords or keys in
encrypted files

🐧 Please support my work on Patreon or with a donation.


🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
RSS feed or Weekly email newsletter

Share on Twitter • Facebook • 0 comments... add one ↓

Related Tutorials
How to install Ansible on Ubuntu 18.04 for IT automation

How to Install and Configure latest version of…

How to upgrade Fedora 29 to Fedora 30

Upgrade Fedora 31 to Fedora 32 using the CLI

How to prepare FreeBSD server to be managed by Ansible tool

Ansible reboot Linux machine or server with playbooks

https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 15/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

How to install and set up LXD on Fedora Linux server

Category List of Unix and Linux commands

File Management cat

Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04


Firewall
• Ubuntu 20.04

Network Utilities dig • host • ip • nmap

OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04

Package Manager apk • apt

Processes bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx •
Management time

Searching grep • whereis • which

groups • id • lastcomm • last • lid/libuser-lid • logname • members • users •


User Information
whoami • who • w

WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

0 comments… add one ↓

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment

https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 16/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

Name *

Email *

Website

Post Comment

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ webmaster@cyberciti.biz

Next FAQ: How to install VirtualBox 6 on Fedora Linux 29

Previous FAQ: How to install Shutter screenshot tool on a Fedora Linux

https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 17/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

🔎 Search this site

https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 18/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

Livros em Oferta
Mercado Livre

FEATURED ARTICLES

1 30 Cool Open Source Software I Discovered in 2013

2 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X

3 Top 32 Nmap Command Examples For Linux Sys/Network Admins

https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 19/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

4 25 PHP Security Best Practices For Linux Sys Admins

5 30 Linux System Monitoring Tools Every SysAdmin Should Know

6 40 Linux Server Hardening Security Tips

7 Linux: 25 Iptables Netfilter Firewall Examples For New SysAdmins

8 Top 20 OpenSSH Server Best Security Practices

9 Top 25 Nginx Web Server Best Security Practices

10 My 10 UNIX Command Line Mistakes

Sign up for my newsletter

https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 20/21
28/01/2021 How to install Ansible on Fedora 32 for IT and server automation - nixCraft

Ultra Fino Relógio


Criativo Quartzo...

Ad Mercado Livre

Linux Set or Change


User Password

cyberciti.biz

DigitalOcean®
Developer Cloud -
Simple, Powerful
Cloud Hosting
Ad try.digitalocean.com

©2002-2021 nixCraft • Privacy • ToS • Contact/Email • Search • Corporate patron Linode

Relógio Cosmograph Ultra Fino - Relógio Ultra-Fino com apenas Relógio de Qu


Preto 6.5mm... Linha...
R$117.70 R$199 R$103.99 R$159 R$187 R$349.9

https://www.cyberciti.biz/faq/how-to-install-ansible-on-fedora-for-it-and-server-automation/ 21/21

You might also like