Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
KeyboardInterrupt opened this issue Oct 5, 2018 · 2 comments
Labels
enhancement New feature or request hacktoberfest

Comments

@KeyboardInterrupt
Copy link
Owner

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.

@jesus-velez
Copy link

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

@KeyboardInterrupt
Copy link
Owner Author

KeyboardInterrupt commented Nov 14, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request hacktoberfest
Projects
None yet
Development

No branches or pull requests

2 participants