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 may be Linux, BSD or Windows. There are some differences in windows but it is not a problem after setup Ansible. All systems managed by the same way. In Linux and BSD connection made over ssh but in windows RMI protocol. This is enough lets start typing commands.
Install Ansible
Firstly we use fedora for 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 |

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 with enter without giving any value.
1 |
$ ssh-keygen |

Than we will install the keys to the remote managed system with ssh-id-copy command. Now we are ready to use ansible in simple way.
1 |
$ ssh-copy-id ismail@192.168.122.165 |
Inventory and Hosts
Ansible uses its hosts 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 |

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

Run Ad Hoc Command
We can specify pure commands to run on 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" |

Debug Ansible and Verbose Output
If we need to debug Ansible work we can use -vvv flags where count of v decides the debug level. As you can see below connection username and 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 is Ansible verbs EXEC, PUT etc.
1 |
$ ansible myex -a "cat /etc/passwd" -vvv |

Run Sudo On Remote System
We can use sudo privileges on 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 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 |