Ansible Installation and Configuration Guide: Checks disk space on remote servers and deletes specified files if the space is above a certain threshold.

Harshal Jethwa
Cloud Native Daily
Published in
4 min readMar 23, 2024

Introduction to Ansible:

Ansible is an open-source automation tool that simplifies the process of managing and configuring servers, applications, and network devices. It allows you to automate repetitive tasks, deploy applications, and manage infrastructure efficiently.

Installation Requirements

Control Node Setup

  • Ansible requires a control node, which can be any machine with Python 3.8 installed.
  • For managing nodes, Ansible communicates over SSH and SFTP or WinRM for Windows hosts.
  • Managed nodes must have Python 2 (version 2.6 or later) or Python 3 (version 3.5 or later) installed.
  • For Windows nodes, PowerShell 3.0 or later and at least .NET 4.0 are necessary.

Installing Ansible

  1. Ensure pip is installed on your system:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py --user

2. Install Ansible using pip:

python -m pip install --user ansible

3. Verify installation:

ansible --version

Setting up Inventory File

The inventory file in Ansible records details of managed nodes and organizes them into groups and subgroups.

Create or edit the inventory file:

sudo nano /etc/ansible/hosts

Example inventory file:

[servers]
server1 ansible_host=203.x.xxx.xxx
server2 ansible_host=203.x.xx.xxx

[all:vars]
ansible_python_interpreter=/usr/bin/python3

List inventory details:

ansible-inventory --list -y

Configuring SSH Keys

SSH keys enable secure communication between the Ansible server and managed nodes.

Generate SSH key pair:

ssh-keygen

Copy the public key to managed nodes:

ssh-copy-id -i $HOME/.ssh/id_rsa.pub root@203.0.113.111
ssh-copy-id -i $HOME/.ssh/id_rsa.pub root@203.0.113.112

Test connection:

ansible all -m ping -u root

Targeting Individual Hosts and Groups

Ansible allows targeting specific hosts or groups for executing commands.

  • To target a single host:
ansible <hostname> -m <module> -a <arguments>

To target a group:

ansible <groupname> -m <module> -a <arguments>

Playbooks

Playbooks are YAML files defining tasks and configurations to be applied to managed nodes.

Web-nginx.yaml

This playbook installs and configures an Nginx web server.

---
- name: Configure nginx web server
hosts: servers
become: true
become_user: root
become_method: sudo
tasks:
- name: Install nginx
ansible.builtin.apt:
name: nginx
state: latest

- name: Comment out specific line in nginx config
ansible.builtin.lineinfile:
path: /etc/nginx/sites-available/default
regexp: '^(\s*listen\s+\[::\]:80\s+default_server;)'
line: "# {{ item }}"
loop:
- "listen [::]:80 default_server;"

- name: Restart Nginx service
ansible.builtin.service:
name: nginx
state: restarted

- name: Start Nginx
ansible.builtin.service:
name: nginx
state: started
enabled: yes

- name: Deploy web app
ansible.builtin.copy:
src: index.html
dest: /var/www/html/

Storage.yaml

This playbook checks disk space on remote servers and deletes specified files if the space is above a certain threshold.

---
- name: Storage
hosts: servers
become: yes
tasks:
- name: Check /tmp freespace
shell: sudo df | grep /dev/mapper/ubuntu--vg-ubuntu--lv | awk '{print $5}';
register: tmp_freespace

- name: Print the output of the shell command
debug:
var: tmp_freespace.stdout

- fail:
msg: '"{{ tmp_freespace.stdout }}" value'
when: tmp_freespace.stdout|float is gt 50

- name: Remove
shell: rm /home/krunal/*;
register: ls_output

- name: Print the output of the shell command
debug:
var: ls_output.stdout

Conclusion

This guide provides a comprehensive overview of installing and using Ansible for server management and automation. With Ansible, you can streamline your workflows, improve efficiency, and maintain consistency across your infrastructure.

Follow me :

Linkedin: https://www.linkedin.com/in/harshaljethwa/

GitHub: https://github.com/HARSHALJETHWA19/

Twitter: https://twitter.com/harshaljethwaa

Thank You!!!

--

--

Harshal Jethwa
Cloud Native Daily

DevOps | Docker | Linux | Jenkins | AWS | Git | Terraform | Technical Blogger