How to deploy using Ansible?

Now that we have installed Ansible successfully, it is time now to deploy it to Remote Hosts.

To perform any deployment/management from the localhost to remote host, first we need to create and copy the ssh keys to the remote host.

Create SSH Key

[[email protected] ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/
The key fingerprint is:
48:dd:4b:c1:b5:d5:af:db:a7:a9:c9:24:fc:29:76:68 [email protected]
The key’s randomart image is:
+–[ RSA 2048]—-+
| …. .. |
| . … o .|
| . . o . .|
| . . . . .|
| . S . . |
| . . |
| o.. o |
| E=.o..o|
| o o*.o..|

Now copy the SSH key to the remote hosts

[[email protected] ~]# ssh-copy-id [email protected]
The authenticity of host ‘ (’ can’t be established.
ECDSA key fingerprint is b9:a0:d4:cb:36:f6:8f:bf:95:0e:90:a5:91:01:eb:34.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed — if you are prompted now it is to install the new keys
[email protected]’s password:

Number of key(s) added: 1

Now try logging into the machine and check to make sure that only the key(s) you wanted were added.

[[email protected] ~]# ssh [email protected]
Last login: Mon Aug 24 09:11:42 2015 from
[[email protected] ~]# exit
Connection to closed.

Do this for all the remote hosts.

The next step is to add the remote host to the config file. The config file of Ansile is /etc/ansible/hosts.

[[email protected] ~]# cat /etc/ansible/hosts
## This is the default ansible ‘hosts’ file.
## It should live in /etc/ansible/hosts
## – Comments begin with the ‘#’ character
## – Blank lines are ignored
## – Groups of hosts are delimited by [header] elements
## – You can enter hostnames or ip addresses
## – A hostname/ip can be a member of multiple groups

[remote-host] #Remote Host 1

Now, let us verify the connectivity between the localhost and remote host.

[[email protected] ~]# ansible -m ping remote-host | success >> {
“changed”: false,
“ping”: “pong”

[[email protected] ~]# ansible -m ping | success >> {
“changed”: false,
“ping”: “pong”

Ansible has multiple modules to work with. In the above example I have used the ping module.

You can use the command module to execute commands on remote host.

[[email protected] ~]# ansible -m command -a ‘df -h’ remote-host | success | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos_centos7-root 10.0G 1.1G 8.9G 11% /
devtmpfs 912M 0 912M 0% /dev
tmpfs 921M 0 921M 0% /dev/shm
tmpfs 921M 8.4M 912M 1% /run
tmpfs 921M 0 921M 0% /sys/fs/cgroup
/dev/sda1 497M 119M 379M 24% /boot

[[email protected] ~]# ansible -m command -a ‘uptime’ remote-host | success | rc=0 >>
02:34:37 up 1 day, 17:24, 3 users, load average: 0.00, 0.01, 0.05

[[email protected] ~]# ansible -m command -a ‘hostname’ remote-host | success | rc=0 >>

[[email protected] ~]# ansible -m command -a ‘cat /etc/redhat-release’ remote-host | success | rc=0 >>
CentOS Linux release 7.1.1503 (Core)

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.
%d bloggers like this: