/ansible_csv_inventory

Primary LanguagePythonGNU General Public License v3.0GPL-3.0

CSV/Ansible Dynamic Inventory script

ToC

Intro

This is a dynamic Inventory script for Ansible that turns an CSV File into an Inventory.

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

Requirements

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

  • Python3 - Python3 programming language

Configuration

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

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

[csv_inventory]
csv_inventory_file = ./example.csv
hostname_col = VM Name
group_by_col = Project

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

./csv_inventory.py --config --file example.csv --hostname-col "VM Name" --group-by-col "Project"

Example File

Included in this Repository is a Example CSV File, used to illustrate, and demonstrate in which ways csv_inventory.py can be used, it contains a Export generated by a third party tool, which can easily be used as an Inventory with some additional Columns added 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 i.E. choose to group your hosts by the Column Project,Datacenteror Environment.

The example file also contains two possible Columns that could be used for unique Hostnames, either VM Name or FQDN are unique.

Host variables

All Columns are automatically turned into Host Variables!

i.E. the Host Variables for the Host dev-iceflex-sql01, from the example.csv are:

{
    "Datacenter": "FRA-1",
    "Environment": "Development",
    "FQDN": "sql01.dev.iceflex.com",
    "Project": "iceflex",
    "VM_Name": "dev-iceflex-sql01",
    "ansible_connection": "",
    "ansible_host": "10.20.11.01",
    "ansible_ssh_user": "admin-user",
    "mysql_root_pass": "Affenzirkus123!"
}

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.

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

Usage

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:

ansible -i csv_inventory.py -m ping dev-zornfix-app01

Usage inside AWX or Ansible Tower

If you put your csv 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:

  • 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 csv_inventory.py
  • Save and update the inventory source

The inputs needed for the credential type:

fields:
  - 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:

env:
  CSV_INVENTORY_CONFIG: '{{tower.filename}}'
file:
  template: |-
    [csv_inventory]
    csv_inventory_file = {{ csv_file }}
    group_by_col = {{ group_by_col }}
    hostname_col = {{ hostname_col }}

License

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