How to set up Ansible

Ansible is an IaCInfrastructure as Code open-source software suite written in Python and can tackle software provisioning, updating, configuration management, and application functionality. Moreover, Ansible allows an automated IT experience that streamlines the process flow. Ansible uses files to store our automation code for all these tasks. These files are called an Ansible Playbook.

Furthermore, Ansible is an agentless push model software. This means that our host machines do not have to install any software to work, and Ansible pushes its commands directly to its hosts from the central server via SSH. This makes it easy to use and set up, which makes it a very popular software.

Installation

Now that we know what Ansible is, we need to use it. For that, we will follow the commands below.

  1. We will first go to the official Ansible website and click on our machine's operating system link. In this Answer, we will follow the installation process for Ubuntu.

  2. Now we will follow the instructions given to install Ansible on our machine.

Configuration

Now let us proceed to the configuration aspect of our setup.

  1. After installation, we can head to our "/etc/ansible" directory to interact with our Ansible files. Here, we can create another folder named "playbooks" to store our YML scripts for what we want to automate.

--| etc/ansible
--| ansible.cfg // configuration file
--| hosts // stores hosts
--| roles // stores processes
--| playbooks // stores scripts
Ansible directory hierarchy
  1. Let's first open our "ansible.cfg" file and locate the commands given below. We must modify them by either uncommenting them and/or setting them to False depending on our Ansible version. This allows us to bypass host key checking, removing a security layer. This is not recommended, but setting up the host key is unnecessary for our test setup.

host_key_checking=False

Note: If this line of code is not in our configuration file, we can run the following command to create an updated configuration file with all plugins installed: ''ansible-config init --disabled -t all > ansible.cfg"

  1. Now we can set our hosts by modifying our "hosts" file. In this, we can either add our remote system's IP or create a group within which we can add multiple remote system IPs. A sample code is provided below.

[EducativeGroup]
172.17.0.1 # remote machine IP(s)
[EducativeGroup:vars]
ansible_password=root@123 # remote machine password
Modify hosts file
  1. Now we can run CLI code (ad-hoc command) on all machines under our EducativeGroup and work on all machines through the central server. An example command is shown below.

ansible EducativeGroup -m ping
Ping command to all IPs in the group
Successful command
Successful command

Playbook approach

A playbook accomplishes the same thing we have done through the CLI; however, it is just a more organized method.

--| Playbook
--| Plays
--| Tasks
Playbook method structure

We can implement this playbook method by following the steps given below.

  1. Open "etc/ansible/playbooks" and create a YML file. We can name it anything, but for our example, we will use filename.

  2. Now let us see what our YML file looks like.

# Playbook
---
# Play
- name: filename
hosts: EducativeGroup
# Tasks
tasks:
- name: install nano
## Module
apt:
name: nano
state: latest
  1. Now we can open our terminal inside the playbook and run the following command to run our playbook.

sudo ansible-playbook filename.yml
Run filename.yml playbook

Conclusion

Ansible provides an easy solution to mundane and repetitive tasks. It also ensures consistency while managing multiple remote IPs and groups. Overall, Ansible is a great automation tool that allows quick actions on multiple remote machines and prevents any potential human errors.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved