---
#test loop with_item
- hosts: all
become: yes
gather_facts: no
pre_tasks:
- name: delete /tmp/test.txt
file:
path: /tmp/test.txt
state: absent
tasks:
- name: set facts ad_users
set_fact:
ad_users: []
- name: set facts ad_users, need to populate ad_results <--------------------
set_fact:
ad_results: {}
- name: Recursively find /home
find:
paths: /home
file_type: directory
register: find_var
- name: Append string to list
set_fact:
ad_users: "{{ ad_users + [ item.path| basename ] }}"
loop: "{{ find_var.files }}"
- debug: var=ad_users
- name: Get full username from Active Directory
shell: "/bin/adquery user '{{ item }}'| awk -F: '{print $5}'"
register: ad_fullname
loop: "{{ ad_users }}"
- name: debug ad_fullname
debug:
var: ad_fullname.results
这是全名的结果。调试结果
TASK [debug ad_fullname] **************************************************************
ok: [centos-test] => {
"ad_fullname.results": [
{
"_ansible_ignore_errors": null,
"_ansible_item_label": "apps",
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "/bin/adquery user 'apps'| awk -F: '{print $5}'",
"delta": "0:00:00.022701",
"end": "2019-06-28 00:09:02.713134",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "/bin/adquery user 'apps'| awk -F: '{print $5}'",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"warn": true
}
},
"item": "apps",
"rc": 0,
"start": "2019-06-28 00:09:02.690433",
"stderr": "apps is not a zone user",
"stderr_lines": [
"apps is not a zone user"
],
"stdout": "",
"stdout_lines": []
},
{
"_ansible_ignore_errors": null,
"_ansible_item_label": "linuxtest",
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "/bin/adquery user 'linuxtest'| awk -F: '{print $5}'",
"delta": "0:00:00.017644",
"end": "2019-06-28 00:09:02.841312",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "/bin/adquery user 'linuxtest'| awk -F: '{print $5}'",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"warn": true
}
},
"item": "linuxtest",
"rc": 0,
"start": "2019-06-28 00:09:02.823668",
"stderr": "",
"stderr_lines": [],
"stdout": "linux test",
"stdout_lines": [
"linux test"
]
},
{
"_ansible_ignore_errors": null,
"_ansible_item_label": "linuxtest2",
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "/bin/adquery user 'linuxtest2'| awk -F: '{print $5}'",
"delta": "0:00:00.017439",
"end": "2019-06-28 00:09:02.966616",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "/bin/adquery user 'linuxtest2'| awk -F: '{print $5}'",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"warn": true
}
},
"item": "linuxtest2",
"rc": 0,
"start": "2019-06-28 00:09:02.949177",
"stderr": "",
"stderr_lines": [],
"stdout": "linux test2",
"stdout_lines": [
"linux test2"
]
},
{
"_ansible_ignore_errors": null,
"_ansible_item_label": "cngo",
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "/bin/adquery user 'cngo'| awk -F: '{print $5}'",
"delta": "0:00:00.022745",
"end": "2019-06-28 00:09:03.097507",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "/bin/adquery user 'cngo'| awk -F: '{print $5}'",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"warn": true
}
},
"item": "cngo",
"rc": 0,
"start": "2019-06-28 00:09:03.074762",
"stderr": "cngo is not a zone user",
"stderr_lines": [
"cngo is not a zone user"
],
"stdout": "",
"stdout_lines": []
}
]
}
我需要使用以下内容填充字典ad_results
ad_fullname.results.item:ad_fullname.results.stdout
如果ad_fullname.results.stdout未定义
问题是 当我尝试调试时 我不断收到错误
- name: debug ad_fullname
debug:
var: ad_fullname.results.item
我不断收到此错误
“ ad_fullname.results.item”:“未定义变量!”
答案 0 :(得分:0)
顾名思义,示例文本显示.results
是一个列表,因此没有这样的属性item
;您正在寻找:
- name: debug ad_fullname
debug:
msg: '{{ ad_fullname.results | map(attribute="item") | list }}'