Guide to Setting Up and Automating Tasks with Ansible
Introduction
As a DevOps engineer, streamlining infrastructure management and automating repetitive tasks are crucial for improving efficiency and reliability. Ansible is a powerful automation tool that allows you to manage your infrastructure as code, enabling consistent and repeatable deployments.
In this guide, we will walk through the process of setting up Ansible, configuring the necessary files, and automating tasks on managed nodes. By the end of this tutorial, you will have an Ansible project that installs Nginx, performs file operations, and copies files from the Control Node to managed nodes
Prerequisites
- A Control Node (where Ansible will be run from).
- Managed Nodes (servers that will be managed by Ansible).
- SSH keys configured for passwordless login from the Control Node to the Managed Nodes.
Steps
1. Install Ansible on Your Control Node
First, ensure Ansible is installed on your Control Node, you have to be a root user to perform this task or have a sudo permission. I discussed this in my previous work here. Use these command to update and install ansible:
sudo apt-get update
sudo apt-get install ansible
ansible --version
2. Create Your Public and Private keys
This will be use to access the manage nodes from the control node. on the terminal run the command and follow the prompt to generate the ssh keys.
ssh-keygen # will generate the keys
ls -a # will list all folder
cd .ssh/ # let you into home directory (/home/username/.ssh)
ls # will list the files in .shh directory
cat id_rsa.pub # will expose the ssh key generated
Copy the content of the public key from the control node into .ssh/authorized_keys file on both web node, past it on a new line and save. run the below commands on both manage nodes
chmod 600 ~/.ssh/authorized_keys # This command sets permissions on the file authorized_keys located inside the .ssh directory
chmod 700 ~/.ssh #This command sets permissions on the .ssh directory in the user's home directory.
3. Copy key Pair File Into Control Node
The key pair generated and downloaded during Vm set up need to be copied into control node (/home/adminuser/) from where it was saved. I Copy the private key from azure /home/ebenezer where it resides, using the code below, However, permissions level was set to 400 before copying.
scp -i /home/ebenezer/ansible-key.pem /home/ebenezer/ansible-key.pem adminuser@4.223.169.191:/home/adminuser/ansible-key.pem
4. Set Up the Project Directory
Create a project directory to organize your Ansible files:
mkdir ~/ansible_project && cd ~/ansible_project
5. Create Inventory file
Create an inventory file (inventory.ini
) to list your managed nodes:
vim inventory.ini
6. Create The Ansible Configuration File
Create an ansible.cfg
file to configure default Ansible settings:
vim ansible.cfg #paste or write your configuration file
7. Test Connectivity of Manage Nodes
Create a playbook (test_connectivity.yaml
) to test connectivity:
vim test_connectivity.yaml #paste or write your playbook
ansible-playbook -i inventory.ini test_connectivity.yaml
ansible -i inventory.ini managenodes -m ping
ansible -i inventory.ini managenodes --list
With successful communication established with the control nodes and the manage nodes, you can now explore using Ansible playbooks to automate tasks on your manage nodes.
8. Install Nginx on Manage Nodes
Create a playbook (install_nginx.yaml
) to install and configure Nginx:
vim install_nginx.yaml # paste or write your playbook and :wq
ansible-playbook -i inventory.ini install_nginx.yaml # run your playbook
8a. Testing:
Open your browser and type in the public ip of your manage nodes. You should see a page similar to the ones below. make sure that port 80 is open in your manage nodes.
9. Automate File Operations on Managed Nodes
Create a playbook (create_files_and_links.yaml
) to perform file operations:
vim create_files_and_links.yaml # paste or write your playbook and :wq
ansible-playbook -i inventory.ini create_files_and_links.yaml # run your playbook
9. Use Template for dynamic content
Create a template file (index.html.j2
) to use with the ansible.builtin.template
module:
Create the directory structure:
mkdir -p ~/ansible_project/template
vim ~/ansible_project/template/index.html.j2 # peate or write what you want to copy
vim use_template.yaml # creates and ansible playbook
ansible-playbook -i inventory.ini use_template.yaml #run your ansible playbook
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
<header>
<h1>Welcome to My Simple HTML Page</h1>
</header>
</body>
</html>
---
- hosts: managenodes
become: yes # Ensure tasks are run with elevated privileges if necessary
tasks:
- name: Copy index.html to the destination folder
ansible.builtin.template:
src: template/index.html.j2
dest: /home/{{ ansible_user }}/test_directory/index.html
A Glimpse At Our Manage Nodes
The file operations on the manage nodes where successful, this automation process is vita, it helps the engineer or system admin to setup working lab seamlessly for multiply members of staff with just a click
Conclusion
By following this guide, you have successfully set up Ansible, created an inventory of managed nodes, tested connectivity, installed Nginx, automated file operations, copied files from the Control Node to managed nodes, and used templates for dynamic content. This setup allows for efficient management and automation of tasks across your infrastructure.