Ansible-将变量注册到循环中的项属性
我正在尝试将shell命令的输出注册到项列表中的项属性 这在循环期间发生,但似乎没有注册属性。运行任务后,属性仍显示值none。我在想我是不是做错了什么?还是有办法做到这一点 变量:Ansible-将变量注册到循环中的项属性,ansible,ansible-playbook,Ansible,Ansible Playbook,我正在尝试将shell命令的输出注册到项列表中的项属性 这在循环期间发生,但似乎没有注册属性。运行任务后,属性仍显示值none。我在想我是不是做错了什么?还是有办法做到这一点 变量: users: - username: someguy description: "Some Guy" groups: ['sudo', 'guy'] new_id: 6001 old_uid: old_gid: user_exists: password:
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})
不幸的是,它不是这样工作的 您的行
register:“{{item.user_exists}}”
可能会导致在名为false
的变量中注册结果
不能将任务的结果注入任何对象。寄存器
功能将只接受一个字符串
另外,寄存器
具有完整的。它不会单独注册每个迭代,而是注册一个对象,该对象保存键results
中所有项目的结果
您仍然可以对用户进行循环并检查是否存在。但我不认为你能达到你想要的目的
首先在单个变量中注册结果,此处users\u 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": []
}
]
}
}
因此,结果不仅包含所有项的实际结果,还包含输入对象。这使您能够循环查看选中的用户。结果
而不是原始的用户
列表
- name: Check user current UID
shell: /usr/bin/id -u {{ item.item.username }}
with_items: users_checked.results
when: item.stdout == 1
但是现在它会变得很糟糕,因为你想再检查两件事。您可能可以继续使用这种方法,注册上面的结果,然后使用注册的数据结构作为下一个循环的输入,但最终将得到一个深度嵌套的对象,并在注册的\u var.results[x].item.results[y].item.results[z].item…
中的某个地方拥有原始对象
为了避免这种混乱,您可能需要对此任务进行调查