KeyboardInterrupt/ansible_xlsx_inventory

Add a README.md Section explaining how to convert xlsx Files into regular Inventorys.

Opened this issue · 2 comments

I would like to point out in the README.MD That you don't have to live with an SpreadSheet as your Inventory! And that I absolutely don't encourage you to do so XD.
To clarify this, I would like to add a Section that explains how to use the [ansible-inventory tool/command)(https://docs.ansible.com/ansible/latest/cli/ansible-inventory.html) to convert into a regular Inventory file.

Hello! Is there an option to create a regular inventory? I am new to ansible and looking for starting point.

There is no such Option in the Inventory itself.
What you can do is use the ansible-inventory command, that comes with Ansible, to generate either a --yaml, --toml or --json version of your Excel inventory.

i.E. this:

ansible-inventory -i xlsx_inventory.py --list --yaml --output output.yml

Will generate a yaml Inventory File

Sadly the ansible-inventory comand does not support exporting into the "ini style" hosts file format.

But you can basically generate whatever format you want with some "jinja" magic and a Playbook that looks like this:

---
- hosts: localhost
  gather_facts: False
  vars:
    ini_inventory_output_default: ./ini_inventory_hosts
  vars_prompt:
    - name: "ini_inventory_output"
      prompt: "output path of ini sytle inventory"
      default: "{{ ini_inventory_output_default }}"
      private: False
  tasks:
    - name: inventory file
      local_action:
        module: copy
        content: |
          {% for group in groups %}
          {% if group != 'all' %}
          [{{ group }}]
          {% for host in groups[group] %} {{ host }}
          {% endfor %}
          {% endif %}
          {% endfor %}
        dest: "{{ ini_inventory_output }}"
      run_once: True

You would then run this ansible-playbook -i xlsx_inventory.py ini_inventory.yml
You can expand the Jinja Template to include whatever variable is set via the xlsx_inventory.

If you attach an anonymized sample of your Excel Sheet and an Example of what the Hosts file should look like, I can help you to come up with a Playbook/Template that suits your needs :)

Greetings