Linux 117: Automating with Ansible for Configuration Management

Linux 117: Automating with Ansible for Configuration Management

As your infrastructure grows, manually configuring and maintaining each server becomes inefficient and error-prone. Ansible is a powerful automation tool that simplifies configuration management, application deployment, and task execution across multiple Linux systems — all without installing agents.


1. What is Ansible?

Ansible is an open-source automation platform that allows you to manage servers via SSH using simple, human-readable YAML files called playbooks.

  • Agentless: No software required on managed hosts.
  • Idempotent: Ensures the same result every time.
  • Easy to Learn: Uses plain English in YAML format.


2. Installing Ansible

On Debian/Ubuntu


sudo apt update sudo apt install ansible

On RHEL/CentOS/Fedora


sudo dnf install epel-release sudo dnf install ansible

Verify installation:


ansible --version

3. Setting Up an Inventory

The inventory file lists the hosts Ansible will manage.

Example: /etc/ansible/hosts


[webservers] 192.168.1.10 192.168.1.11 [dbservers] 192.168.1.20

You can also use dynamic inventory scripts or tools like Ansible Tower for large environments.


4. Connecting with SSH

Ansible uses SSH for communication. Ensure you can connect to the managed hosts with SSH keys:


ssh-copy-id user@192.168.1.10

Test connection:


ansible all -m ping -u user

5. Running Your First Ansible Command

Use ad-hoc commands to quickly execute tasks:


ansible webservers -m apt -a "name=nginx state=present" -b -u user

  • -m: Module (e.g., apt, yum, copy, service)
  • -a: Arguments
  • -b: Become (sudo)
  • -u: User


6. Creating and Running a Playbook

A playbook defines multiple tasks to run in sequence.

Example: install_nginx.yml


--- - name: Install and Start NGINX hosts: webservers become: yes tasks: - name: Install NGINX apt: name: nginx state: present - name: Start NGINX service: name: nginx state: started enabled: yes

Run the playbook:


ansible-playbook install_nginx.yml -u user

7. Variables and Templates

Use variables to customize tasks:


vars: nginx_port: 8080

Use Jinja2 templates (.j2) to configure dynamic files:

Example: nginx.conf.j2


server { listen {{ nginx_port }}; server_name localhost; root /var/www/html; }

Use the template module to apply it.


8. Ansible Roles for Reusability

Roles help organize your playbooks by separating code into reusable components:


roles/ ├── nginx/ │ ├── tasks/ │ ├── handlers/ │ ├── templates/ │ └── defaults/

Use roles in playbooks:


roles: - nginx

9. Managing Secrets with Ansible Vault

Encrypt sensitive data like passwords and keys:


ansible-vault create secrets.yml

Edit:


ansible-vault edit secrets.yml

Use in playbooks:


vars_files: - secrets.yml

Run with decryption:


ansible-playbook playbook.yml --ask-vault-pass

10. Conclusion

Ansible transforms Linux server management from tedious manual tasks to reliable, repeatable automation. With just a few commands or YAML files, you can provision entire environments quickly and consistently.


Next Steps:

  • Learn Ansible Galaxy for reusable community roles
  • Explore Ansible AWX/Tower for a web UI and job scheduling
  • Automate more complex workflows like multi-node deployments


Coming next: Linux 118: Systemd Deep Dive – Managing Services and Boot Processes

Post a Comment (0)
Previous Post Next Post

ads