Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: removing hosts

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?

like image 533
cscan Avatar asked May 31 '16 23:05

cscan


People also ask

How do you get unreachable hosts in Ansible?

Details: Use Special Variables. Quoting: ansible_play_hosts_all: List of all the hosts that were targeted by the play. ansible_play_hosts: List of hosts in the current play run, not limited by the serial.

What is Hostvars Ansible?

With hostvars , you can access variables defined for any host in the play, at any point in a playbook. You can access Ansible facts using the hostvars variable too, but only after you have gathered (or cached) facts.

How do I ignore fatal errors in Ansible?

Ignoring failed commands By default Ansible stops executing tasks on a host when a task fails on that host. You can use ignore_errors to continue on in spite of the failure. The ignore_errors directive only works when the task is able to run and returns a value of 'failed'.

What is .RC in Ansible?

rc. Some modules execute command line utilities or are geared for executing commands directly (raw, shell, command, and so on), this field contains 'return code' of these utilities.


1 Answers

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'
like image 90
Nick Roz Avatar answered Oct 28 '22 00:10

Nick Roz