How can I use Ansible nested variable? -
i have ansible playbook 1 below, want use nested variable this: msg={{{{component}}.community_release_num}}
, when run playbook:
ansible-playbook vartest.yml -e 'version=version_402', not work [es@vpn-server nested-var]$ tree . ├── vars │ ├── horizon.yml │ └── version_402.yml └── vartest.yml 1 directory, 3 files [es@vpn-server nested-var]$ cat vartest.yml --- - name: test hosts: localhost vars_files: - vars/{{version}}.yml tasks: - debug: msg={{{{component}}.community_release_num}} - debug: msg={{{{component}}.release_num}} [es@vpn-server nested-var]$ cat vars/horizon.yml component: horizon [es@vpn-server nested-var]$ cat vars/version_402.yml - horizon: community_release_num: '9.0.1' release_num: '4.0.2' [es@vpn-server nested-var]$
error messages
[es@vpn-server nested-var]$ ansible-playbook vartest.yml -e 'version=version_402' /usr/lib64/python2.6/site-packages/cryptography/__init__.py:25: deprecationwarning: python 2.6 no longer supported python core team, please upgrade python. deprecationwarning play [test] ******************************************************************************************************* /usr/lib64/python2.6/site-packages/crypto/util/number.py:57: powminsecurewarning: not using mpz_powm_sec. should rebuild using libgmp >= 5 avoid timing attack vulnerability. _warn("not using mpz_powm_sec. should rebuild using libgmp >= 5 avoid timing attack vulnerability.", powminsecurewarning) task [debug] ****************************************************************************************************** fatal: [localhost]: failed! => {"failed": true, "msg": "template error while templating string: expected token 'colon', got '}'. string: {{{{component}}.community_release_num}}"} retry, use: --limit @/data/wangqian/artemis-code-test/artemis/ansible/update/nested-var/vartest.retry play recap ******************************************************************************************************** localhost : ok=0 changed=0 unreachable=0 failed=1
can ansible use nested variable, if yes, how use it?
per ansible faq:
another rule ‘moustaches don’t stack’. see this:
{{ somevar_{{other_var}} }}
the above not work, if need use dynamic variable use hostvars or vars dictionary appropriate:
{{ hostvars[inventory_hostname]['somevar_' + other_var] }}
so in case:
- debug: msg={{hostvars[inventory_hostname][component].community_release_num}}
or
- debug: msg={{vars[component].community_release_num}}
Comments
Post a Comment