How can we help?
Categories
< All Topics
Print

Classes: Install and Configure Ansible

Previously we have introduced Ansible. Therefore, this article will discuss how to install and configure Ansible on Linux.

In short, Ansible is an agentless automation tool that uses SSH and Python to manage remote servers. It does not need a database server or run as a daemon or service. You can easily install it on a server or a laptop, and that’s it; after that, you can start using Ansible to manage an entire fleet of remote servers from your laptop.

When Ansible manages remote machines, it does not leave software installed or running on them. So there’s no real question about how to upgrade Ansible when moving to a new version. In addition, currently, Ansible can be run from any machine with Python 2 (version 2.7) or Python 3 (versions 3.5 and higher) installed.

Ansible only needs python2 (version 2.7) or python3 (version 3.5 and higher) installed on the remote servers to manage them, and that’s it; no need for other software or service, which is why it’s called agentless.

Therefore, to understand better, please closely examine how to install and configure Ansible on Linux below.

Installing Ansible on Linux

  • On Fedora:
$ sudo dnf install ansible 
  • On RHEL and CentOS:
$ sudo yum install ansible
  • On Ubuntu:
$ sudo apt update 
$ sudo apt install software-properties-common
$ sudo apt-add-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible

Once you have installed Ansible, run the command below to make sure that Ansible is installed properly.

$ ansible --version

Then, you will see the output would be something like the below.


ansible 2.9.16
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.17 (default, Sep 30 2020, 13:38:04) [GCC 7.5.0]

Due to the output, we could see that Ansible has a configuration that can be optimized as your need at /etc/ansible/ansible.cfg
By default, the configuration is enough for you to make an Ansible role as well as Ansible read the ssh keys form ~/.ssh/id_rsa. Although It depends on what users are logging in, you still can optimize that in the [ssh_connection] section. Not only can you make it more fun by optimizing the color of your ansible output, but you can also change it in the [colors] section as well.

As I mentioned in the previous classes, you need to follow the Ansible hierarchy to make Ansible run well. By default, ansible already created files on /etc/ansible path for inventory and roles, but you can move it to your desired path.

Ansible hierarchy

  • Inventory: this file is for the inventory list of your server. You can group it with server functions or roles.
    Example format:
    [allserver]
    192.168.20.1
    192.168.20.2
    192.168.20.3
    192.168.20.4

    [lb_server]
    192.168.20.1
    192.168.20.2
  • Group_vars: here we assign variables to particular groups
    This will be applied to all servers when Ansible runs
    Example format:
    ntpserverip: ntp.domain.com
    rsyslog_version: ‘*’
  • Host_vars: if systems need specific variables, put them here.
    This will be applied to some servers that are defined in this path.
    Thus, the file should be named with the IP listed in the inventory
    Example format:
    host_name: lb-vm1
    host_domain: domain.com
    full_host_name: “{{ host_name }}.{{ host_domain }}”
    ip_address: 192.168.20.1
  • site.yml: master playbook
    This file will be called in the first time when running ansible-playbook
    In this file, you need to define what role and which server will be executed. Then, you need to save the file in yml format.
    Example format:
    hosts: allserver
    gather_facts: false
    roles:
    – usergroup
  • roles: list of roles and scripts that will be executed.

So, do you want to know how to use Ansible?
Please follow this link.

Table of Contents