patternMinor
Ansible - merging inventories to share variables across?
Viewed 0 times
acrossinventoriesmergingvariablesshareansible
Problem
Is it possible to merge inventories so I can share variables across them? Let's say I have two inventory files. Inventory A and Inventory B. They are both in a directory called inventories.
Variables are defined in B and I want to reference them in inventory A. Is this possible? I've heard of merging inventories so share variables across but I haven't gotten this to work.
Here's are the files in my inventories directory:
/opt/ansible/inventories/aws_ec2.yaml
/opt/ansible/inventories/secrets.yaml
contents of
Here is my
When I run my playbook, referencing both inventories, inventory does not seem to occur and I get these errors. It looks like the variables aren't being referenced and expanded?:
```
me@workstation:/opt/ansible/inventories$ ansible-playbook -i /opt/ansible/inventories/aws_ec2.yaml -i /opt/ansible/inventories/secrets.yaml /opt/ansible/playbooks/test.yaml
[WARNING]: * Failed to parse /opt/ansible/inventories/aws_ec2.yaml with aws_ec2 plugin: Failed to
describe instances: An error occurred (AuthFailure) when calling the DescribeInstances operation:
Credential must have exactly 5 slash-delimited elements, e.g. keyid/date/region/service/term, got '{{'
[WARNING]: Unable to parse /opt/ansible/inventories/aws_ec2.yaml as an inventory source
[WARNING]: Unable to parse /opt/ansible/inventories/secrets.yaml as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the imp
Variables are defined in B and I want to reference them in inventory A. Is this possible? I've heard of merging inventories so share variables across but I haven't gotten this to work.
Here's are the files in my inventories directory:
/opt/ansible/inventories/aws_ec2.yaml
/opt/ansible/inventories/secrets.yaml
contents of
aws_ec2.yaml:---
plugin: aws_ec2
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{aws_secret_key}} "
aws_security_token: "{{aws_security_token}}"
regions:
- us-west-2
keyed_groups:
- key: tags
prefix: tag
- key: placement.region
prefix: aws_region
groups:
TEST: "'prod' in (tags:list)"
compose:
ansible_host: private_ip_addressHere is my
secrets.yaml file:---
aws_access_key: xxxxx
aws_secret_key: xxxxx
aws_security_token: xxxxxWhen I run my playbook, referencing both inventories, inventory does not seem to occur and I get these errors. It looks like the variables aren't being referenced and expanded?:
```
me@workstation:/opt/ansible/inventories$ ansible-playbook -i /opt/ansible/inventories/aws_ec2.yaml -i /opt/ansible/inventories/secrets.yaml /opt/ansible/playbooks/test.yaml
[WARNING]: * Failed to parse /opt/ansible/inventories/aws_ec2.yaml with aws_ec2 plugin: Failed to
describe instances: An error occurred (AuthFailure) when calling the DescribeInstances operation:
Credential must have exactly 5 slash-delimited elements, e.g. keyid/date/region/service/term, got '{{'
[WARNING]: Unable to parse /opt/ansible/inventories/aws_ec2.yaml as an inventory source
[WARNING]: Unable to parse /opt/ansible/inventories/secrets.yaml as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the imp
Solution
While I haven't worked with
The order of inventory files matter; the files given first will be read first; the ones read last will take precedence. In your execution, you're referencing
You're trying to define a variable based on another variable with the same name. That's asking for trouble; rename the reference variables, as that will help you debugging.
When defining variables for the inventory, it's generally better to not do that directly on the inventory itself. See the link below on how to do that:
https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#organizing-host-and-group-variables
Finally,
Again, I never used that inventory plugin. It may be the case that the inventory plugin does not allow for variables at that point.
plugin: aws_ec2, here's some general Ansible information that might help you:The order of inventory files matter; the files given first will be read first; the ones read last will take precedence. In your execution, you're referencing
secrets.yaml last, so when aws_ec2.yaml is read, the secrets are still undefined.You're trying to define a variable based on another variable with the same name. That's asking for trouble; rename the reference variables, as that will help you debugging.
When defining variables for the inventory, it's generally better to not do that directly on the inventory itself. See the link below on how to do that:
https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#organizing-host-and-group-variables
Finally,
keyed_groups, regions and all these other keywords are specific to aws_ec2, and they'd work because you have plugin: aws_ec2 at the top of the first file. You have no such definition on secrets.yaml, so Ansible would probably treat it as a normal inventory file, where that structure would not be accepted. Your approach here is probably wrong, then.Again, I never used that inventory plugin. It may be the case that the inventory plugin does not allow for variables at that point.
Context
StackExchange DevOps Q#12629, answer score: 1
Revisions (0)
No revisions yet.