Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

How to collaborate with AI to improve your Ansible skills

March 4, 2026
Roberto Nozaki
Related topics:
Artificial intelligenceAutomation and managementLinuxPython
Related products:
Red Hat Ansible Automation PlatformRed Hat Enterprise Linux

    In January of 2023, I wrote an article about using an AI assistant to write Ansible Playbooks. At that time, I was not too impressed with the results. But I kept exploring AI, and I have to admit that now (February 2026) things look much better. The following is an article coached by me, but mostly written by AI and edited by a human.

    As sysadmins, we often find ourselves managing a fleet of servers that should be identical; but frequently in reality, they're not. Whether you are preparing for a security patch, planning a major version upgrade, or troubleshooting a mysterious bug that only seems to affect some nodes, the first question is always: "Where and what exactly are we running?"

    I needed a quick, reliable way to audit my entire inventory and get a clean report of Red Hat Enterprise Linux (RHEL) versions, logically grouped. I decided to see if AI could help me build a sophisticated reporting playbook. What started as a simple prompt, turned into a deep dive into Jinja2 grouping logic, handling unreachable hosts, and mastering the output formatting for Red Hat Ansible Automation Platform.

    The challenge: Grouping the fleet

    The goal was simple: connect to a list of RHEL servers, capture their version, and print a summary grouped by version at the end of the play. If you’ve ever tried this, you know the debug module’s default behavior is to print a JSON blob for every single host as the task runs. If you have 100 hosts, your terminal becomes a scrolling nightmare. I wanted a single, formatted report that appeared once the data collection finished.

    Next, I'll discuss how we built a production-grade reporting tool, and the good stuff I learned along the way.

    Step 1: Solve the unreachable problem

    The first lesson I learned from the AI was about variable resilience. If a server is down, Ansible Automation Platform usually drops it from the play. If your reporting logic tries to look up facts for a host that didn't respond, the whole playbook crashes with an UndefinedError.

    The trick is to use ansible_play_hosts instead of ansible_play_hosts_all. Use ansible_play_hosts_all for every host in your targeted inventory and ansible_play_hosts for only the hosts that actually responded and succeeded.

    Step 2: Use Jinja2 as a logic engine

    Most people use Jinja2 for simple variable replacement. But for a report like this, you need it to behave more like a Python script. We used Python-style dictionary methods directly inside a set_fact task to group our data as follows:

    {% if ver not in summary %}
      {% set _ = summary.update({ver: []}) %}
    {% endif %}
    {% set _ = summary[ver].append(host) %}

    By using .update() and .append(), we built a dynamic dictionary where each RHEL version is a key, and the value is a list of hostnames. 

    Pro tip: The set _ = syntax keeps the template from printing "None" every time it calls a function.

    Step 3: Make it picky-coder approved

    To make this process ready for publication, we had to look at best practices. For picky coders, that means:

    • Using FQCNs: Always use ansible.builtin.set_fact instead of the short-form set_fact.
    • Whitespace control: Use {%- and -%} to strip out unwanted new lines from the Jinja2 logic.
    • Using the literal scalar |: Use this pipe character in YAML to ensure the report preserves line breaks in the terminal.

    The final solution

    The final, audited playbook generates a sorted, hierarchical report and even lists unreachable hosts at the bottom for full transparency.

    YAML:

    ---
    - name: Identify RHEL Version
      hosts: all
      gather_facts: false  # Disable default 'all' gathering
      tasks:
        - name: Gather minimal distribution facts
          ansible.builtin.setup:
            gather_subset:
              - "!all"     # Start with nothing
              - "min"      # Only minimal facts (includes distribution)
    
        - name: "Consolidate RHEL version data into a report"
          run_once: true
          delegate_to: localhost
          ansible.builtin.set_fact:
            rhel_report: |
              {%- set summary = {} -%}
              {%- for host in ansible_play_hosts -%}
                {%- set ver = hostvars[host].ansible_facts['distribution_version'] | default('Unknown') -%}
                {%- if ver not in summary -%}
                  {%- set _ = summary.update({ver: []}) -%}
                {%- endif -%}
                {%- set _ = summary[ver].append(host) -%}
              {%- endfor -%}
    
              REPORT: RHEL VERSION DISTRIBUTION
              =================================
              {% for version in summary.keys() | sort %}
              Version: {{ version }}
              {% for host in summary[version] | sort %}
              - {{ host }}
              {% endfor %}
    
              {% endfor %}
              {%- set unreachable = ansible_play_hosts_all | difference(ansible_play_hosts) -%}
              {%- if unreachable %}
              UNREACHABLE HOSTS
              =================
              {% for host in unreachable | sort %}
              - {{ host }}
              {% endfor %}
              {%- endif %}
    
        - name: "Emit formatted report to console"
          run_once: true
          delegate_to: localhost
          ansible.builtin.debug:
            msg: "{{ rhel_report.split('\n') }}"
    

    This is the output from my lab (YAML):

    PLAY [Identify RHEL Version] *********************************************
    
    TASK [Gather minimal distribution facts] *********************************
    Thursday 26 February 2026  16:03:46 -0500 (0:00:00.004)       0:00:00.004 ok: [node1]
    ok: [node2]
    ok: [node3]
    fatal: [node4]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host node4 port 22: No route to host", "unreachable": true}
    fatal: [win1x]: UNREACHABLE! => {"changed": false, "msg": "Data could not be sent to remote host \"win1x\". Make sure this host can be reached over ssh: ssh: connect to host win1x port 22: No route to host\r\n", "unreachable": true}
    fatal: [node5]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host node5 port 22: No route to host", "unreachable": true}
    
    TASK [Consolidate RHEL version data into a report] ***********************
    Thursday 26 February 2026  16:03:50 -0500 (0:00:03.509)       0:00:03.513 ok: [node1 -> localhost]
    
    TASK [Emit formatted report to console] *********************************
    Thursday 26 February 2026  16:03:50 -0500 (0:00:00.020)       0:00:03.533 
    
    ok: [node1 -> localhost] => {
        "msg": [
            "REPORT: RHEL VERSION DISTRIBUTION",
            "=================================",
            "Version: 9.7",
            "- node1",
            "- node2",
            "",
            "Version: 8.10",
            "- node3",
            "",
            "UNREACHABLE HOSTS",
            "=================",
            "- node4",
            "- node5",
            "- win1x",
            ""
        ]
    }
    
    PLAY RECAP ****************************************************************
    node1 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0   
    node2 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0   
    node3 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0   
    node4 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0   
    node5 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0   
    win1x : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0

    Wrap up

    What I learned from this AI collaboration wasn't just about syntax; it was about data architecture. Instead of settling for messy logs, we used the control node to aggregate data, handled failures gracefully, and presented the results in a human-readable format. Whether you're auditing RHEL versions or checking kernel levels across a thousand nodes, these grouping and reporting patterns will save you hours of manual investigation. Of course, if I were handling thousands of boxes, I would also send the output to a database or a CSV file. But that was just what I needed initially.

    Related Posts

    • Generate Ansible Playbooks using natural language prompts

    • Debug Ansible errors faster with an AI monitoring agent

    • Multi-homed inventory management with Ansible Automation Platform

    • Manage RHEL inventory using natural language

    Recent Posts

    • Introducing virtualization platform autopilot

    • Integrate zero trust workload identity manager with Red Hat OpenShift GitOps

    • Best Practice Configuration and Tuning for Linux and Windows VMs

    • Red Hat UBI 8 builders have been promoted to the Paketo Buildpacks organization

    • Using eBPF in Red Hat products

    What’s up next?

    Learning Path Ansible Content Collections

    Get started with Ansible Playbooks

    For the best experience in this learning path, we suggest that you complete...
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility