鉴于以下原因,我想最后列出与这些UID匹配的用户名列表,例如用户:['u1']
- name: getent
hosts: all
tasks:
- name: getent
getent:
database: passwd
key: "{{ item }}"
split: ':'
with_items:
- 20001
register: users
- name: debug
debug:
msg: "{{ users.results | map(attribute='ansible_facts.getent_passwd') | list }}"
TASK [debug] *******************************************************************
ok: [localhost] => {
"users": {
"changed": false,
"msg": "All items completed",
"results": [
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"ansible_facts": {
"getent_passwd": {
"u1": [ <-- I do not care about anything contained within u1!
至于我最终得到
[{u'u1': [u'x', u'20001', u'20001', u'', u'/home/u1', u'/sbin/nologin']}]
我怎么得到
['u1']
答案 0 :(得分:0)
以下是使用JMESPath查询的解决方案:
- debug:
msg: "{{ users.results | json_query('[].ansible_facts.getent_passwd.keys(@)[]') }}"
[].ansible_facts.getent_passwd
等同于您的过滤器keys(@)
函数返回上述列表的键名[]
展平结果列表