Ansible - 在循环中将变量注册到item属性

时间:2015-09-08 18:46:53

标签: ansible ansible-playbook

我正在尝试将shell命令的输出注册到项目列表中的item属性。

这发生在循环期间,但似乎没有注册属性。运行任务后,该属性仍显示值none。我想知道我做错了什么?或者有办法实现这个目标吗?

变量:

users:
  - username: someguy
    description: "Some Guy"
    groups: ['sudo', 'guy']
    new_id: 6001
    old_uid:
    old_gid:
    user_exists:
    password: waffles
  - username: somedude
    description: "Some Dude"
    groups: ['dude']
    new_id: 6002
    old_uid:
    old_gid:
    user_exists:
    password: toast

任务

---

- name: Check if user exists
  shell: /usr/bin/getent passwd {{ item.username }} | /usr/bin/wc -l | tr -d ' '
  with_items: "{{ users }}"
  register: item.user_exists

- name: Check user current UID
  shell: /usr/bin/id -u {{ item.username }}
  with_items: "{{ users }}"
  register: item.old_uid
  when: item.user_exists == 1

- name: Check user current GID
  shell: /usr/bin/id -g {{ item.username }}
  with_items: "{{ users }}"
  register: item.old_gid
  when: item.user_exists == 1

输出

TASK: [users | Check if user exists] ****************************************** 
changed: [bserver] => (item={'username': 'someguy', 'password': 'waffles', 'description': 'Some Guy', 'new_id': 6001, 'groups': ['sudo', 'guy'], 'user_exists': None, 'old_uid': None, 'old_gid': None})
changed: [aserver] => (item={'username': 'someguy', 'password': 'waffles', 'description': 'Some Guy', 'new_id': 6001, 'groups': ['sudo', 'guy'], 'user_exists': None, 'old_uid': None, 'old_gid': None})
changed: [aserver] => (item={'username': 'somedude', 'password': 'toast', 'description': 'Some Dude', 'new_id': 6002, 'groups': ['dude'], 'user_exists': None, 'old_uid': None, 'old_gid': None})
changed: [bserver] => (item={'username': 'somedude', 'password': 'toast', 'description': 'Some Dude', 'new_id': 6002, 'groups': ['dude'], 'user_exists': None, 'old_uid': None, 'old_gid': None})

TASK: [users | Check user current UID] **************************************** 
skipping: [aserver] => (item={'username': 'someguy', 'password': 'waffles', 'description': 'Some Guy', 'new_id': 6001, 'groups': ['sudo', 'guy'], 'user_exists': None, 'old_uid': None, 'old_gid': None})
skipping: [aserver] => (item={'username': 'somedude', 'password': 'toast', 'description': 'Some Dude', 'new_id': 6002, 'groups': ['dude'], 'user_exists': None, 'old_uid': None, 'old_gid': None})
skipping: [bserver] => (item={'username': 'someguy', 'password': 'waffles', 'description': 'Some Guy', 'new_id': 6001, 'groups': ['sudo', 'guy'], 'user_exists': None, 'old_uid': None, 'old_gid': None})
skipping: [bserver] => (item={'username': 'somedude', 'password': 'toast', 'description': 'Some Dude', 'new_id': 6002, 'groups': ['dude'], 'user_exists': None, 'old_uid': None, 'old_gid': None})

TASK: [users | Check user current GID] **************************************** 
skipping: [aserver] => (item={'username': 'someguy', 'password': 'waffles', 'description': 'Some Guy', 'new_id': 6001, 'groups': ['sudo', 'guy'], 'user_exists': None, 'old_uid': None, 'old_gid': None})
skipping: [aserver] => (item={'username': 'somedude', 'password': 'toast', 'description': 'Some Dude', 'new_id': 6002, 'groups': ['dude'], 'user_exists': None, 'old_uid': None, 'old_gid': None})
skipping: [bserver] => (item={'username': 'someguy', 'password': 'waffles', 'description': 'Some Guy', 'new_id': 6001, 'groups': ['sudo', 'guy'], 'user_exists': None, 'old_uid': None, 'old_gid': None})
skipping: [bserver] => (item={'username': 'somedude', 'password': 'toast', 'description': 'Some Dude', 'new_id': 6002, 'groups': ['dude'], 'user_exists': None, 'old_uid': None, 'old_gid': None})

1 个答案:

答案 0 :(得分:1)

不幸的是,这不是它的工作原理。

您的行register: "{{item.user_exists}}"可能会导致在名为false的变量中注册的结果。

您无法将任务的结果注入任何对象。 register功能只会带一个字符串。

此外register有一个完整的different behavior in a loop。它不是单独注册每个迭代,而是注册一个单独的对象,该对象保存密钥results中所有项的结果。

您仍然可以遍历用户并检查是否存在。但我认为你不会想要成为这样的人。

首先将结果注册到一个变量中,users_checked

- name: Check if user exists
  shell: /usr/bin/getent passwd {{ item.username }} | /usr/bin/wc -l | tr -d ' '
  with_items: users
  register: users_checked

我建议您使用debug模块,以便始终检查您正在使用的数据结构。

- debug: var=users_checked

这将显示如下:

"var": {
    "users_checked": {
        "changed": true,
        "msg": "All items completed",
        "results": [
            {
                "changed": true,
                "cmd": "/usr/bin/getent passwd someguy | /usr/bin/wc -l | tr -d ' '",
                "delta": "0:00:00.005415",
                "end": "2015-09-08 20:59:33.379516",
                "invocation": {
                    "module_args": "/usr/bin/getent passwd someguy | /usr/bin/wc -l | tr -d ' '",
                    "module_name": "shell"
                },
                "item": {
                    "description": "Some Guy",
                    "groups": [
                        "sudo",
                        "guy"
                    ],
                    "new_id": 6001,
                    "old_gid": null,
                    "old_uid": null,
                    "password": "waffles",
                    "user_exists": null,
                    "username": "someguy"
                },
                "rc": 0,
                "start": "2015-09-08 20:59:33.374101",
                "stderr": "",
                "stdout": "0",
                "stdout_lines": [
                    "0"
                ],
                "warnings": []
            },
            {
                "changed": true,
                "cmd": "/usr/bin/getent passwd somedude | /usr/bin/wc -l | tr -d ' '",
                "delta": "0:00:00.006362",
                "end": "2015-09-08 20:59:33.530546",
                "invocation": {
                    "module_args": "/usr/bin/getent passwd somedude | /usr/bin/wc -l | tr -d ' '",
                    "module_name": "shell"
                },
                "item": {
                    "description": "Some Dude",
                    "groups": [
                        "dude"
                    ],
                    "new_id": 6002,
                    "old_gid": null,
                    "old_uid": null,
                    "password": "toast",
                    "user_exists": null,
                    "username": "somedude"
                },
                "rc": 0,
                "start": "2015-09-08 20:59:33.524184",
                "stderr": "",
                "stdout": "0",
                "stdout_lines": [
                    "0"
                ],
                "warnings": []
            }
        ]
    }
}

因此,结果不仅包含所有项目的实际结果,还包含输入对象。这使您可以循环users_checked.results而不是原始的users列表。

- name: Check user current UID
  shell: /usr/bin/id -u {{ item.item.username }}
  with_items: users_checked.results
  when: item.stdout == 1

但现在它会变得讨厌,因为你想再检查两件事。你可能可以继续这种方法,注册上面的结果,然后使用注册的数据结构作为下一个循环的输入,但你最终会得到一个深度嵌套的对象,最后在{{1}的某个地方有原始对象}。

为避免这种混乱,您可能需要查看creating a custom module来完成此任务。