How to set up Control and Managed nodes in Ansible

In the first part of setting up Ansible with PIP we have installed Ansible on Ubuntu 20.04 LTS that is our Control Node, if you don't have Ansible installed on Control node than refer How to install Ansible with PIP in Ubuntu.

Prerequisite for following this tutorial
  1. Control Node : VM having Ubuntu 18.04 LTS or above with Ansible installed.
  2. Managed Node : VM having Ubuntu 18.04 LTS or above with Python installed

This tutorial will walk through the steps for setting up Control Node, Managed Node and testing connection between Control Node and Managed Node.

Create ansible user on Managed Node

Ansible runs command as ansible user on managed node, so we need ansible user created on all managed nodes.

Run below command for creating ansible user, note down the password and IP as these details would be required in setting up control node.


ubuntu@managed-node-1:~$ sudo adduser ansible

Setting up Control Node for running ad-hoc commands

Till now we have Ansible running on Control Node and ansible user created on Managed Node. We need to perform below steps to complete the set up on Control Node.

  • Add Entry of Managed Node in /etc/hosts of Control node
  • 
    ansible@control-node:~$ sudo vi /etc/hosts
    

    Add below entry in /etc/hosts, replace [IP] with IP of your Managed Node and save the configuration.

    
    [IP] managed-node-1
    

  • Create Ansible project directory on Control Node
  • 
    ansible@control-node:~$ cd /home/ansible;
    ansible@control-node:~$ mkdir test-ansible;
    ansible@control-node:~$ cd test-ansible;
    

  • As we have done installation through PIP, so there wont be any default ansible.cfg generated, we will create it manually inside Ansible project directory that is test-ansible
  • 
    ansible@control-node:~$ vi /home/ansible/test-ansible/ansible.cfg
    

    Write below lines in ansible.cfg

    
    [defaults]
    inventory=./inventory
    

  • Create inventory with name hosts and add entry for managed-node-1
  • 
    ansible@control-node:~$ vi /home/ansible/test-ansible/hosts
    

    Write below line in hosts

    
    managed-node-1
    

  • Generate SSH keys on Control node with default values
  • 
    ansible@control-node:~$ ssh-keygen
    

  • Copy Generated SSH keys to managed-node-1, this will prompt for password provided for ansible user on Managed Node
  • 
    ansible@control-node:~$ ssh-copy-id -i ~/.ssh/id_rsa ansible@managed-node-1
    

    With these steps set up for both Control and Managed node is complete, lets verify connection with ping .

    List all hosts in inventory

    
    ansible@control-node:~$ ansible all -i hosts  --list-hosts
    

    Output

    
      hosts (1):
        managed-node-1
    
    

    Execute ping command for all Managed Nodes, in this case we have only one node managed-node-1

    
    ansible@control-node:~$ ansible  -i hosts -m ping all
    

    You should see output for managed nodes similar to the below one

    
    managed-node-1 | SUCCESS => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "changed": false,
        "ping": "pong"
    }
    

    If you are getting any error while following this tutorial, please post errors in comments, we will try to resolve those as soon as possible.

    Ansible Installation < Prev Next >Managing Apache with Ansible

    Category: Linux