Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/KeyboardInterrupt/ansible_xlsx_inventory

Turn any Excel Sheet into an Ansible Inventory
https://github.com/KeyboardInterrupt/ansible_xlsx_inventory

ansible dynamic-inventory hacktoberfest

Last synced: about 2 months ago
JSON representation

Turn any Excel Sheet into an Ansible Inventory

Lists

README

        

# Excel/Ansible Dynamic Inventory script

## ToC

- [Intro](#intro)
- [Compatibility](#compatibility)
- [Configuration](#configuration)
- [Example File](#example-file)
- [Grouping](#grouping)
- [Hosts variables](#host-variables)
- [Usage](#usage)
- [Licens](#license)

## Intro

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.

## Requirements

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

- [Python](https://www.python.org/) - Python programming language
- [OpenPyXL](https://openpyxl.readthedocs.io/en/stable/) - A Python library to read/write Excel 2010 xlsx/xlsm files

## Compatibility

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)

## Configuration

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:

```ini
[xlsx_inventory]
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:

```bash
./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:

```json
{
"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__)

```json
{
"ansible_host": "10.13.37.01",
"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 `

## Usage

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

```bash
./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:

```yaml
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:

```yaml
env:
EXCEL_INVENTORY_CONFIG: '{{tower.filename}}'
file:
template: |-
[xlsx_inventory]
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') }}
```

## License

This project is licensed under the GPL3 License - see the [LICENSE.md](/LICENSE.md) file for details