【问题标题】:Can't use dictionaries in ansible不能在ansible中使用字典
【发布时间】:2016-08-19 06:29:22
【问题描述】:

我尝试在 ansible 中使用字典。

我的tasks/main.yml

- name: Create Repository Folders
  file: path=/srv/svn/{{ ansible_fqdn }}/{{ item.value.reponame }} state=directory mode=0755 owner=apache group=apache
  with_dict:
    - repos

还有我的vars/main.yml

---
repos:
  repo1:
    reponame: repository1
    repogroup: group1
  repo2:
    reponame: repository2
    repogroup: group2
  repo3:
    reponame: repository3
    repogroup: group3

但是在运行 ansible-playbook 时出现以下错误:

TASK [svn : Create Repository Folders] *****************************************
fatal: [sun.beach.lan]: FAILED! => {"failed": true, "msg": "with_dict expects a dict"}

我在这里按照“循环哈希”的说明进行操作:http://docs.ansible.com/ansible/playbooks_loops.html

我想我没有正确的 YAML 语法,但我的想法已经不多了。

【问题讨论】:

    标签: dictionary yaml ansible


    【解决方案1】:

    只需将repos 前面的- 去掉with_dict 后面的with_dict

    - name: Create Repository Folders
      file: path=/srv/svn/{{ ansible_fqdn }}/{{ item.value.reponame }} state=directory mode=0755 owner=apache group=apache
      with_dict:
        repos
    

    如果您使用的是 ansible 2.0+,请像这样使用它:

    with_dict: "{{ repos }}"
    

    希望这会对你有所帮助。

    【讨论】:

    • 谢谢...你能解释一下为什么 ansible 2.0+ 会这样工作吗?
    • ansible 2.0+ 不推荐使用裸变量
    猜你喜欢
    • 2022-11-22
    • 2021-09-12
    • 2010-11-22
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 2021-11-16
    • 2021-01-31
    • 2017-05-25
    相关资源
    最近更新 更多