Ansible: removing hosts

10,666

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'
Share:
10,666
cscan
Author by

cscan

Updated on June 11, 2022

Comments

  • cscan
    cscan almost 2 years

    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?