2017-09-14 5 views
2

Ich habe ein ansible Textbuch wie unten, möchte ich dieses verschachtelte Variable wie verwenden: msg={{{{Component}}.community_release_num}}, aber wenn ich Textbuch laufen:Wie kann ich Ansible verschachtelte Variable verwenden?

ansible-playbook vartest.yml -e 'version=version_402', it not work 

[[email protected] nested-var]$ tree 
. 
├── vars 
│   ├── horizon.yml 
│   └── version_402.yml 
└── vartest.yml 

1 directory, 3 files 
[[email protected] 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}} 

[[email protected] nested-var]$ cat vars/horizon.yml 
Component: horizon 

[[email protected] nested-var]$ cat vars/version_402.yml 
- horizon: 
    community_release_num: '9.0.1' 
    release_num: '4.0.2' 
[[email protected] nested-var]$ 

Fehlermeldungen

[[email protected] nested-var]$ ansible-playbook vartest.yml -e 'version=version_402' 
/usr/lib64/python2.6/site-packages/cryptography/__init__.py:25: DeprecationWarning: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. 
    DeprecationWarning 

PLAY [test] ******************************************************************************************************* 
/usr/lib64/python2.6/site-packages/Crypto/Util/number.py:57: PowmInsecureWarning: Not using mpz_powm_sec. You should rebuild using libgmp >= 5 to avoid timing attack vulnerability. 
    _warn("Not using mpz_powm_sec. You should rebuild using libgmp >= 5 to 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}}"} 
    to 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 

Kann ansible Verwendung variabler verschachtelt Wenn ja, wie benutzt man es?

Antwort

3

Per Ansible FAQ:

Eine weitere Regel ist ‚Schnurrbärte sind nicht stapelbar‘. Wir sehen oft so:

{{ somevar_{{other_var}} }} 

Die oben nicht funktioniert, wenn Sie eine dynamische Variable, die hostvars oder Vars Wörterbuch gegebenenfalls verwenden verwenden müssen:

{{ hostvars[inventory_hostname]['somevar_' + other_var] }} 

Also in Ihrem Fall:

- debug: msg={{hostvars[inventory_hostname][Component].community_release_num}} 

Oder

- debug: msg={{vars[Component].community_release_num}} 
+0

es funktioniert viel Dank – qianwang

Verwandte Themen