【问题标题】:How can I use Ansible nested variable?如何使用 Ansible 嵌套变量?
【发布时间】:2020-12-30 04:26:11
【问题描述】:

我有一个像下面这样的 Ansible 剧本,我想使用这样的嵌套变量: msg={{{{Component}}.community_release_num}},但是当我运行剧本时:

ansible-playbook vartest.yml -e 'version=version_402', it 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]$

错误信息

[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 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  

Ansible可以使用嵌套变量吗,如果可以,如何使用?

【问题讨论】:

    标签: ansible jinja2


    【解决方案1】:

    Ansible FAQ:

    另一条规则是“胡须不堆叠”。我们经常看到这样的:

    {{ somevar_{{other_var}} }} 
    

    上述方法不起作用,如果您需要使用动态变量,请使用 hostvars 或 vars 字典(视情况而定):

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

    所以在你的情况下:

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

    或者:

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

    或者(从 Ansible 2.5 开始):

    - debug: msg={{(lookup('vars', Component)).community_release_num}}
    

    【讨论】:

    • 如何做到这一点。 region: 1 dev1_ips: 1,2 {{ dev[region]_ips }}
    • @SNR {{ lookup('vars', 'dev' ~ region ~ '_ips') }}
    • @SNR 另请注意~。在您的示例中,您给出了数字。如果是region: "1"+ 会起作用。否则"Unexpected templating type error occurred on ({{ lookup('vars', 'dev' + region + '_ips') }}): can only concatenate str (not \"int\") to str"。但是~ 在这两种情况下都有效。详情请见jinja.palletsprojects.com/en/3.0.x/templates/#other-operators
    • 明白谢谢@JasmineHegman
    【解决方案2】:

    运行此命令:
    ansible-playbook 1_lambda-facts.yml -e "func_name=mylambdaFunctionName"

    
    
    
    - name: get lamda Info
       hosts: localhost
       connection: local
       become: yes
       gather_facts: true
       tasks:
       - name: List all for a specific function
         lambda_facts:
           query: all
           function_name: "{{func_name}}"
        
       - name: debug info
         debug:
           msg: "{{lambda_facts}}"
           msg: "variable name is: {{vars['func_name']}}"
           msg: "{{lambda_facts['function'][vars['func_name']]['function_arn']}}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      • 2015-08-10
      • 2021-03-05
      • 1970-01-01
      • 2023-01-10
      相关资源
      最近更新 更多