Ansible: removing hosts

cscan picture cscan · Jun 1, 2016 · Viewed 8.6k times · Source

I know that one can add host with the following task:

- name: Add new instance to host group
  add_host:
    hostname: '{{ item.public_ip }}'
    groupname: "tag_Name_api_production"
  with_items: ec2.instances

But I can't seem to find a way to remove a host from inventory. Is there any way to do this?

Answer

Nick Roz picture Nick Roz · Jul 6, 2016

Unfortunately, it seems, that you can't do this using Ansible 2. There is no such a module called remove_host or another one.

However, using Ansible 2 you can refresh your inventory mid-play:

- meta: refresh_inventory

Have a look at this question

Another idea might be to filter hosts beforehand. Try adding them to group, and then excluding this group in a play lately, e.g. :

- hosts: '!databases'