Ansible is a free & open source Configuration and automation tool for UNIX like operating system. It is written in python and similar to Chef or Puppet but there is one difference and advantage of Ansible is that we don’t need to install any agent on the nodes. It uses SSH for making communication to its nodes.
In this article we will demonstrate how to install and configure Ansible in CentOS 7 and will try to manage its two nodes.
Ansible Server – ansible.linuxtechi.com ( 192.168.1.15 )
Nodes – 192.168.1.9 , 192.168.1.10
Step :1 Set EPEL repository
Ansible package is not available in the default yum repositories, so we will enable epel repository for CentOS 7 using below commands
[root@ansible ~]# yum install epel-release -y
Step:2 Install Anisble using yum command
[root@ansible ~]# yum install ansible
Once the installation is completed, check the ansible version :
[root@ansible ~]# ansible --version
Step:3 Setup keys based SSH authentication with Nodes.
Generate keys on the Ansible server and copy public key to the nodes.
root@ansible ~]# ssh-keygen
Use ssh-copy-id command to copy public key of Ansible server to its nodes.
Step:4 Define the nodes or inventory of servers for Ansible.
File ‘/etc/ansible/hosts‘ maintains the inventory of servers for Ansible.
[root@ansible ~]# vi /etc/ansible/hosts [test-servers] 192.168.1.9 192.168.1.10
Save and exit the file.
Sample output of hosts file.
Step:5 Now try to run the Commands from Ansible Server.
Check the connectivity of ‘test-servers’ or ansible nodes using ping
[root@ansible ~]# ansible -m ping 'test-servers'
Executing Shell commands :
Example :1 Check the uptime of Ansible nodes
[root@ansible ~]# ansible -m command -a "uptime" 'test-servers'
Example:2 Check Kernel Version of nodes
[root@ansible ~]# ansible -m command -a "uname -r" 'test-servers'
Example:3 Adding a user to the nodes
[root@ansible ~]# ansible -m command -a "useradd mark" 'test-servers' [root@ansible ~]# ansible -m command -a "grep mark /etc/passwd" 'test-servers'
Example:4 Redirecting the output of command to a file
[root@ansible ~]# ansible -m command -a "df -Th" 'test-servers' > /tmp/command-output.txt
Read Also: How to Download and Use Ansible Galaxy Roles in Ansible Playbook
what if we get permission denied during ssh-copy-id, how to trouble shoot that?
getting below error
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
what if we get permission denied during ssh-copy-id, how to trouble shoot that?
getting below error
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
Hello ,
I solved the problem.Solution of the problem, we need to specify the user to the specified IP in test-server.
[test-server]
root@ip-address
Just a note: ‘http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm’ no longer exists. Using ‘http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm’ seems to work.
Thank You Will , I have updated the same in the article.
Helped a lot!! Thanks.
Adding a user to the nodes
ansible -m command -a “useradd avitest” ‘SharedHosting’
ansible -m command -a “grep avitest /etc/passwd” ‘SharedHosting’
What is the password of avitest and how to add user in /var/cpanel/resellers with reseller privillage.
Hi!
Thanks for the informative article. Ansible looks like an awesome tool for SysAdmins.
New epel repo: ‘http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm’
Thanks a lot for the article. was really helpful