当原始任务使用 with_subelements 时如何访问 "results"
How do I access "results" when original ansible task uses with_subelements
我正在尝试在每个视图中生成 "views" 和 "domains" 的列表。
即:
internal/baseserver1
internal/baseserver2
external/baseserver1
...
对于以上每一项,我想检查远程主机上已知目录下是否存在 "regular" 文件,将上述值插入要检查的文件列表中。我正在尝试检查上面列表中每个 view/host 的 isreg
的值。
roles/default/main.yml
---
zone_dir: /var/named
zone_domains:
- domain:
name: "jlhimpel.net"
hostmaster_email: "john"
views:
- view:
name: internal
hosts:
- host:
name: baseserver1
ip: 192.168.1.20
- host:
name: baseserver2
ip: 192.168.1.30
- view:
name: external
hosts:
- host:
name: baseserver1
ip: 343.22.56.237
...
roles/tasks/main.yml
---
- name: Debug fqn zone path
stat:
path: /var/named/internal/jlhimpel.net.zone
register: f
become: true
- name: Display f
debug:
var: f
- name: Display f.stat.isreg
debug:
var: f.stat.isreg
- name: Debug symbolic zone path
stat:
path: "{{ zone_dir }}/{{ item[1].name }}/{{ item[0].name }}.zone"
loop: "{{ zone_domains|subelements('views') }}"
register: p
become: true
- name: Display p
debug:
var: p
- name: Display p.results.stat.isreg
debug:
var: item.isreg
with_items:
p.results.stat
...
输出:
PLAY [dnsServiceGroup] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [testFedora.jlhimpel.net]
TASK [pocDnsService : Debug fqn zone path] *************************************
ok: [testFedora.jlhimpel.net]
TASK [pocDnsService : Display f] ***********************************************
ok: [testFedora.jlhimpel.net] => {
"f": {
"changed": false,
"failed": false,
"stat": {
"atime": 1582308757.2357316,
"attr_flags": "",
"attributes": [],
"block_size": 4096,
"blocks": 8,
"charset": "us-ascii",
"checksum": "b8e6ecf9a617314ed1e040b4cd72dbde3dc9753c",
"ctime": 1581784999.833953,
"dev": 64768,
"device_type": 0,
"executable": false,
"exists": true,
"gid": 25,
"gr_name": "named",
"inode": 18788348,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mimetype": "text/plain",
"mode": "0640",
"mtime": 1581784999.1509356,
"nlink": 1,
"path": "/var/named/internal/jlhimpel.net.zone",
"pw_name": "root",
"readable": true,
"rgrp": true,
"roth": false,
"rusr": true,
"size": 406,
"uid": 0,
"version": "1161797691",
"wgrp": false,
"woth": false,
"writeable": true,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
}
}
TASK [pocDnsService : Display f.stat.isreg] ************************************
ok: [testFedora.jlhimpel.net] => {
"f.stat.isreg": true
}
TASK [pocDnsService : Debug symbolic zone path] ********************************
ok: [testFedora.jlhimpel.net] => (item=[{'domain': None, 'name': 'jlhimpel.net', 'hostmaster_email': 'john', 'views': [{'view': None, 'name': 'internal', 'hosts': [{'host': None, 'name': 'baseserver1', 'ip': '192.168.1.20'}, {'host': None, 'name': 'baseserver2', 'ip': '192.168.1.30'}]}, {'view': None, 'name': 'external', 'hosts': [{'host': None, 'name': 'baseserver1', 'ip': '343.22.56.237'}]}]}, {'view': None, 'name': 'internal', 'hosts': [{'host': None, 'name': 'baseserver1', 'ip': '192.168.1.20'}, {'host': None, 'name': 'baseserver2', 'ip': '192.168.1.30'}]}])
ok: [testFedora.jlhimpel.net] => (item=[{'domain': None, 'name': 'jlhimpel.net', 'hostmaster_email': 'john', 'views': [{'view': None, 'name': 'internal', 'hosts': [{'host': None, 'name': 'baseserver1', 'ip': '192.168.1.20'}, {'host': None, 'name': 'baseserver2', 'ip': '192.168.1.30'}]}, {'view': None, 'name': 'external', 'hosts': [{'host': None, 'name': 'baseserver1', 'ip': '343.22.56.237'}]}]}, {'view': None, 'name': 'external', 'hosts': [{'host': None, 'name': 'baseserver1', 'ip': '343.22.56.237'}]}])
TASK [pocDnsService : Display p] ***********************************************
ok: [testFedora.jlhimpel.net] => {
"p": {
"changed": false,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"invocation": {
"module_args": {
"checksum_algorithm": "sha1",
"follow": false,
"get_attributes": true,
"get_checksum": true,
"get_md5": false,
"get_mime": true,
"path": "/var/named/internal/jlhimpel.net.zone"
}
},
"item": [
{
"domain": null,
"hostmaster_email": "john",
"name": "jlhimpel.net",
"views": [
{
"hosts": [
{
"host": null,
"ip": "192.168.1.20",
"name": "baseserver1"
},
{
"host": null,
"ip": "192.168.1.30",
"name": "baseserver2"
}
],
"name": "internal",
"view": null
},
{
"hosts": [
{
"host": null,
"ip": "343.22.56.237",
"name": "baseserver1"
}
],
"name": "external",
"view": null
}
]
},
{
"hosts": [
{
"host": null,
"ip": "192.168.1.20",
"name": "baseserver1"
},
{
"host": null,
"ip": "192.168.1.30",
"name": "baseserver2"
}
],
"name": "internal",
"view": null
}
],
"stat": {
"atime": 1582308757.2357316,
"attr_flags": "",
"attributes": [],
"block_size": 4096,
"blocks": 8,
"charset": "us-ascii",
"checksum": "b8e6ecf9a617314ed1e040b4cd72dbde3dc9753c",
"ctime": 1581784999.833953,
"dev": 64768,
"device_type": 0,
"executable": false,
"exists": true,
"gid": 25,
"gr_name": "named",
"inode": 18788348,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mimetype": "text/plain",
"mode": "0640",
"mtime": 1581784999.1509356,
"nlink": 1,
"path": "/var/named/internal/jlhimpel.net.zone",
"pw_name": "root",
"readable": true,
"rgrp": true,
"roth": false,
"rusr": true,
"size": 406,
"uid": 0,
"version": "1161797691",
"wgrp": false,
"woth": false,
"writeable": true,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
},
{
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"invocation": {
"module_args": {
"checksum_algorithm": "sha1",
"follow": false,
"get_attributes": true,
"get_checksum": true,
"get_md5": false,
"get_mime": true,
"path": "/var/named/external/jlhimpel.net.zone"
}
},
"item": [
{
"domain": null,
"hostmaster_email": "john",
"name": "jlhimpel.net",
"views": [
{
"hosts": [
{
"host": null,
"ip": "192.168.1.20",
"name": "baseserver1"
},
{
"host": null,
"ip": "192.168.1.30",
"name": "baseserver2"
}
],
"name": "internal",
"view": null
},
{
"hosts": [
{
"host": null,
"ip": "343.22.56.237",
"name": "baseserver1"
}
],
"name": "external",
"view": null
}
]
},
{
"hosts": [
{
"host": null,
"ip": "343.22.56.237",
"name": "baseserver1"
}
],
"name": "external",
"view": null
}
],
"stat": {
"atime": 1582326121.0318115,
"attr_flags": "",
"attributes": [],
"block_size": 4096,
"blocks": 8,
"charset": "us-ascii",
"checksum": "5deeb57dfd80c5b264ab5ebff8af728b2a551860",
"ctime": 1582079607.5288289,
"dev": 64768,
"device_type": 0,
"executable": false,
"exists": true,
"gid": 0,
"gr_name": "root",
"inode": 25378368,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mimetype": "text/plain",
"mode": "0640",
"mtime": 1582079607.4818277,
"nlink": 1,
"path": "/var/named/external/jlhimpel.net.zone",
"pw_name": "root",
"readable": true,
"rgrp": true,
"roth": false,
"rusr": true,
"size": 406,
"uid": 0,
"version": "2850301314",
"wgrp": false,
"woth": false,
"writeable": true,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
}
]
}
}
TASK [pocDnsService : Display f.stat[].isreg] **********************************
ok: [testFedora.jlhimpel.net] => (item=p.results.stat) => {
"ansible_loop_var": "item",
"item": "p.results.stat",
"item.isreg": "VARIABLE IS NOT DEFINED!"
}
如有任何建议,我们将不胜感激。
很抱歉粘贴了如此冗长的输出。
下面的任务
- name: Display p.results.stat.isreg
debug:
msg: "{{ item }}"
loop: "{{ p.results|json_query('[].stat.isreg') }}"
给予
"msg": true
"msg": true
我正在尝试在每个视图中生成 "views" 和 "domains" 的列表。
即:
internal/baseserver1
internal/baseserver2
external/baseserver1
...
对于以上每一项,我想检查远程主机上已知目录下是否存在 "regular" 文件,将上述值插入要检查的文件列表中。我正在尝试检查上面列表中每个 view/host 的 isreg
的值。
roles/default/main.yml
---
zone_dir: /var/named
zone_domains:
- domain:
name: "jlhimpel.net"
hostmaster_email: "john"
views:
- view:
name: internal
hosts:
- host:
name: baseserver1
ip: 192.168.1.20
- host:
name: baseserver2
ip: 192.168.1.30
- view:
name: external
hosts:
- host:
name: baseserver1
ip: 343.22.56.237
...
roles/tasks/main.yml
---
- name: Debug fqn zone path
stat:
path: /var/named/internal/jlhimpel.net.zone
register: f
become: true
- name: Display f
debug:
var: f
- name: Display f.stat.isreg
debug:
var: f.stat.isreg
- name: Debug symbolic zone path
stat:
path: "{{ zone_dir }}/{{ item[1].name }}/{{ item[0].name }}.zone"
loop: "{{ zone_domains|subelements('views') }}"
register: p
become: true
- name: Display p
debug:
var: p
- name: Display p.results.stat.isreg
debug:
var: item.isreg
with_items:
p.results.stat
...
输出:
PLAY [dnsServiceGroup] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [testFedora.jlhimpel.net]
TASK [pocDnsService : Debug fqn zone path] *************************************
ok: [testFedora.jlhimpel.net]
TASK [pocDnsService : Display f] ***********************************************
ok: [testFedora.jlhimpel.net] => {
"f": {
"changed": false,
"failed": false,
"stat": {
"atime": 1582308757.2357316,
"attr_flags": "",
"attributes": [],
"block_size": 4096,
"blocks": 8,
"charset": "us-ascii",
"checksum": "b8e6ecf9a617314ed1e040b4cd72dbde3dc9753c",
"ctime": 1581784999.833953,
"dev": 64768,
"device_type": 0,
"executable": false,
"exists": true,
"gid": 25,
"gr_name": "named",
"inode": 18788348,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mimetype": "text/plain",
"mode": "0640",
"mtime": 1581784999.1509356,
"nlink": 1,
"path": "/var/named/internal/jlhimpel.net.zone",
"pw_name": "root",
"readable": true,
"rgrp": true,
"roth": false,
"rusr": true,
"size": 406,
"uid": 0,
"version": "1161797691",
"wgrp": false,
"woth": false,
"writeable": true,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
}
}
TASK [pocDnsService : Display f.stat.isreg] ************************************
ok: [testFedora.jlhimpel.net] => {
"f.stat.isreg": true
}
TASK [pocDnsService : Debug symbolic zone path] ********************************
ok: [testFedora.jlhimpel.net] => (item=[{'domain': None, 'name': 'jlhimpel.net', 'hostmaster_email': 'john', 'views': [{'view': None, 'name': 'internal', 'hosts': [{'host': None, 'name': 'baseserver1', 'ip': '192.168.1.20'}, {'host': None, 'name': 'baseserver2', 'ip': '192.168.1.30'}]}, {'view': None, 'name': 'external', 'hosts': [{'host': None, 'name': 'baseserver1', 'ip': '343.22.56.237'}]}]}, {'view': None, 'name': 'internal', 'hosts': [{'host': None, 'name': 'baseserver1', 'ip': '192.168.1.20'}, {'host': None, 'name': 'baseserver2', 'ip': '192.168.1.30'}]}])
ok: [testFedora.jlhimpel.net] => (item=[{'domain': None, 'name': 'jlhimpel.net', 'hostmaster_email': 'john', 'views': [{'view': None, 'name': 'internal', 'hosts': [{'host': None, 'name': 'baseserver1', 'ip': '192.168.1.20'}, {'host': None, 'name': 'baseserver2', 'ip': '192.168.1.30'}]}, {'view': None, 'name': 'external', 'hosts': [{'host': None, 'name': 'baseserver1', 'ip': '343.22.56.237'}]}]}, {'view': None, 'name': 'external', 'hosts': [{'host': None, 'name': 'baseserver1', 'ip': '343.22.56.237'}]}])
TASK [pocDnsService : Display p] ***********************************************
ok: [testFedora.jlhimpel.net] => {
"p": {
"changed": false,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"invocation": {
"module_args": {
"checksum_algorithm": "sha1",
"follow": false,
"get_attributes": true,
"get_checksum": true,
"get_md5": false,
"get_mime": true,
"path": "/var/named/internal/jlhimpel.net.zone"
}
},
"item": [
{
"domain": null,
"hostmaster_email": "john",
"name": "jlhimpel.net",
"views": [
{
"hosts": [
{
"host": null,
"ip": "192.168.1.20",
"name": "baseserver1"
},
{
"host": null,
"ip": "192.168.1.30",
"name": "baseserver2"
}
],
"name": "internal",
"view": null
},
{
"hosts": [
{
"host": null,
"ip": "343.22.56.237",
"name": "baseserver1"
}
],
"name": "external",
"view": null
}
]
},
{
"hosts": [
{
"host": null,
"ip": "192.168.1.20",
"name": "baseserver1"
},
{
"host": null,
"ip": "192.168.1.30",
"name": "baseserver2"
}
],
"name": "internal",
"view": null
}
],
"stat": {
"atime": 1582308757.2357316,
"attr_flags": "",
"attributes": [],
"block_size": 4096,
"blocks": 8,
"charset": "us-ascii",
"checksum": "b8e6ecf9a617314ed1e040b4cd72dbde3dc9753c",
"ctime": 1581784999.833953,
"dev": 64768,
"device_type": 0,
"executable": false,
"exists": true,
"gid": 25,
"gr_name": "named",
"inode": 18788348,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mimetype": "text/plain",
"mode": "0640",
"mtime": 1581784999.1509356,
"nlink": 1,
"path": "/var/named/internal/jlhimpel.net.zone",
"pw_name": "root",
"readable": true,
"rgrp": true,
"roth": false,
"rusr": true,
"size": 406,
"uid": 0,
"version": "1161797691",
"wgrp": false,
"woth": false,
"writeable": true,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
},
{
"ansible_loop_var": "item",
"changed": false,
"failed": false,
"invocation": {
"module_args": {
"checksum_algorithm": "sha1",
"follow": false,
"get_attributes": true,
"get_checksum": true,
"get_md5": false,
"get_mime": true,
"path": "/var/named/external/jlhimpel.net.zone"
}
},
"item": [
{
"domain": null,
"hostmaster_email": "john",
"name": "jlhimpel.net",
"views": [
{
"hosts": [
{
"host": null,
"ip": "192.168.1.20",
"name": "baseserver1"
},
{
"host": null,
"ip": "192.168.1.30",
"name": "baseserver2"
}
],
"name": "internal",
"view": null
},
{
"hosts": [
{
"host": null,
"ip": "343.22.56.237",
"name": "baseserver1"
}
],
"name": "external",
"view": null
}
]
},
{
"hosts": [
{
"host": null,
"ip": "343.22.56.237",
"name": "baseserver1"
}
],
"name": "external",
"view": null
}
],
"stat": {
"atime": 1582326121.0318115,
"attr_flags": "",
"attributes": [],
"block_size": 4096,
"blocks": 8,
"charset": "us-ascii",
"checksum": "5deeb57dfd80c5b264ab5ebff8af728b2a551860",
"ctime": 1582079607.5288289,
"dev": 64768,
"device_type": 0,
"executable": false,
"exists": true,
"gid": 0,
"gr_name": "root",
"inode": 25378368,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mimetype": "text/plain",
"mode": "0640",
"mtime": 1582079607.4818277,
"nlink": 1,
"path": "/var/named/external/jlhimpel.net.zone",
"pw_name": "root",
"readable": true,
"rgrp": true,
"roth": false,
"rusr": true,
"size": 406,
"uid": 0,
"version": "2850301314",
"wgrp": false,
"woth": false,
"writeable": true,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
}
]
}
}
TASK [pocDnsService : Display f.stat[].isreg] **********************************
ok: [testFedora.jlhimpel.net] => (item=p.results.stat) => {
"ansible_loop_var": "item",
"item": "p.results.stat",
"item.isreg": "VARIABLE IS NOT DEFINED!"
}
如有任何建议,我们将不胜感激。
很抱歉粘贴了如此冗长的输出。
下面的任务
- name: Display p.results.stat.isreg
debug:
msg: "{{ item }}"
loop: "{{ p.results|json_query('[].stat.isreg') }}"
给予
"msg": true
"msg": true