Add host using add_host not taking effect

Hi,

I am doing a very straight forward testing of adding a new host dynamically.

Split the playbook to 2 plays. Add the host in the first play, and use it the
second.

- hosts: all
   connection: local
   gather_facts: no
   tasks:
     - name: add host
       add_host:
         name: 'testserver'
         groups: all
       run_once: true

- hosts: all
   connection: local
   gather_facts: no
   tasks:
     - name: print
       debug:
         msg: "hello"

HTH,

  -vlado

In below usecase it is not working:

run.yml

run.yml
----------
---
- hosts: all
  connection: local
  gather_facts: no

  tasks:
    - name: include role task
      include_role:
        name: role1
        tasks_from: hello

- hosts: all
  connection: local
  gather_facts: no

  tasks:
    - name: include role task
      include_role:
        name: role1
        tasks_from: bye

then inside role tasks:
------------------------
hello.yml

- name: add host
  add_host:
    name: 'test'
    groups: all

There is no point in adding a host to all, the all group are a special group that all hosts is a member of.

- name: refresh
  meta: refresh_inventory

Here you say, purge my inventory and read it in again.
Then all your dynamic hosts get purged and your inventory is read again which don't have the dynamic ones.
So just remove this task and it should work.

The point is to add a host that hasn't been declared in any inventory yet.

  -vlado

I understand that, it wasn't that I commented on, I can rephrase
Using "groups: all" in add_host is pointless since all hosts is automatically added to the group all anyway.