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.


[email protected]:~$ 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
  • 
    [email protected]:~$ 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
  • 
    [email protected]:~$ cd /home/ansible;
    [email protected]:~$ mkdir test-ansible;
    [email protected]:~$ 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
  • 
    [email protected]:~$ 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
  • 
    [email protected]:~$ vi /home/ansible/test-ansible/hosts
    

    Write below line in hosts

    
    managed-node-1
    

  • Generate SSH keys on Control node with default values
  • 
    [email protected]:~$ ssh-keygen
    

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

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

    List all hosts in inventory

    
    [email protected]:~$ 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

    
    [email protected]:~$ 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

    Categories: Linux