Skip to content

Instantly share code, notes, and snippets.

@eon01
Forked from enisozgen/ansible-nested-variable.yml
Created December 20, 2019 19:15
Show Gist options
  • Save eon01/0fb25898f727c4cb07a2aa19aad50275 to your computer and use it in GitHub Desktop.
Save eon01/0fb25898f727c4cb07a2aa19aad50275 to your computer and use it in GitHub Desktop.
Ansible example which shows how to reach nested variable with dynamic elements
# Example which shows how to reach nested ansible variable which is partially different.
# Run that plabook with ansible-playbook -e "env=test" ansible-nested-variable.yml
---
#
- hosts: localhost
connection : ssh
gather_facts: no
vars:
cidr_blocks:
vpc_production_cidr_block: "10.10.0.0/28"
vpc_infra_cidr_block: "10.20.0.0/28"
vpc_test_cidr_block: "10.30.0.0/28"
tasks:
- name: Show all CIDRs
debug:
msg: "{{ cidr_blocks}}"
- name: Show spesific CIDRs
debug:
msg: "{{ cidr_blocks['vpc_%s_cidr_block' | format(env)] }}" # Be careful about position of square brackets.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment