{"id":6234,"date":"2020-06-24T20:45:32","date_gmt":"2020-06-24T17:45:32","guid":{"rendered":"https:\/\/kifarunix.com\/?p=6234"},"modified":"2024-03-14T21:12:21","modified_gmt":"2024-03-14T18:12:21","slug":"install-and-setup-ansible-on-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/install-and-setup-ansible-on-ubuntu-20-04\/","title":{"rendered":"Install and Setup Ansible on Ubuntu 20.04\/Ubuntu 22.04"},"content":{"rendered":"\n

How to setup Ansible in Ubuntu? In this tutorial, we are going to learn how to install and setup Ansible on Ubuntu 20.04\/Ubuntu 22.04. Ansible<\/a> is a simple agentless IT automation tool. It handles configuration management, application deployment, cloud provisioning, ad-hoc task execution, network automation, and multi-node orchestration over the SSH protocol. Ansible makes complex changes like zero-downtime rolling updates with load balancers easy.<\/p>\n\n\n\n

Installing Ansible on Ubuntu 20.04<\/a>\/Ubuntu 22.04<\/h2>\n\n\n\n

Since Ansible is used to manage remote hosts via SSH protocol, the node on which you install Ansible, becomes the control node<\/code><\/strong> and the node which you will manage using Ansible is called managed hosts<\/code><\/strong>.<\/p>\n\n\n\n

Run System Update<\/h3>\n\n\n\n

Before you can proceed, ensure that your system package cache is up-to-date;<\/p>\n\n\n\n

sudo apt update<\/code><\/pre>\n\n\n\n

Install Ansible on Ubuntu 20.04<\/a>\/Ubuntu 22.04<\/h3>\n\n\n\n

There different methods you can use to install Ansible on Ubuntu 20.04\/Ubuntu 22.04 systems;<\/p>\n\n\n\n

    \n
  1. Install Ansbile using PIP on Ubuntu<\/a><\/li>\n\n\n\n
  2. Install Ansible using APT Package Manager<\/a><\/li>\n<\/ol>\n\n\n\n

    Install Ansbile using PIP on Ubuntu<\/h4>\n\n\n\n

    To install Ansbile using Python PIP command, proceed as follows;<\/p>\n\n\n\n

    Ensure that PIP is available on the system;<\/p>\n\n\n\n

    python3 -m pip -V<\/code><\/pre>\n\n\n\n

    Such an output should be printed if PIP is available;<\/p>\n\n\n\n

    pip 22.3.1 from \/usr\/local\/lib\/python3.8\/dist-packages\/pip (python 3.8)<\/code><\/pre>\n\n\n\n

    If PIP is available, then you can proceed to install Ansible.<\/p>\n\n\n\n

    Otherwise, install PIP before you can proceed;<\/p>\n\n\n\n

    apt install python3-pip<\/code><\/pre>\n\n\n\n

    If PIP is already installed, you might want to upgrade it;<\/p>\n\n\n\n

    python3 -m pip install --upgrade pip<\/code><\/pre>\n\n\n\n

    With PIP being available on the system, you can install Ansbile for the current user only by running the command below;<\/p>\n\n\n\n

    python3 -m pip install --user ansible<\/code><\/pre>\n\n\n\n

    All the Ansible binaries are installed in ‘$HOME\/.local\/bin<\/code><\/strong>‘ which is not on PATH.<\/p>\n\n\n\n

    You can add this path to PATH environment variable temporarily using;<\/p>\n\n\n\n

    export PATH=$PATH:~\/.local\/bin<\/code><\/pre>\n\n\n\n

    Or load it when you start your shell (if using bash);<\/p>\n\n\n\n

    echo \"export PATH=$PATH:~\/.local\/bin\" >> ~\/.bashrc<\/code><\/pre>\n\n\n\n
    source ~\/.bashrc<\/code><\/pre>\n\n\n\n

    Or, you simply install Ansible for system wide users using the command below;<\/p>\n\n\n\n

    sudo python3 -m pip install ansible<\/code><\/pre>\n\n\n\n

    Check Ansible version;<\/p>\n\n\n\n

    ansible --version<\/code><\/pre>\n\n\n\n
    \nansible [core 2.14.2]\n  config file = None\n  configured module search path = ['\/home\/kifarunix\/.ansible\/plugins\/modules', '\/usr\/share\/ansible\/plugins\/modules']\n  ansible python module location = \/home\/kifarunix\/.local\/lib\/python3.10\/site-packages\/ansible\n  ansible collection location = \/home\/kifarunix\/.ansible\/collections:\/usr\/share\/ansible\/collections\n  executable location = \/home\/kifarunix\/.local\/bin\/ansible\n  python version = 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] (\/usr\/bin\/python3)\n  jinja version = 3.0.3\n  libyaml = True\n<\/code><\/pre>\n\n\n\n

    Install Ansible using APT Package Manager<\/h4>\n\n\n\n

    Ubuntu default repos provides Ansible packages. However, such packages may not be up-to-date.<\/p>\n\n\n\n

    To check available versions of the Ansible package, run the command below;<\/p>\n\n\n\n

    sudo apt-cache policy ansible<\/code><\/pre>\n\n\n\n

    Sample output from Ubuntu 20.04;<\/p>\n\n\n\n

    \nansible:\n  Installed: (none)\n  Candidate: 2.9.6+dfsg-1\n  Version table:\n     2.9.6+dfsg-1 500\n        500 http:\/\/us.archive.ubuntu.com\/ubuntu focal\/universe amd64 Packages\n<\/code><\/pre>\n\n\n\n

    You can as well install Ansible PPA repos that will also provide latest Ansible packages;<\/p>\n\n\n\n

    apt -y install software-properties-common<\/code><\/pre>\n\n\n\n
    sudo apt-add-repository ppa:ansible\/ansible --yes<\/code><\/pre>\n\n\n\n

    You can then install Ansible by executing the command below;<\/p>\n\n\n\n

    sudo apt install ansible<\/code><\/pre>\n\n\n\n

    Once the installation is done, you can check the version of install Ansible as follows;<\/p>\n\n\n\n

    ansible --version<\/code><\/pre>\n\n\n\n

    Setting up Ansible on Control Node<\/h3>\n\n\n\n

    Now that Ansible is installed on an Ubuntu 20.04\/Ubuntu 22.04 control node, it is time to set it up to enable you automate deployment of your tasks on the managed remote hosts via SSH protocol.<\/p>\n\n\n\n

    Setup Ansible Inventory<\/a><\/h4>\n\n\n\n

    An Ansible inventory<\/a> is a file that list or defines the hosts to be managed via Ansible. \/etc\/ansible\/hosts<\/code> is the default Ansible inventory file. It is also possible to define a custom file as your Ansible inventory file. When you use a non-default inventory file, you have to specify the path to it with -i <path><\/strong><\/code> option while running ansible.<\/p>\n\n\n\n

    An inventory file can also be used to store variable values that relate to a specific host or group in inventory.<\/p>\n\n\n\n

    In the inventory file:<\/p>\n\n\n\n