Ansible循环列表var

时间:2016-08-30 14:37:36

标签: ubuntu-14.04 ansible ansible-playbook ansible-2.x

我正在尝试将属于群组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"}

任何人都知道更好的方法吗?

由于

1 个答案:

答案 0 :(得分:3)

{{ 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 }}"
相关问题