questions regarding inventories

hi guys,

quick question for you when you have time, based on the following scenario:

  • using a git project with an ansible inventory plugin (grazie ansible team!!!)
  • thus retrieving my inventory on awx from my scm project
  • assigning the yaml file as inventory file

one way to configure ansible in order to get it working:

  • setup an ansible.cfg located in the root dir of my project and filed with :

[defaults]
inventory = ${ANSIBLE_HOME}/inventories
inventory_plugins = ${ANSIBLE_HOME}/inventory_plugins

[inventory]
enable_plugins = myplugin, ini

  • create a yaml/python script according to existing inventory_plugins available on 2.4
  • fix an issue with 2.4 (https://github.com/ansible/ansible/pull/31605) until 2.4.1
  • declare environment variables tied with my inventory source configured in awx:
    ANSIBLE_HOME: “/var/lib/awx/projects/_22__myproject”
    ANSIBLE_CONFIG: “/var/lib/awx/projects/_22__myproject/ansible.cfg”

the questions are :

  • any way/suggestion/idea in order to avoid to declare those env vars in awx ? like having an environment variable pointing to the related project would greatly help ? seems like a feature to request ? or did i miss something ? enabled debug in order to catch the variable passed to inventory plugin but no luck afaik
  • just wondering what constraints led to change the pwd execution to the default inventory (e.d: ‘PWD’: ‘/usr/lib/python2.7/site-packages/awx/plugins/inventory’ )? probably because of the issue mentioned above ? once the fix in place, what should be the default pwd ?

thanks for your time
Franck

Hi Franck,

You have stumbled onto an issue of open development! We have been actively looking into how we might enable this for the purpose of standard cloud sources (inside the awx/plugins/inventory folder), and discussion is going on here.

https://github.com/ansible/awx/issues/171

To your question, I think we also want to allow for the use of plugins in SCM inventory sources, but I don’t think that any serious planning has yet happened for this. You summarize the issues fairly well. We need to set the list of plugin names to use for the particular inventory import, as well as set the path for the plugin search path. How we manage a directory for housing custom inventory plugins is a different issue. I wanted to look into whether some folder adjacent to the inventory file (similar to how the roles, or group_vars/host_vars folders work) might allow a user to put their plugins in source control, and your solution of putting ansible.cfg in the root of source control is something I might have to chew on a bit.

I’ve seen some other issues about the working directory in inventory imports. My expectation: The directory should be the directory where the inventory file (script, ini, yaml file, etc.) lives.

Please keep me in the loop if you establish a set of instructions to use inventory plugins in AWX, and I will see about reproducing them. When this feature development gets underway, I would like to have an approach that doesn’t involve overriding anything Ansible configuration parameters or environment variables. Ultimately, this is something a user should be able to set up in the UI + combined with source control. I would love for you to stay plugged into this process, and give us feedback about how well our solution fits your use case.

Alan

Hi Alan,

glad to hear your interesting feedback, thanks for it, keeping an eye on this issue as well.

regarding ansible.cfg, I need to be able to run my project’s playbooks either using ansible cli or awx. I could achieve the same result using environment vars I guess (environment vars have the highest priority if I’m not mistaken, thus might worth to keep them as last resort ?).

I like the high level of flexibility provided by ansible (e.d: ansible.cfg priority order). If we can keep it while not interfering with awx concepts. I’m not familiar enough with awx logic/mechanisms though.

Regarding the working directory in inventory imports, if i’ve understand correctly your expectation:

  • how would you deal with inventory scripts/config files ? for instance :

  • my inventory plugin python script is located in /scm_root/inventory_plugins/myplugin.py ,

  • while my yaml config file , used conjointly with my inventory_plugin is located in /scm_root/inventories/myplugin.yml.

which one would you select ?

my 2 cents, I would keep the idea of changing of directory only if required.

Actually I’m already using inventory_plugins feature if ansible 2.4, the way I mentionned it earlier.

Works pretty well actually. the only annoying thing is the use of those environment variables used for :

  • identifying the custom inventory_plugin location

  • declare and enable my inventory plugin , using enable_plugins option

declaring as env variable enable_plugins in a source inventory is fine for me, since the value is « static »

whereas inventory_plugin contains a dynamic value, the project name location which is subject to change (e.d delete/recreate a project will probably give me a new project id)

Thus, the idea of providing:

  • when an scm update from a source inventory is executed,

  • if the source inventory rely on a file from a project

an additional environment variable containing the project root folder location, imho

Let me know if something is unclear with my previous scenario explanation, feel free to contact me directly if I can help. Working actively on a project requiring awx thus, completely focused on playing and enjoying automation with awx/ansible :slight_smile:

and I agree with the approach of limiting our « footprint » , only when necessary

Again let me know how can I help, it will be a pleasure

Cheers

Franck

Franck,

I need to go back over this in detailed again, but let me just spitball an extremely simple proposal using your example setup:

  • we add a new field plugin_path to the inventory update
  • in the UI for the SCM inventory source, you specify “inventory_plugins/myplugin.py” for this field
  • in AWX task code, we set the plugin search path, and set the enabled plugins to just “myplugin”
    (all the rest about the inventory source stays the same, like source_path, which points to the inventory file to be read by the plugin)

I’m not 100% sure this syntax is how Ansible works, it’ll need testing. I just want to throw it out there, and think about if anything is missing from this proposal.

Best,
Alan

Franck,

I have a PR up that will perform a basic demo of what was talked about here.

https://github.com/ansible/awx/pull/480

This is for the purpose of using a custom inventory plugin that you’ve stored in the same source control as your inventory file.
I’ve tested this with a simple custom plugin that replaces the inventory contents with 1 host named “alan”, and it works.

The final version of this is sure to look at least a little different. We also need to convert the standard inventory cloud source types, and I’ll be poking at openstack first out of those.

Alan