【问题标题】:ansible iterate over loopansible迭代循环
【发布时间】:2017-06-25 04:24:57
【问题描述】:

在 ansible 中迭代集合时出了什么问题?

执行ansible-playbook -i "localhost," -c local main.yml的时候报错

- name: echo kerberos
        shell: echo "addprinc -pw {{ item.password }} {{ item.username }}"
             ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

但对我来说,我似乎已经正确地遵守了这些规则。

这是我的最小示例:

main.yml

---
- hosts: all
  tasks:
    - name: echo kerberos
        shell: echo "addprinc -pw {{ item.password }} {{ item.username }}"
        with_items: "{{ users }}"

users.yml

---
users:
  - username: test_user
    password: test_user
    sn: User
    uid: 50001
    gid: 100
  - username: test_user1
    password: test_user
    cn: Test User1
    sn: User1
    uid: 50002
    gid: 100

user_groups:
  - cn: access1
    gid: 100001
    users:
      - test_user1

【问题讨论】:

    标签: ansible yaml ansible-2.x


    【解决方案1】:

    你应该小心 YAML 中的填充:

    ---
    - hosts: all
      tasks:
        - name: echo kerberos
          shell: echo "addprinc -pw {{ item.password }} {{ item.username }}"
          with_items: "{{ users }}"
    

    shellwith_itemsname 对齐。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 2017-06-10
      • 2013-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多