Skip to main content
better tags and formatting
Source Link
chicks
  • 1.9k
  • 1
  • 12
  • 29

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 aws_ec2.yamlaws_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_address

Here is my secrets.yamlsecrets.yaml file:

---
  
aws_access_key: xxxxx
aws_secret_key: xxxxx
aws_security_token: xxxxx

when iWhen 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 implicit localhost
does not match 'all'

Or is my sytaxsyntax for variable assigning and referencing wrong? Am I on the right path here? my /etc/ansible/ansible.cfg/etc/ansible/ansible.cfg file, under [defaults][defaults] is as such:

[defaults] inventory = /opt/ansible/inventory/aws_ec2.yaml

[defaults]
inventory = /opt/ansible/inventory/aws_ec2.yaml

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 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_address

Here is my secrets.yaml file:

---
  
aws_access_key: xxxxx
aws_secret_key: xxxxx
aws_security_token: xxxxx

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 implicit localhost
does not match 'all'

Or is my sytax for variable assigning and referencing wrong? Am I on the right path here? my /etc/ansible/ansible.cfg file, under [defaults] is as such:

[defaults] inventory = /opt/ansible/inventory/aws_ec2.yaml

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 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_address

Here is my secrets.yaml file:

---
  
aws_access_key: xxxxx
aws_secret_key: xxxxx
aws_security_token: xxxxx

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 implicit localhost
does not match 'all'

Or is my syntax for variable assigning and referencing wrong? Am I on the right path here? my /etc/ansible/ansible.cfg file, under [defaults] is as such:

[defaults]
inventory = /opt/ansible/inventory/aws_ec2.yaml
Source Link

Ansible - merging inventories to share variables across?

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 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_address

Here is my secrets.yaml file:

---
  
aws_access_key: xxxxx
aws_secret_key: xxxxx
aws_security_token: xxxxx

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 implicit localhost
does not match 'all'

Or is my sytax for variable assigning and referencing wrong? Am I on the right path here? my /etc/ansible/ansible.cfg file, under [defaults] is as such:

[defaults] inventory = /opt/ansible/inventory/aws_ec2.yaml