Passing a parameter to an Ansible dynamic resource

I use Ansible to configure some virtual machines. I wrote a Python script that retrieves hosts from a REST service.
My virtual machines are organized in Media. For example, I have Test, Red, and Integration environments, each of which has a subset of virtual machines.

This Python script requires the user parameter --environment <ENV> retrieve the hosts of the required environment.

The problem I encountered is passing the <ENV> command to the ansible-playbook command. Actually the following command does not work

 ansible-playbook thePlaybook.yml -i ./inventory/FromREST.py --environment Test 

I get an error message:

 Usage: ansible-playbook playbook.yml ansible-playbook: error: no such option: --environment 

What is the correct syntax for passing variables to a dynamic script resource?

Update:

To better explain, FromREST.py script takes the following parameters:

  • Parameter --list or parameter --host <HOST> in accordance with the Rules of dynamic inventory
  • The --environment <ENVIRONMENT> parameter, which I added to those that Ansible needs to manage different environments
+6
source share
2 answers

Workaround using $PPID to parse the -e / --extra-vars process snapshot.

 ansible-playbook -i inventory.sh deploy.yml -e cluster=cl_01 

inventory.sh file

 #!/bin/bash if [[ $1 != "--list" ]]; then exit 1; fi extra_var=`ps -f -p $PPID | grep ansible-playbook | grep -oh "\w*=\w*" | grep cluster | cut -f2 -d=` ./inventory.py --cluster $extra_var 

inventory.py returns JSON inventory for cl_01 cluster.

Not really I know, but it works.

+3
source

I had a similar problem, I did not find any solution, so I just changed my dynamic resource to use the OS Environment variable if the user did not pass --env

Grab env var in your inventory as shown below:

import os print os.environ['ENV']

Pass env var inaccessible

export ENV=dev ansible -i my_custom_inv.py all --list-host

+3
source

All Articles