ansible - 来自已注册 var 输出的 Ansible 地图过滤器
问题描述
我通过一个已注册的变量从一台服务器获取 dhcpd.leases 文件的完整副本。带有 ansible 调试的输出如下所示:
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\";",
"}",
无论如何映射提交的租约ip +客户端主机名然后基于它进行过滤并将ips严格放在库存文件中?我知道我可以直接获取 ip,但是有些服务器有 4 个 ip,因此它们具有相同的主机名。而不是将所有 4 个 ip 都放入清单中,我只需要他们的一个 ip(不管是哪一个),并且我正在尝试根据主机名进行过滤。此外,输出中的某些条目没有客户端主机名,因此理想情况下应该忽略这些条目。
这个问题与我在这里发布的另一个问题相似: Ansible jina2 template, filter output based on uniqueness
但是有了这个,我正在尝试一种不同的方法。
到目前为止,我尝试过的是:
debug:
msg: "{{ p_storage_ips.stdout | map(attribute='client-hostname' | list) }}"
但我什么也没得到: TASK [显示被动存储节点信息]
ok: [10.241.55.2] => {
"msg": "<generator object do_map at 0x7f40e52785f0>"
}
也试过这样:
- set_fact:
dhcp_hosts: "{{ dhcp.stdout_lines | selectattr('client-hostname', 'defined') | map(attribute='lease') | list }}"
任何帮助将非常感激!
解决方案
没关系,我发现过滤直接从 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
这并不能解决最初的问题,但至少提供了所需的答案。
推荐阅读
- python - 如何自定义特定熊猫数据框列的函数调用顺序?
- c - 使用结构在 C 中初始化 void qsort() 的参数 4
- plsql - ALL_MVIEWS 的循环游标生成 PL/SQL 错误 ORA-06502
- android-ndk - 当我用 android-ndk 构建 x264 时,它没有完成
- windows - 清除 Visual Studio Code 的 workspaceStorage 文件夹
- docker - 无法连接在不同端口上运行的 Elasticsearch docker 容器
- ios - Ionic 支持:我想使用本机 ios 应用程序库 .a 文件到当前的 ionic 项目。怎么做?
- bash - 如何在 shell 脚本中使用 awk 的位置参数
- c++ - c++14: 模板化 boost 静态访问者
- github - 无法在 github 机密中存储特殊字符