Created
June 26, 2017 13:35
-
-
Save halberom/6f3712931f5ad1b001a739fb3ba48c20 to your computer and use it in GitHub Desktop.
ansible - example of generating complex fact using jinja, and passing to role
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: localhost | |
connection: local | |
gather_facts: no | |
vars: | |
foo: bar | |
cops_es_nginx_foo: bar | |
tasks: | |
- set_fact: | |
es_vhost: | | |
{% set nginx = {'v': {}} %} | |
{% set o = 'cops_es_nginx_' %} | |
{% set p = 'corpusops_nginx_' %} | |
{% for i, val in vars.items() %} | |
{% if i.startswith(o) %} | |
{% set _ = nginx.v.update({p+o.join(i.split(o)[1:]): val}) %} | |
{% endif %} | |
{% endfor %} | |
{{ nginx.v | to_json }} | |
- debug: | |
var: es_vhost | |
- include_role: | |
name: test | |
vars: | |
role_vhost: '{{ es_vhost }}' |
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
# roles/test/tasks/main.yml | |
--- | |
- debug: | |
var: role_vhost |
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
PLAY [localhost] *************************************************************************************************************************************************************************************************************************************************************** | |
TASK [set_fact] **************************************************************************************************************************************************************************************************************************************************************** | |
ok: [localhost] | |
TASK [debug] ******************************************************************************************************************************************************************************************************************************************************************* | |
ok: [localhost] => { | |
"es_vhost": { | |
"corpusops_nginx_foo": "bar" | |
} | |
} | |
TASK [test : debug] ************************************************************************************************************************************************************************************************************************************************************ | |
ok: [localhost] => { | |
"role_vhost": { | |
"corpusops_nginx_foo": "bar" | |
} | |
} | |
PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************* | |
localhost : ok=3 changed=0 unreachable=0 failed=0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment