如何解决Ansible 如何通过键删除组值
我正在玩一个游戏,我将在运行任务之前收集可用的主机名,我将其用于某个目的,
我的播放代码:
--
- name: check reachable side A hosts
hosts: ????ha???
connection: local
gather_facts: no
roles:
- Juniper.junos
vars:
credentials:
host: "{{ loopback_v4 }}"
username: "test"
ssh_keyfile: "/id_rsa"
port: "{{ port }}"
timeout: 60
tasks:
- block:
- name: "Check netconf connectivity with switches"
juniper_junos_ping:
provider: "{{ credentials }}"
dest: "{{ loopback_v4 }}"
- name: Add devices with connectivity to the "reachable" group
group_by:
key: "reachable_other_pairs"
rescue:
- debug: msg="Cannot ping to {{inventory_hostname}}. Skipping OS Install"
当我使用
- debug:
msg: "group: {{ groups['reachable_other_pairs'] }}"
我得到了低于结果
"this group : ['testha1','testha2','testha3']",
现在,如果使用相同键分组的不同主机再次调用相同的游戏,我将获得附加到现有值的新主机名,如下所示
- name: check reachable side B hosts
hosts: ????hb???
connection: local
gather_facts: no
roles:
- Juniper.junos
vars:
credentials:
host: "{{ loopback_v4 }}"
username: "test"
ssh_keyfile: "/id_rsa"
port: "{{ port }}"
timeout: 60
tasks:
- block:
- name: "Check netconf connectivity with switches"
juniper_junos_ping:
provider: "{{ credentials }}"
dest: "{{ loopback_v4 }}"
- name: Add devices with connectivity to the "reachable" group
group_by:
key: "reachable_other_pairs"
rescue:
- debug: msg="Cannot ping to {{inventory_hostname}}. Skipping OS Install"
如果我打印reachable_other_pairs,我会得到低于结果
"msg": " new group: ['testhb1','testhb2','testhb3','testha1','testha3']"
我只想要前 3 个条目 ['testhb1','testhb3']
有人可以让我知道如何实现这一目标吗?
解决方法
将其添加为块之前的任务。它将刷新您的库存并清理所有不在那里的组:
- meta: refresh_inventory
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。