In the last post, we have seen how to install ansible on our system.
After ansible install before creating YML script and running it, we should setup some files to ease the ansible usage.
1. Create a hosts file in your current working directory or /etc/ansible/hosts file.
In ansible the first pre-requisite is to create a hosts file which contains a list of hosts
$ cat hosts a005-host1.ngelinux.com a168-host1.ngelinux.com
2. The second and most important thing is to create a configuration file ansible.cfg in the same directory to define the username, etc.
$ cat ansible.cfg [defaults] inventory = hosts remote_user=saket host_key_checking = False
3. Setup Password less authentication of the configured hosts for seamless access.
4. Try checking ansible command if its working fine.
We are done with defining the username, remote user for login and host key checking.
Now lets run an ansible command to check if its able to login on these hosts.
$ ansible -m shell -a 'uname -a' all a168-host1.ngelinux.com | SUCCESS | rc=0 >> Linux a168-host1.ngelinux.com 3.10.0-862.14.4.el7.x86_64 #1 SMP Tue Sep 25 17:20:38 PDT 2018 x86_64 x86_64 x86_64 GNU/Linux a005-host1.ngelinux.com | SUCCESS | rc=0 >> Linux a005-host1.ngelinux.com 3.10.0-862.14.4.el7.x86_64 #1 SMP Tue Sep 25 17:20:38 PDT 2018 x86_64 x86_64 x86_64 GNU/Linux
If the command ran fine then you are good to go now.
Now we will see how to create the YML scripts to automate the tasks using ansible.