Skip to content

Instantly share code, notes, and snippets.

@darKoram
Last active December 25, 2015 13:19
Show Gist options
  • Save darKoram/615d075d16762d848a13 to your computer and use it in GitHub Desktop.
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 '/'
---
# 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)}}"
---
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 }
---
- hosts: all
vars:
- complicated: { 'a': 'b', 'c': [{'1':'2'}]}
tasks:
- debug: msg="{{complicated['a'] and also complicated.c }}"
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