Categories: Automation

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

[root@localhost ~]# 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/id_rsa.pub.
The key fingerprint is:
48:dd:4b:c1:b5:d5:af:db:a7:a9:c9:24:fc:29:76:68 root@localhost
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

[root@localhost ~]# ssh-copy-id root@192.168.10.2
The authenticity of host ‘192.168.10.2 (192.168.10.2)’ 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
root@192.168.10.2’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.

[root@localhost ~]# ssh root@192.168.10.2
Last login: Mon Aug 24 09:11:42 2015 from 192.168.56.1
[root@remotehost ~]# exit
logout
Connection to 192.168.10.2 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.

[root@localhost ~]# 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]
192.168.10.2 #Remote Host 1

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

[root@localhost ~]# ansible -m ping remote-host
192.168.10.2 | success >> {
“changed”: false,
“ping”: “pong”
}

[root@localhost ~]# ansible -m ping 192.168.10.2
192.168.10.2 | 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.

[root@localhost ~]# ansible -m command -a ‘df -h’ remote-host
192.168.10.2 | 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

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

[root@localhost ~]# ansible -m command -a ‘hostname’ remote-host
192.168.10.2 | success | rc=0 >>
remote-host

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

Nitesh Shah

Share
Published by
Nitesh Shah

Recent Posts

How to setup first Azure Virtual Machine?

Setting up your first Azure Virtual Machine can be done by following these steps: Create…

1 year ago

How to setup Amazon Cloudfront and S3 to serve static resources

Amazon CloudFront is a content delivery network (CDN) that helps you serve static content such…

1 year ago

Step-By-Step Guide To Setting Up An AWS Application Load Balancer

Step-By-Step Guide To Setting Up An AWS Application Load Balancer Are you looking for a…

1 year ago

How to restore MySQL database from .frm and .ibd files?

MySQL databases often get corrupted due to issues like hardware failure, file system failure etc.…

4 years ago

SQL Server Replication

SQL Server Replication is the process of copying databases from one node to another to…

5 years ago

101 System Admin Tools to make life easy

Here are 101 System Admin tools which make System Admins' life easy.

7 years ago

This website uses cookies.