Wednesday, February 5, 2025

Ansible Playbooks and Roles


Double-click on the image to zoom-out ...Larger.

To return to Home page: Refresh Page or Take ESC Button on Keyboar

Starting with Ansible Playbooks and Roles for organizing Playbooks

Getting Started with Ansible Playbooks

An Ansible Playbook is a YAML-based script that defines tasks to automate system configurations, deployments, and management. Below is a step-by-step guide to help you get started.

1. Install Ansible

Ensure Ansible is installed on your control machine (For Linux -ubuntu: use the script below).

https://github.com/Devopspat35/ansible-roles/blob/master/bootstrap-ansible.sh

Verify the installation:

ansible --version
2. Set Up the Inventory File

The inventory file (/etc/ansible/hosts or a custom file) defines the managed hosts.

Create an inventory file (inventory.ini):

# ini

[webservers] 192.168.1.10 ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa 192.168.1.11 ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa

Test connectivity:

ansible all -m ping -i inventory.ini

Expected Output:

# json

192.168.1.10 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.1.11 | SUCCESS => { "changed": false, "ping": "pong" }
3. Create Your First Playbook

A playbook is a YAML file that defines tasks to execute.

Example: Install and Start Nginx

Create a new file install_nginx.yml:

# yaml

- name: Install and Start Nginx hosts: webservers become: yes # Run as sudo tasks: - name: Install Nginx apt: name: nginx state: present - name: Start Nginx service service: name: nginx state: started
4. Run the Playbook

Use the ansible-playbook command:

ansible-playbook -i inventory.ini install_nginx.yml

Expected Output:

# markdown

PLAY [Install and Start Nginx] **************** TASK [Install Nginx] ************************** changed: [192.168.1.10] changed: [192.168.1.11] TASK [Start Nginx service] ******************** ok: [192.168.1.10] ok: [192.168.1.11] PLAY RECAP ************************************ 192.168.1.10 : ok=2 changed=1 192.168.1.11 : ok=2 changed=1
5. Verify the Installation

Check if Nginx is running:

ansible webservers -i inventory.ini -m shell -a "systemctl status nginx"

Alternatively, access it in a browser:

# cpp

http://192.168.1.10
6. Add Handlers for Better Management

Handlers are triggered only when changes occur. Modify install_nginx.yml:

# yaml

- name: Install and Start Nginx hosts: webservers become: yes tasks: - name: Install Nginx apt: name: nginx state: present notify: Restart Nginx # Calls handler - name: Start Nginx service service: name: nginx state: started handlers: - name: Restart Nginx service: name: nginx state: restarted

Re-run the playbook:

ansible-playbook -i inventory.ini install_nginx.yml
7. Use Variables for Flexibility

Define variables in vars.yml:

# yaml

nginx_package: nginx

Modify the playbook to use variables:

# yaml

- name: Install and Start Nginx hosts: webservers become: yes vars_files: - vars.yml tasks: - name: Install Nginx apt: name: "{{ nginx_package }}" state: present notify: Restart Nginx - name: Start Nginx service service: name: "{{ nginx_package }}" state: started handlers: - name: Restart Nginx service: name: "{{ nginx_package }}" state: restarted

Run it:

ansible-playbook -i inventory.ini install_nginx.yml
8. Debugging & Testing
  • Check Syntax:
    ansible-playbook install_nginx.yml --syntax-check
  • Run in Dry Mode:
    ansible-playbook -i inventory.ini install_nginx.yml --check
  • Enable Verbose Mode:
    ansible-playbook -i inventory.ini install_nginx.yml -vvv

 Thoughts:

  • Explore roles for organizing playbooks (ansible-galaxy init myrole).
  • Use Jinja2 templates for configuration files.
  • Integrate Ansible with Docker, Kubernetes, or AWS.
  •  

Ansible Roles for Organizing Playbooks

Ansible roles help organize playbooks by breaking them into reusable components. A role is a structured directory that contains tasks, handlers, templates, variables, and other necessary configurations.

1. Why Use Roles?
  • Modular & Reusable: Write once, use multiple times.
  • Scalability: Manage complex infrastructure easily.
  • Separation of Concerns: Keep playbooks clean and manageable.
2. Creating an Ansible Role

You can manually create a role structure or use ansible-galaxy to generate it.

Manual Structure

A role follows this directory structure:

# perl

my-playbook/ ├── roles/ │ ├── nginx/ │ │ ├── tasks/ # Contains task files (main.yml) │ │ ├── handlers/ # Handlers for service restart, etc. │ │ ├── templates/ # Jinja2 templates for configuration files │ │ ├── files/ # Static files (e.g., scripts, binaries) │ │ ├── vars/ # Role-specific variables │ │ ├── defaults/ # Default variables │ │ ├── meta/ # Role metadata (dependencies, author) │ │ ├── tests/ # Testing scripts │ │ └── README.md # Documentation ├── inventory.ini ├── site.yml # Main playbook

Using ansible-galaxy to Generate a Role

Run the following command:

ansible-galaxy init roles/nginx

This creates the necessary directories and files automatically.

3. Writing Role Components

A. Tasks (roles/nginx/tasks/main.yml)

Defines the sequence of actions.

# yaml

- name: Install Nginx apt: name: nginx state: present notify: Restart Nginx - name: Ensure Nginx is running service: name: nginx state: started enabled: yes
B. Handlers (roles/nginx/handlers/main.yml)

Executed when notified.

# yaml

- name: Restart Nginx service: name: nginx state: restarted

 C. Variables (roles/nginx/vars/main.yml)

Define custom variables.

# yaml

nginx_port: 80
D. Templates (roles/nginx/templates/nginx.conf.j2)

Jinja2 template for configuration files.

# jinja

server { listen {{ nginx_port }}; server_name localhost; location / { root /var/www/html; index index.html; } }
E. Default Variables (roles/nginx/defaults/main.yml)

Define default values.

# yaml

nginx_port: 80
F. Files (roles/nginx/files/index.html)

Static files (e.g., website content).

4. Using the Role in a Playbook

Create site.yml to apply the role.

# yaml

- name: Configure Web Server hosts: webservers become: yes roles: - nginx

Run the playbook:

ansible-playbook -i inventory.ini site.yml
5. Using Role Dependencies

Define dependencies in roles/nginx/meta/main.yml:

# yaml

dependencies: - role: common
6. Sharing Roles with Ansible Galaxy

Upload roles for reuse:

ansible-galaxy role install myrole
Insights:

Ansible roles provide a structured way to organize playbooks, making them reusable, scalable, and maintainable.

No comments:

Post a Comment

AWS Lambda | Cold Start, Warm Start & Provisioned Concurrency.

twtech break down of a Cold Start, Warm start   and Provisioned Concurrency for AWS Lambda . when they happen, and how to control them. 1. ...