列表变量的 Ansible 循环
Ansible loop over list var
我正在尝试将属于组 abc
的用户添加到组 xyz
中。要在 ansible 中执行此操作,我正在执行
- name: Get the list of user belongs to abc group
command: lid -g -n abc
register: abc_users
- name: Add user belongs to abc group to xyz group
user: name={{ items }} groups=xyz append=yes
with_items: "{{ abc_users.stdout }}"
但是出现以下错误
fatal: [10.8.17.14]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'items' is undefined\n\nThe error appears to have been in '/home/#/workspace/#/ansible/roles/ubuntu-common/tasks/main.yml': line 26, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Add user belongs to sentience group to docker group\n ^ here\n"}
有人知道更好的方法吗?
谢谢
将{{ items }}
替换为{{ item }}
。
更新:正如@udondan 指出的那样,遍历 stdout_lines
- name: Add user belongs to abc group to xyz group
user: name={{ item }} groups=xyz append=yes
with_items: "{{ abc_users.stdout_lines }}"
我正在尝试将属于组 abc
的用户添加到组 xyz
中。要在 ansible 中执行此操作,我正在执行
- name: Get the list of user belongs to abc group
command: lid -g -n abc
register: abc_users
- name: Add user belongs to abc group to xyz group
user: name={{ items }} groups=xyz append=yes
with_items: "{{ abc_users.stdout }}"
但是出现以下错误
fatal: [10.8.17.14]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'items' is undefined\n\nThe error appears to have been in '/home/#/workspace/#/ansible/roles/ubuntu-common/tasks/main.yml': line 26, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Add user belongs to sentience group to docker group\n ^ here\n"}
有人知道更好的方法吗?
谢谢
将{{ items }}
替换为{{ item }}
。
更新:正如@udondan 指出的那样,遍历 stdout_lines
- name: Add user belongs to abc group to xyz group
user: name={{ item }} groups=xyz append=yes
with_items: "{{ abc_users.stdout_lines }}"