Getting ready N’ Ansible ping…

<< Previous : https://knowledgehunter.code.blog/2020/10/10/setup-ansible-in-local/


In the previous aritcle we setup our VM and installed Ansible.

First ssh to the VM to see the user, password and ip is ok. You must be able to log with password.

eg : ssh ubuntu@192.168.1.20

Note : you can get the IP by running the ifconfig on the VMโ€™s terminal.


Get readyโ€ฆ

Letโ€™s try to run some basic Ansible commands.

First of all, to run Ansible, you need to have

  1. One Ansible Control Node : machine weโ€™ll use to connect to and control the Ansible hosts over SSH
  2. One or more Ansible Hosts : any machine that your Ansible control node is configured to automate

In this case, our local PC is the Ansible Control Node and VM is the Ansible Host.

There are some prerequisites fulfilled before start playing with Ansibleโ€ฆ

  1. Ansible Control Node must have a non-root user with sudo privileges.
  2. Ansible Control Node must have SSH keypair associated with this user.

Note : In this case, you must have been already setup the SSH Keys on the local PC. Otherwise refer : https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys-on-ubuntu-20-04,

Or you can generate SSH Keys using IDEโ€™s also like eclipse

[Windows > Preferences > General > Network Connection > SSH2 > Key Management> Generate RSA Key]

3. The Ansible control nodeโ€™s SSH public key must have been added to Ansible Hostโ€™s authorized_keysย for a system user.

You can easily do this by using ssh-copy-id in ssh-copy-id username@remote_host format

eg : $ssh-copy-id ubuntu@192.168.1.20

Note :

Sometimes you may get below like error

sign_and_send_pubkey: signing failed for RSA โ€œ/home/user/.ssh/id_rsaโ€ from agent: agent refused operation

In this case, you must add the ssh keys to Agent using $ ssh-add.

Here. if you get below like warning, you must change the access of the .ssh/id_rsa by running chmod 600 ~/.ssh/id_rsa since the private key has been exposed. Otherwise again ssh-copy-id will fail.

Warining : Permissions 0664 for โ€˜/home/namal/.ssh/id_rsaโ€™ are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.

Once above done, run ssh-copy-id ubuntu@192.168.1.20 again and you should see a success message.

Then try to run ssh ubuntu@192.168.1.20 again this time you must be able to connect without giving the password. This is password less login

Now environment is ready to do Ansible ping..


Do Ansible ping..

Ansible ping means running a connectivity test using Ansibleโ€™s built-in ping module. The ping module will test:

  • if hosts are accessible;
  • if you have valid SSH credentials;
  • if hosts are able to run Ansible modules using Python.

Hosts fileโ€ฆ

You must define the host detail (IPs) in the file called .

The default location for hosts file is /etc/ansible/hosts. But you can define the hosts file in any location you want.

You must define the hosts IPs in the hosts file as mentioned below

[servers]
server1 ansible_host=203.0.113.111
server2 ansible_host=203.0.113.112
server3 ansible_host=203.0.113.113

These server1,ย server2, andย server3 are custom aliases.

Note :

You can define all:vars subgroup also there as mentioned below.

[all:vars]
ansible_python_interpreter=/usr/bin/python3

This parameter makes sure the remote server uses the /usr/bin/python3 Python 3 executable instead of /usr/bin/python (Python 2.7), which is not present on recent Ubuntu versions.

Once the hosts file is defined you can run below command to list the inventories

ansible-inventory โ€“list -y

Note :

If you run any ansible command having specific hosts file (not the generic on in /etc/ansible/hosts), you must give the path (absolute or relative path) using -i flag as follows

if the host file is in the same location that ansible command is executing : -i .

otherwise : -i

eg :

1. ansible-inventory โ€“list -y -i .

2. ansible-inventory โ€“list -y -i inventory

3. ansible-inventory โ€“list -y -i /home/user/ansibleProjects/inventory

If everything is ok youโ€™ll see success response as mentioned below

all:
  children:
    servers:
      hosts:
        server1:
          ansible_host: 103.0.113.111
        server2:
          ansible_host: 103.0.113.112
        server3:
          ansible_host: 103.0.113.113
    ungrouped: {}

Executeโ€ฆ

Then we can run the ansible ping command

ansible all -m ping -u

eg : ansible all -m ping -u ubuntu

For the success responses youโ€™ll get pong for ping as mentioned below.

Outputserver1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
server2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
server3 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

Note : this all will execute the ansible module in all the hosts. If you want to execute it only on certain hosts you can mention those specifically. eg :ansible server1 -m ping -u ubuntu

reference : https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-ansible-on-ubuntu-20-04


https://knowledgehunter.code.blog/2020/10/11/lets-play-ansible-book/ Next >>

#ansible, #config-management