How to import inventory files from Ansible

,

Hi,

I’ve been using Ansible for a couple of years now and have an inventory file with around 100 VMs in it which I manage.

I have just started to look at AWX so that I can add users and delegate some of my playbooks to other folks in the Company.

How can I import a static Ansible inventory file into AWX? I had a quick look at the Inventory pages and can see how to add hosts manually one at a time, and I think there’s a way to run inventory scripts to pull the details in from AWS etc, but all I want to do is paste in the 100 lines of inventory from my Ansible hosts file.

Can anyone tell me how to do this? I am using AWX v1.0.2 which I built from this repo: https://copr.fedorainfracloud.org/coprs/mrmeee/awx/

Regards,

GXW

Importing inventories can be done through custom inventory script. I have seen a webinar from https://www.ansible.com/resources/videos where the presenter importing inventories through a python script file which reads up the local inventory and import it

Hi

Has versioning or the way you are tagging version changed after 1.0.8??

mj@falcon: /Download$ git clone [ https://github.com/ansible/awx.git | https://github.com/ansible/awx.git ]
Cloning into 'awx'...
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 179555 (delta 0), reused 1 (delta 0), pack-reused 179548
Receiving objects: 100% (179555/179555), 201.52 MiB | 6.75 MiB/s, done.
mj@falcon: /Download/awx$ git describe --long
1.0.8-14-g8a66213db
mj@falcon:~/Download/awx$ git describe --long --first-parent
1.0.7-9-g8a66213db

When building the lastest source, the resulting tar.gz is named awx-1.0.7.9.tar.gz

-bash-4.2$ git describe --long
1.0.8-14-g8a66213db
-bash-4.2$ ls -la dist/
total 10652
drwxr-xr-x. 2 root root 32 Sep 26 11:35 .
drwxr-xr-x. 14 awx awx 4096 Sep 26 11:35 ..
-rw-r--r--. 1 root root 10900105 Sep 26 11:35 awx-1.0.7.9.tar.gz

In the makefile this is used:

VERSION=$(shell git describe --long --first-parent)
VERSION3=$(shell git describe --long --first-parent | sed 's/-g. //')
VERSION3DOT=$(shell git describe --long --first-parent | sed 's/-g. //' | sed 's/-/./')
RELEASE_VERSION=$(shell git describe --long --first-parent | sed 's@([0-9.]{1,}).*@\1@')

Which results in the 1.0.7.9 version tag..

/Martin

Alternatively to a custom script you can create an inventory that references a file in a project. So add the project that contains the file and then create a new inventory, pick the project+file in the settings by adding it as an inventory source.

You don’t have to write a script for this.

Hi Matt - sounded like a good plan but when I tried that I just got this error:

ERROR!
source_project: Cannot use manual project for SCM-based inventory.

Any ideas what I might be doing wrong?
P.S. I have re-installed as v1.0.8 with docker now.

Hey GXW,

Exactly what it says - AWX doesn’t support SCM-based inventory when using manually synced projects (it only works for projects that come from source control, like git or svn).

There’s an issue in the queue that asks for specifically this - SCM inventory that imports from a manual project.

I have hesitations about that. Firstly, in that case, calling it SCM inventory is not correct. At that point, it’s file-based inventory, not SCM based. We already have a feature for file-based inventory. You use it by running the awx-manage CLI command to import inventory. If the server allowed SCM inventory from manual projects, then it would be doing the same management command, just through the API. If you’re using manual projects, then you’re doing things outside of the standard API interface already. In terms of implementation, there are cases where we use the SCM revision for certain things related to SCM inventory. That’s not a reason we couldn’t do this, but it would require special consideration in several cases (because manual projects don’t have a revision per se), and I think the drawbacks may outweigh the benefits.

Instead, it’s probably better to point to the CLI use of the inventory import command, and we’ll address any issues that come up with that.

https://docs.ansible.com/ansible-tower/latest/html/administration/tipsandtricks.html

Section 25.18
You just need to make sure you have the correct directory structure and then run awx-manage inside the task container and it will import everything.

Here’s the steps to upload the static inventory to Ansible AWX/ tower .

If you are using docker to run the AWX then for using awx-manage command you have to connect the awx web container " docker exec -it awx_web /bin/bash " and then follow the below commands .

  1. Login to Ansible Tower’s host or AWX’s task container in interactive mode (Linux Host). Execute the following command to check the “awx-manage” version

[root@awx awx]# awx-manage --version
1.0.8.0
[root@awx awx]#

  1. Create a file with the list of hosts which needs to be added into Ansible Tower/AWX’s inventory.

[root@awx awx]# cat hosts_add
192.168.3.152
192.168.3.153
192.168.3.154
[root@awx awx]#

  1. Once the list is ready, identify the inventory name in which you need to add the hosts from AWX/Tower’s GUI. (Refer Step 5). In the example, I will be adding the prepared hosts into “Linux_UA_Hosts” inventory.

[root@awx awx]# awx-manage inventory_import --inventory-name Linux_UA_Hosts --source hosts_add

Cheers
Sanjeev Rana

another way (and the one I personally favor) is to add inventory from a
project (as in: inventory file from a git project).

Roughly steps to go through:

1. create a Git (or any supported version control system) repo that
contains your inventory (Gitlab, Github, Subversion...whatever
2. add that repo as a project to AWX. Sync it.
3. create new inventory
3a. go into sources
3b. create new source that points to project from (2)/inventory_file
you've got
4. profit

This approach does not import variables though, which is really strange.

Hello Thanks,

Wich format and extension for the inventory file?

Host1 ansible_host=1.2.3.5
host2 ansible_host=5.6.7.8

Thanks a lot.