Wednesday, February 5, 2025

More Key features on Ansible

 More Ansible Key concepts.

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

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

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

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


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

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

Above: More Difference between tasks and handlers in ansible playbooks.

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

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

Above:  Custom Ansible Modules in Ansible.

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

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

Structure of an Ansible Playbook

An Ansible Playbook is structured in YAML and consists of one or more plays, which define a set of tasks to be executed on specified hosts. Here’s the typical structure of an Ansible Playbook:

Basic Structure of an Ansible Playbook

# yaml

--- - name: Example Playbook hosts: webservers become: true # Run tasks with elevated privileges (sudo) vars: package_name: nginx tasks: - name: Install a package apt: name: "{{ package_name }}" state: present when: ansible_os_family == "Debian" - name: Ensure the service is running service: name: "{{ package_name }}" state: started enabled: true handlers: - name: Restart nginx service: name: nginx state: restarted

 

Key Components of an Ansible Playbook

1. --- (YAML Header)

  • Indicates that the file is in YAML format.

2. - name: (Play Definition)

  • Defines a play, which includes target hosts, roles, and tasks.

3. hosts:

  • Specifies the group of machines (from inventory) where the playbook will run.

4. become:

  • Grants administrative privileges (sudo) when necessary.

5. vars: (Variables)

  • Allows defining reusable variables.

6. tasks: (List of Tasks)

  • Contains a list of actions to execute on the target machines.
  • Each task has:
    • name: (Descriptive name)
    • Module (e.g., aptservice)
    • Arguments (e.g., name: nginxstate: present)

7. when: (Conditional Execution)

  • Executes a task only if a condition is met.

8. handlers: (Event-Driven Actions)

  • Triggered by tasks (e.g., restart a service after package installation).

Advanced Playbook Structure

A more structured playbook with multiple plays and roles:

# yaml

--- - name: Setup Web Servers hosts: webservers become: true roles: - webserver - name: Setup Database Servers hosts: dbservers become: true roles: - database

Here, roles organize tasks into reusable components.

Ansible Playbook Best Practices

 Use roles for modularity (ansible-galaxy init <role-name>)
Leverage variables (varsvars_filesgroup_vars) for flexibility
Use handlers to restart services only when needed
Utilize when: to conditionally execute tasks
Keep playbooks DRY (Don’t Repeat Yourself) with includes & roles.


No comments:

Post a Comment

AWS Lambda Intergration with other AWS Services | Auto-Invoked Or May be Used with help of SDK.

Here’s twtech comprehensive list of AWS services that integrate with AWS Lambda , organized by category for clarity:  Lambda Integration Cat...