-
-
Save darKoram/615d075d16762d848a13 to your computer and use it in GitHub Desktop.
This gist demonstrates how to pass a complex structure (dict) from one ansible playbook to another via dependency roles. Gist made me use filenames with '_' in place of '/'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
# Goal: Use the variables defined in caller/tasks/main.yml in these tasks | |
# This will fail with an error | |
# One or more undefined variables: 'unicode object' has no attribute 'a' | |
- debug: msg="{{complex['a']}}" | |
# The problem is that passing vars via role dependencies seems to serialize the structure | |
# The solution is to de-serialize and capture it into a new variable with some jinja filter magic | |
- debug: msg="{{complex | to_json | capture(complicated)}}" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
dependencies: | |
# Unlike python, you can't do complicated=complicated to pass the variable along. | |
# This will cause a circular reference error. | |
#- { role: called, complex: complicated } | |
# So we have to rename the variable and convert back in the called tasks file. | |
- { role: called, complex: complicated } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: all | |
vars: | |
- complicated: { 'a': 'b', 'c': [{'1':'2'}]} | |
tasks: | |
- debug: msg="{{complicated['a'] and also complicated.c }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imagine this: | |
``` | |
project-name/ | |
roles/ | |
caller/ | |
tasks/ | |
main.yml | |
meta/ | |
main.yml | |
called/ | |
tasks/ | |
main.yml | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment