Installing Ansible in the control node – Infrastructure as Code (IaC) with Terraform

Ansible requires a Linux/Unix machine (preferably), and you should have Python 2.x or 3.x installed.

As the Ansible control node runs on Ubuntu, Ansible provides a personal package archive (PPA) repository that we can configure to download and install Ansible using apt commands.

Use the following commands to install Ansible on the server:

$ sudo apt update
$ sudo apt install software-properties-common -y
$ sudo apt-add-repository –yes –update ppa:ansible/ansible $ sudo apt install ansible -y

To check whether Ansible has been installed successfully, run the following command:

$ ansible –version
ansible 2.9.27

And, as we see, ansible 2.9.27 is successfully installed on your control node.

Ansible uses an inventory file to manage nodes. Therefore, the next step is to set up an inventory file.

Setting up an inventory file

An inventory file within Ansible is a file that allows you to group your managed nodes according to roles. For example, you can define roles such as webserver and dbserver and group related servers together. You can use IP addresses, hostnames, or aliases for that.

Tip

Always use aliases because they provide room for IP address and hostname changes.

You can run Ansible commands on hosts or a group of hosts using the role tagged to them. There is no limit to servers that can have a particular role. If your server uses a non-standard SSH port, you can also use that port within the inventory file.

The default location of the Ansible inventory file is /etc/ansible/hosts. If you look at the / etc/ansible directory ownership, it is owned by the root user. We want to use the ansible user that we created for security purposes. Therefore, we must change the /etc/ansible directory ownership and its subdirectories and files to ansible. Use the following command to do so:

$ sudo chown -R ansible:ansible /etc/ansible

We can then switch the user to ansible and clone the Git repository that contains the required files into the control server using the following commands:

$ sudo su – ansible
$ git clone https://github.com/PacktPublishing/Modern-DevOps-Practices-2e.git \ modern-devops
$ cd ~/modern-devops/ch9/ansible-exercise

In our scenario, we have a web server called web and a database server called db. Therefore, if you check the host file called hosts within the repository, you will see the following:

[webservers]
web ansible_host=web
[dbservers]
db ansible_host=db
[all:vars]
ansible_python_interpreter=/usr/bin/python3

The [all:vars] section contains variables applicable to all groups. Here, we’re explicitly defining ansible_python_interpreter to python3 so that Ansible uses python3 instead of python2. As we’re using Ubuntu, python3 comes installed as default, and python2 is deprecated.

We also see that instead of using web directly, we’ve specified an ansible_host section. That defines web as an alias, pointing to a host with the hostname web. You can also use the IP address instead of the hostname if required.

Tip

Always group the inventory according to the function performed. That helps us to apply a similar configuration to a large number of machines with a similar role.

As we want to keep the configuration with code, we would wish to stay within the Git repository itself. So, we must tell Ansible that the inventory file is in a non-standard location. To do so, we will create an Ansible configuration file.

Leave a Reply

Your email address will not be published. Required fields are marked *