Introduction

Ansible is a powerful tool used for automation and configuration management. While it is commonly associated with tasks like server provisioning and application deployment, its flexibility allows it to be used for a wide variety of tasks, even something as simple as searching for a specific band member in a list of bands. In this article, we’ll create an Ansible playbook that searches for bands formed with a member named “Starr” and outputs the result.

Disclaimer

Full example: https://www.redhat.com/sysadmin/ansible-jinja-lists-dictionaries

The Playbook

Below is the Ansible playbook designed for this task. It includes a list of bands, each with its members, formation year, and the decade they were most active. The playbook filters through this list to find any band that has a member named “Starr.”

---
- name: List bands formed with a member named Starr
  hosts: localhost
  gather_facts: no
  vars:
    bands:
      - name: The Beatles
        members:
          - Lennon
          - McCartney
          - Harrison
          - Starr
        formed: 1960
        decade: 60s

      - name: The Eagles
        members:
          - Frey
          - Henley
          - Leadon
          - Meisner
        formed: 1971
        decade: 70s

      - name: Run DMC
        members:
          - Simmons
          - McDaniels
          - Mizell
        formed: 1983
        decade: 80s

      - name: Red Hot Chili Peppers
        members:
          - Kiedis
          - Smith
          - Frusciante
          - Balzary
        formed: 1982
        decade: 90s

      - name: Destiny's Child
        members:
          - Knowles
          - Rowland
          - Williams
        formed: 1990
        decade: 00s

      - name: Black Eyed Peas
        members:
          - Adams
          - Lindo
          - Gomez
        formed: 1995
        decade: 00s

  tasks:
    - name: Display bands with a member named Starr
      ansible.builtin.debug:
        msg: "{{ bands | selectattr('members', 'search', 'Starr') | map(attribute='name') | list }}"

Explanation of the Code

  • Playbook Structure:

    • The playbook targets the localhost and does not gather facts, as this is a simple data-processing task.
  • Variables Section:

    • The bands variable is defined as a list containing information about several bands, including their names, members, formation year, and the decade they were active.
  • Task:

    • A single task is defined that uses the ansible.builtin.debug module to filter through the bands list and find any band with a member named “Starr.”
    • The filtering is done using Jinja2 templating with the selectattr filter, which searches for “Starr” within the members list of each band.

Running the Playbook

To run this playbook, save it as list_bands.yml and execute the following command in your terminal:

ansible-playbook list_bands.yml

Expected Output

When the playbook is executed, the output will look like this:

PLAY [List bands formed with a member named Starr] *********************************************************************************************

TASK [Display bands with a member named Starr] *************************************************************************************************
ok: [localhost] => {
    "msg": [
        "The Beatles"
    ]
}

PLAY RECAP *************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Analysis of the Output

As shown in the output, the playbook successfully found that “The Beatles” is the band with a member named “Starr.” The playbook executed successfully, with Ansible confirming that the task was “ok,” meaning it completed as expected without any changes, failures, or other issues.

Conclusion

This example Playbooknstrates how Ansible’s flexibility extends beyond traditional IT automation tasks. By leveraging Ansible’s powerful templating system, you can automate various types of data processing tasks. This playbook, while simple, showcases the potential of Ansible for managing and filtering data, opening up a wide range of possibilities for its use in unconventional automation tasks.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and AWX System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza