运行此ansible播放时出错“ERROR!'unicode object'hasno属性'comment'

时间:2016-02-18 22:39:56

标签: ansible ansible-playbook

这是我的剧本

 - name: Add multiple users
   user:
    name: "{{ item[0].name }}"
    comment: "{{ item[0].comment }}"
    uid: "{{ item[0].uid }}"
    groups: "{{ item[0].groups}}"
    shell: /bin/bash
   with_nested:
     - "{{ name }}"
     - "{{ comment }}"
     - "{{ uid }}"
     - "{{ groups }}"

这是我的vars文件

---
name:
 - test1
 - test2

comment:
 - "comment1"
 - "comment2"

uid:
 - 150
 - 151

groups: "sudo, admin"

我不确定是什么导致这个,任何想法?我相信我可能需要使用子元素而不是嵌套?我在正确的轨道吗?

更新:

更改了我的代码,但现在遇到以下情况。更新了代码和错误消息

 - 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 个答案:

答案 0 :(得分:1)

问题是变量名称冲突。 groups是保留变量,用于保存清单中的组。 all是一个自动生成的组,其中包含您广告资源的所有主机。

From the docs

  

即使您没有自己定义它们,Ansible也会自动为您提供一些变量。其中最重要的是hostvarsgroup_namesgroups。用户不应使用这些名称,因为它们是保留的。 environment也被保留。

  

groups是清单中所有组(和主机)的列表。这可用于枚举组内的所有主机。

只需重命名变量即可。通常,使用角色名称为角色的所有变量添加前缀是个好主意。如果您使用第三方角色,这会变得更加重要,例如:来自Ansible Galaxy,只是为了避免冲突。因此,您可以使用groups代替myrole_groups,而且可以确定永远不会发生冲突。