Working with Ansible for Configuration Management

Ansible is an open source software used for configuration management that allows IT administrators to automate configuration, deployment and management of systems. With Ansible, you can simplify complex tasks and perform them faster, making it an excellent tool for constructing and maintaining infrastructure.

Ansible provides users with a simple and effective platform that ensures the automation of IT environments. The tool relies on the use of playbooks, which are a set of instructions that allow you to automate various tasks such as setting up new servers, deploying applications or creating users with specific access levels.

In this tutorial, we will cover the basics of Ansible and guide you through the process of creating your first playbook. We will start by installing Ansible and then walk through the development of a playbook to install and configure Nginx on a CentOS 7 server.

Prerequisites

Before we proceed, ensure that you have the following installed:

  • CentOS 7 server
  • The latest version of Ansible
  • SSH access to the CentOS 7 server using a sudo-enabled user account

Step 1: Installing Ansible

To use Ansible, you need to install it on your control machine, which is the machine from which you will be controlling other servers. You may install Ansible on any operating system, including Windows, Linux or Mac.

To install Ansible on CentOS 7, execute the command below:

sudo yum install epel-release
sudo yum install ansible

The first command adds the EPEL repository to your CentOS 7 system, which contains additional packages that are not included in the default CentOS distribution. The second command installs Ansible.

Step 2: Configuring your inventory file

The Ansible inventory file defines the remote servers that Ansible will manage and their connection credentials. By default, Ansible looks for the inventory file at /etc/ansible/hosts. We can edit this configuration file to add the server details we need.

To add your server’s IP address to the inventory file, open the file /etc/ansible/hosts with your preferred text editor, add the IP address of your server and save the file.

[servers]
192.168.1.100

You can add multiple servers under the group named servers.

Step 3: Testing the connection

At this point, you can test the Ansible connection to your server to ensure that everything is set up correctly.

Execute the command below:

ansible servers -m ping -u <username>

Replace <username> with your server’s sudo-enabled user account.

If you see Success or pong, the connection is active and correct. If your connection is unsuccessful, check your server settings to ensure that everything is correctly configured.

Step 4: Developing your first Ansible playbook

Ansible playbooks are a set of instructions that run sequentially to automate server configuration. In this step, we will develop our first Ansible playbook to install and configure Nginx on CentOS 7.

Create a new file called nginx.yml in the /etc/ansible/playbooks directory with the following content:

- name: Install Nginx
  hosts: servers
  become: yes
  tasks:
  - name: Install EPEL repo
    yum:
      name: epel-release
      state: present
  - name: Install Nginx
    yum:
      name: nginx
      state: present
  - name: Start and Enable Nginx
    systemd:
      name: nginx
      enabled: yes
      state: started

This playbook contains three tasks.

  • Install EPEL repo
  • Install Nginx
  • Start and Enable Nginx

Each play in this playbook is described using a YAML syntax, with the name parameter used to give the play a descriptive name. The hosts parameter specifies which servers the play will run on. The become: yes parameter tells Ansible to run the play with elevated privileges (typically with the sudo command).

The tasks parameter includes a list of the tasks that Ansible must complete sequentially on the server.

The first task installs the EPEL repository using the yum module. The state: present parameter specifies that Ansible should only install the repository if it is not already installed.

The second task installs Nginx using the same yum module. Finally, the last task starts and enables the Nginx service using the systemd module.

Save the playbook and exit the editor.

Step 5: Running your playbook

You are now ready to run your first Ansible playbook.

Execute the command below:

ansible-playbook /etc/ansible/playbooks/nginx.yml -u <username>

The -u <username> parameter specifies which user to use for the connection.

If Ansible executes the playbook without errors, your Nginx installation should now be complete.

Conclusion

With this tutorial, you have learned the basics of working with Ansible for configuration management. We have covered the installation of Ansible on CentOS 7, configuration of the inventory file, testing the connection with your server and developing your first playbook.

Ansible is a powerful automation tool that streamlines the configuration management of servers, reduces errors, and simplifies troubleshooting. With a comprehensive understanding of Ansible, you can accomplish complex tasks within a short time, and increase your productivity, making it an essential tool for IT administrators.

Related Post