来自已注册var输出的Ansible映射筛选器
我使用ansible通过一个注册变量从一台服务器获取dhcpd.leases文件的完整副本。ansible debug的输出如下所示:来自已注册var输出的Ansible映射筛选器,ansible,ansible-2.x,ansible-inventory,Ansible,Ansible 2.x,Ansible Inventory,我使用ansible通过一个注册变量从一台服务器获取dhcpd.leases文件的完整副本。ansible debug的输出如下所示: TASK [Show passive storage nodes info] ***********************************************************************************************************************************************
TASK [Show passive storage nodes info] *************************************************************************************************************************************************************
ok: [10.241.55.2] => {
"msg": [
"# The format of this file is documented in the dhcpd.leases(5) manual page.",
"# This lease file was written by isc-dhcp-4.2.2",
"",
"lease 10.241.55.165 {",
" starts 4 2019/12/12 09:30:37;",
" ends 4 2019/12/12 21:30:37;",
" tstp 4 2019/12/12 21:30:37;",
" cltt 4 2019/12/12 09:30:37;",
" binding state free;",
" hardware ethernet b4:d1:35:00:04:50;",
" uid \"\\001\\264\\3215\\000\\004P\";",
"}",
"lease 10.241.55.163 {",
" starts 1 2019/12/16 08:10:56;",
" ends 1 2019/12/16 20:10:56;",
" tstp 1 2019/12/16 20:10:56;",
" cltt 1 2019/12/16 08:10:56;",
" binding state free;",
" hardware ethernet b4:d1:35:00:03:d7;",
"}",
"lease 10.241.55.159 {",
" starts 1 2019/12/16 08:52:20;",
" ends 1 2019/12/16 20:52:20;",
" tstp 1 2019/12/16 20:52:20;",
" cltt 1 2019/12/16 08:52:20;",
" binding state free;",
" hardware ethernet b4:d1:35:00:03:b4;",
"}",
"lease 10.241.55.166 {",
" starts 1 2019/12/16 08:54:38;",
" ends 1 2019/12/16 20:54:38;",
" tstp 1 2019/12/16 20:54:38;",
" cltt 1 2019/12/16 08:54:38;",
" binding state free;",
" hardware ethernet b4:d1:35:00:04:63;",
"}",
"lease 10.241.55.164 {",
" starts 1 2019/12/16 10:48:10;",
" ends 1 2019/12/16 22:48:10;",
" tstp 1 2019/12/16 22:48:10;",
" cltt 1 2019/12/16 10:48:10;",
" binding state free;",
" hardware ethernet b4:d1:35:00:04:4e;",
"}",
"lease 10.241.55.143 {",
" starts 5 2020/02/14 04:38:09;",
" ends 5 2020/02/14 16:38:09;",
" cltt 5 2020/02/14 04:38:09;",
" binding state active;",
" next binding state free;",
" rewind binding state free;",
" hardware ethernet 08:94:ef:9c:80:4a;",
" client-hostname \"linux-compute-e7d573cb9fda4e689939e21a7843c4fb\";",
"}",
"lease 10.241.55.148 {",
" starts 5 2020/02/14 05:03:08;",
" ends 5 2020/02/14 17:03:08;",
" cltt 5 2020/02/14 05:03:08;",
" binding state active;",
" next binding state free;",
" rewind binding state free;",
" hardware ethernet 00:e0:ec:ca:44:84;",
" client-hostname \"linux-storage-4391fc6548764de5a15a46b432d3cd37\";",
"}",
"lease 10.241.55.235 {",
" starts 5 2020/02/14 05:20:28;",
" ends 5 2020/02/14 17:20:28;",
" cltt 5 2020/02/14 05:20:28;",
" binding state active;",
" next binding state free;",
" rewind binding state free;",
" hardware ethernet 00:e0:ec:ca:45:74;",
" client-hostname \"linux-storage-58aba44ac09d4f639b9374b9142b1a53\";",
"}",
"lease 10.241.55.141 {",
" starts 5 2020/02/14 06:01:04;",
" ends 5 2020/02/14 18:01:04;",
" cltt 5 2020/02/14 06:01:04;",
" binding state active;",
" next binding state free;",
" rewind binding state free;",
" hardware ethernet 08:94:ef:9c:80:6c;",
" client-hostname \"linux-compute-d228bc79357c4c8b87c280f9efae6ba6\";",
"}",
"lease 10.241.55.236 {",
" starts 5 2020/02/14 06:12:28;",
" ends 5 2020/02/14 18:12:28;",
" cltt 5 2020/02/14 06:12:28;",
" binding state active;",
" next binding state free;",
" rewind binding state free;",
" hardware ethernet 00:e0:ec:ca:45:75;",
" client-hostname \"linux-storage-58aba44ac09d4f639b9374b9142b1a53\";",
"}",
"lease 10.241.55.140 {",
" starts 5 2020/02/14 06:13:18;",
" ends 5 2020/02/14 18:13:18;",
" cltt 5 2020/02/14 06:13:18;",
" binding state active;",
" next binding state free;",
" rewind binding state free;",
" hardware ethernet 08:94:ef:9c:80:24;",
" client-hostname \"linux-compute-d0a9a76a7b5249048f78642220a1958c\";",
"}",
- set_fact:
dhcp_hosts: "{{ dhcp.stdout_lines | selectattr('client-hostname', 'defined') | map(attribute='lease') | list }}"
是否需要映射已归档的租约ip+客户端主机名,然后在此基础上进行过滤,并严格将ip放入清单文件中?我知道我可以直接获得IP,但是有些服务器有4个IP,所以它们有相同的主机名。与其将所有4个IP都放在清单中,我只需要它们的一个IP(不管是哪一个),我正试图根据主机名进行过滤。此外,输出中还有一些条目没有客户端主机名,因此理想情况下应该忽略这些条目
这个问题与我在这里发布的另一个问题类似:
但是有了这个,我尝试了一种不同的方法
到目前为止,我尝试的是:
debug:
msg: "{{ p_storage_ips.stdout | map(attribute='client-hostname' | list) }}"
但我什么也得不到:
任务[显示被动存储节点信息]
ok: [10.241.55.2] => {
"msg": "<generator object do_map at 0x7f40e52785f0>"
}
任何帮助都将不胜感激 没关系,我发现直接从bash过滤发送到ansible的输出会更容易。最后我想到了这个:
awk '!seen[$0]++' /var/lib/dhcp/dhcpd.leases | grep -vE -- 'ends|starts|tstp|cltt|uid|bind|hardware' | grep -B1 storage | awk '{print $2}' | grep '^[0-9]'
这将为存储节点生成2个ip地址(共8个)
这并不能解决最初的问题,但至少提供了所需的答案
10.241.55.148
10.241.55.235