遇到了一些令人烦恼的问题,我就是无法解决。
这是我的代码。如果我完全注释掉 group 和 groups 变量,一切都会正常工作。但是,这会引发以下错误。
它基本上告诉我该组不存在。在这个例子中,它说消息:“组”‘全部’不存在。我不知道我需要做什么来解决这个问题。
- name: Add new group if it doesn't exist already
group:
name: "{{ group }}"
when: group is defined
- name: Add multiple users
user:
name: "{{ item.0 }}"
comment: "{{item.1 }}"
uid: "{{ item.2 }}"
group: "{{ group }}"
groups: "{{ groups }}"
append: yes
with_together:
- "{{ name }}"
- "{{ comment }}"
- "{{ uid }}"
- "{{ group }}"
And variable file:
name:
- test1
- test2
comment:
- "comment1"
- "comment2"
uid:
- 150
- 151
group: sudo
groups:
- admin
- test
However, now I am receiving this error.
failed: [127.0.0.1] => (item=[u'test1', u'comment1', 150, u'sudo']) => {"failed": true, "invocation": {"module_args": {"append": true, "comment": "comment1", "createhome": true, "expires": null, "force": false, "generate_ssh_key": null, "group": "sudo", "groups": "{'ungrouped': ['127.0.0.1'], 'all': ['127.0.0.1']}", "home": null, "login_class": null, "move_home": false, "name": "test1", "non_unique": false, "password": null, "remove": false, "shell": null, "skeleton": null, "ssh_key_bits": "2048", "ssh_key_comment": "ansible-generated on ubuntu-512mb-sfo1-01", "ssh_key_file": null, "ssh_key_passphrase": null, "ssh_key_type": "rsa", "state": "present", "system": false, "uid": "150", "update_password": "always"}, "module_name": "user"}, "item": ["test1", "comment1", 150, "sudo"], "msg": "Group 'all': ['127.0.0.1']} does not exist"}
failed: [127.0.0.1] => (item=[u'test2', u'comment2', 151, None]) => {"failed": true, "invocation": {"module_args": {"append": true, "comment": "comment2", "createhome": true, "expires": null, "force": false, "generate_ssh_key": null, "group": "sudo", "groups": "{'ungrouped': ['127.0.0.1'], 'all': ['127.0.0.1']}", "home": null, "login_class": null, "move_home": false, "name": "test2", "non_unique": false, "password": null, "remove": false, "shell": null, "skeleton": null, "ssh_key_bits": "2048", "ssh_key_comment": "ansible-generated on ubuntu-512mb-sfo1-01", "ssh_key_file": null, "ssh_key_passphrase": null, "ssh_key_type": "rsa", "state": "present", "system": false, "uid": "151", "update_password": "always"}, "module_name": "user"}, "item": ["test2", "comment2", 151, null], "msg": "Group 'all': ['127.0.0.1']} does not exist"}
答案1
问题是变量名冲突。groups
是保留变量,保存清单中的组。all
是自动生成的组,保存清单中的所有主机。
来自文档:
即使您没有自己定义它们,Ansible 也会自动为您提供一些变量。其中最重要的是
hostvars
、group_names
和groups
。用户不应自己使用这些名称,因为它们是保留的。environment
也是保留的。
和
groups
是清单中所有组(和主机)的列表。这可用于枚举组内的所有主机。
只需重命名变量,它就可以正常工作。通常,最好在角色的所有变量前加上角色名称。如果您使用第三方角色(例如来自 Ansible Galaxy 的角色),这一点就变得更加重要,只是为了避免冲突。因此,groups
您可以使用myrole_groups
,并且可以确信永远不会发生冲突。