Excel/Ansible Dynamic Inventory script



This is a dynamic Inventory script for Ansible that turns an Excel Spreadsheet into an Inventory.

This is helpful whenever you get an Excel export from another Tool, containing Information on server, which you need to quickly access via Ansible, without manually writing an Inventory file beforehand.


The following requirements must be installed to use this Inventory Script:

  • Python – Python programming language
  • OpenPyXL – A Python library to read/write Excel 2010 xlsx/xlsm files


Supported Excel file formats are:

  • .xlsx
  • .xlsm
  • .xltx
  • .xltm

Tested Ansible Version is: 2.6.2 (but it should be compatible with all Ansible version supporting Dynamic Inventory scripts)


To make use of xlsx_inventory.py you must first specify some configuration inside the xlsx_inventory.cfg File.

A Typical configuration file looks like this and is rather self explanatory:

xlsx_inventory_file = ./example.xlsx
sheet = Sheet1
hostname_col = A
group_by_col = B

If no such configuration file exists, you can easily create it by running:

./xlsx_inventory.py --config --file example.xlsx --hostname-col A --group-by-col B --sheet Sheet1

Example File

Included in this Repository is a Example Excel Spreadsheet, containing multiple sheets used to illustrate, and demonstrate in which ways xlsx_inventory.py can be used.

  • The first Sheet (Sheet1) contains a Export generated by a third party tool, which can easily be used as an Inventory
  • The second Sheet (Sheet2) contains an a Subset of (Sheet1) with additional Columns used to set/overwrite Ansible and custom variables.

Grouping and Hostnames

Servers can be grouped by any Column, i.E. with the included example file you could choose to group your hosts by the Column B (Project) or D (Environment) :

The example file also contains two possible Columns that could be used for unique Hostnames, either A (VM Name) or E (DNS Name) would be a good choices, depending on your DNS Settings/Environment.

Host variables

All Columns are automatically turned into Host Variables!

i.E. the Host Variables for the Host stg-zornfix-sql01 (row 35 in Sheet1) are:

    "datacenter": "FRA-2",
    "dns_name": "sql01.stg.zornfix.uk",
    "environment": "Staging",
    "project": "zornfix",
    "vm_name": "stg-zornfix-sql01"

You can use this to either set/overwrite ansible variables like ansible_host and ansible_ssh_user or to set/overwrite custom variables like mysql_root_pass (example.xlsx Sheet2)

    "ansible_host": "",
    "ansible_ssh_user": "mysql-admin",
    "datacenter": "FRA-1",
    "dns_name": "sql01.dev.zornfix.uk",
    "environment": "Development",
    "mysql_root_pass": "Implosionsstrudel123!",
    "project": "zornfix",
    "vm_name": "dev-zornfix-sql01"

To check which host variables are set by the inventory/xlsx file, you can run xlsx_inventory.py --host <HOSTNAME>


Once configured the Inventory script can be used like any other Dynamic Inventory by specifying it as the inventory in your ansible/ansible-playbook commands:

./xlsx_inventory.py --config --file example.xlsx --group-by-col B --hostname-col A --sheet Sheet2
ansible -i xlsx_inventory.py -m ping dev-zornfix-app01

Usage inside AWX or Ansible Tower

If you put your xlsx file inside of the same git repository as this script, then you can import the inventory contents via an SCM-based inventory source into an AWX or Ansible Tower server.

Step by step instructions, must be done by a superuser:

  • Inside the Ansible virtualenv, install needed packages
    • pip install openpyxl
    • pip install configparser
  • Create a credential type that defines the inputs and injectors below (must be superuser)
  • Create a credential, providing any values unique to your project
  • Create an inventory
  • Create an inventory source
    • source from project
    • apply the credential from earlier
    • select the inventory file xlsx_inventory.py
  • Save and update the inventory source

The inputs needed for the credential type:

  - type: string
    id: excel_file
    label: Location of Excel file
  - type: string
    id: group_by_col
    label: Column letter to group hosts by
  - type: string
    id: hostname_col
    label: Column that contains hostnames
  - type: string
    id: sheet
    label: Sheet in Excel to use

Injectors needed for the credential type:

  EXCEL_INVENTORY_CONFIG: '{{tower.filename}}'
  template: |-
    xlsx_inventory_file = {{excel_file | default('example.xlsx')}}
    group_by_col = {{group_by_col | default('B')}}
    hostname_col = {{ hostname_col | default('A') }}
    sheet = {{ sheet | default('Sheet1') }}


This project is licensed under the GPL3 License – see the LICENSE.md file for details


View Github