Execution
Date 23 May 2025 11:12:38 +0100
Duration 00:00:01.94
Controller ssh-gw-4.layershift.com
User root
Versions
Ansible 2.16.11
ara 1.7.2 / 1.7.2
Python 3.10.10
Summary
1 Hosts
4 Tasks
4 Results
1 Plays
1 Files
0 Records

File: /home/ssh-gateway/ansible/kuly/generate_inventory_from_files.yml

---
- name: Generate Ansible inventory from fetched .ini files
  hosts: localhost
  gather_facts: false
  vars:
    base_dir: "fetched_files"
    output_inventory_file: "generated_inventory.ini"

  tasks:
    - name: Find all .ini files under each host folder
      ansible.builtin.find:
        paths: "{{ base_dir }}"
        patterns: "*.ini"
        recurse: true
      register: ini_files

    - name: Debug - show how many files found
      ansible.builtin.debug:
        msg: "Found {{ ini_files.files | length }} .ini files."

    - name: Extract hostnames from filenames
      ansible.builtin.set_fact:
        hosts_list: >-
          {{
            ini_files.files |
            map(attribute='path') |
            map('basename') |
            map('regex_replace', '-panel.ini$', '') |
            list
          }}

    - name: Write inventory to file
      ansible.builtin.template:
        src: inventory_template.j2
        dest: "{{ output_inventory_file }}"
        mode: 0644