Ansible - 自动删除Sensu中的主机

时间:2017-01-13 17:50:22

标签: ansible ansible-2.x sensu

我正在使用Ansible的库存文件和sensu-cli(与Sensu的API交互)

从库存中删除服务器后,服务器仍然会一直停留,直到通过API调用手动删除它。

此工作流程有效,但无论如何都不理想。

最后一项任务将删除所有服务器。任何安装了sensu-client的服务器都将联系Sensu主站并自行读取。这根本不酷。

这里的想法是仅删除不在清单文件中的服务器。

- name: Get a list hosts in Sensu
  shell: sensu-cli client list -f json | jq -r .[].name
  register: sensu_hosts

- name: Get a list of Ansible hosts
  set_fact:
    sensu_ansible_hosts: "{{ hostvars[item]['inventory_hostname'] }}"
  with_items: groups['all']

- name: Delete clients not in inventory
  shell: sensu-cli client delete {{ item }}
  with_items: sensu_hosts.stdout_lines
  when: item not in sensu_ansible_hosts

1 个答案:

答案 0 :(得分:1)

试试这个:

- name: Get a list hosts in Sensu
  shell: sensu-cli client list -f json | jq -r .[].name
  register: sensu_hosts

- name: Delete clients not in inventory
  shell: sensu-cli client delete {{ item }}
  with_items: "{{ sensu_hosts.stdout_lines | difference( groups['all'] ) }}"
相关问题