ansible vars_files和extra_vars读取输入

时间:2018-10-30 01:29:30

标签: ansible

希望传递字典以根据位置读取一组键值对。将值硬编码到剧本时,它可以正常工作,但通过extra_vars调用会给出错误消息。即使它支持也不确定。感谢您的想法和投入。

ansible-playbook play3.yml -e'{“ var1”:“ loc2”}'-vv

play3.yml

---
- name: testing
  hosts: localhost
  connection: local
  gather_facts: no
  vars_files:
    -  var_file.yml
  tasks:
    - debug:
        msg: "{{ var1['first'] }}"

var_file.yml

---
loc1:
  first: name1
  last: name2
loc2:
  first: python
  last: perl
...

1 个答案:

答案 0 :(得分:0)

“动画片中一切皆有可能。” -Bugs Bunny

此playook:

---
- name: testing
  hosts: localhost
  connection: local
  gather_facts: no
  vars_files:
    - var_file.yml
  tasks:
    - debug:
        var: "{{ item }}.first"
      with_items: "{{ var1 }}"

给我这个输出:

TASK [debug] **********************************************************************************************************************************
task path: /home/jack/Ansible/CANES/PLAYBOOKS/play3.yml:9
ok: [localhost] => (item=None) => {
    "loc2.first": "python"
}