Introduction To Ansible Tutorial
Hi today we will look to a system management automation tool named Ansible. There is a lot of tool in this category but Ansible is different with being agent-less. Yes you read it right it is client-less and developed in python language. Ansible architecture is very simple which made by a manager which must be Linux and the systems managed which maybe Linux, BSD or Windows. There are some differences in windows but it is not a problem after setup Ansible. All systems managed in the same way. In Linux and BSD connection made over ssh but in windows RMI protocol.
Install Ansible
Firstly we use fedora for the manager and we should install ansible only on this system. Below command install ansible on the manager
Ubuntu, Debian, Mint, Kali
1 |
$ sudo apt install ansible |

Ubuntu, Debian, Mint, Kali
Fedora, CentOS, RHEL
1 |
$ sudo yum install ansible -y |
Password-less Connection
And then create ssh-id if it doesn’t exist. We can skip key generation questions to enter without giving any value.
1 |
$ ssh-keygen |

Password-less Connection
Than we will install the keys to the remotely managed system with ssh-id-copy command. Now we are ready to use ansible in a simple way.
1 |
$ ssh-copy-id ismail@192.168.122.165 |
Inventory and Hosts
Ansible uses its host’s file which is named as Inventory to get information about remote systems. We can group hosts in this inventory file. Add this host (192.168.122.165) to the Add host inventory . Here myex
is group name which covers hosts listed below. For now I just add one host
1 2 3 4 |
$ sudo vim /etc/ansible/hosts [myex] 192.168.122.165 |

Inventory and Hosts
Ping Hosts and Check Connectivity
Now we can use ansible. We use the ping module with -m
option ans by specifying the group name. We can use all for all inventory hosts.
1 |
$ ansible myex -m ping |

Ping Hosts and Check Connectivity
Run Ad Hoc Command
We can specify pure commands to run on a remote system with -a
option. Ad Hoc command provides us the ability to run a command on the remote systems. In this example, we will print the passwd
file with cat
command.
1 |
$ ansible myex -a "cat /etc/passwd" |

Run Ad Hoc Command
Debug Ansible and Verbose Output
If we need to debug Ansible work we can use -vvv
option where the count of v decides the debug level. As you can see below connection username and the remote module is specified and than ssh connection is made with specified parameters if you have a problem with ssh connection you can find detailed info about the problem here. There are Ansible verbs EXEC, PUT, etc.
1 |
$ ansible myex -a "cat /etc/passwd" -vvv |

Debug Ansible and Verbose Output
Run Sudo On Remote System
We can use sudo privileges on the remote system with --sudo
and specify sudo user with –sudo-user .
1 |
$ ansible myex -a "/bin/cat /etc/passwd" --sudo |
Ansible Configuration
Because ansible use ssh we can disable ssh host key checking by uncommenting below the line.
1 2 3 4 5 6 7 8 9 |
$ sudo vim /etc/ansible/ansible.cfg # additional paths to search for roles in, colon separated #roles_path = /etc/ansible/roles # uncomment this to disable SSH key host checking host_key_checking = False # change this for alternative sudo implementations |
Sometimes there are a lot of systems to manage and run commands. We can make ansible work parallel with -f
option
1 |
$ ansible all -a "ls ~ismail" --sudo -f 10 |