Add host using add_host not taking effect

38 views
Skip to first unread message

Punit Jain

unread,
Jan 22, 2020, 11:03:27 AM1/22/20
to ansible...@googlegroups.com
Hi,

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

---
- hosts: all
  connection: local
  gather_facts: no

  tasks:
    - name: add host
      add_host:
        name: 'testserver'
        groups: all

    - name: refresh
      meta: refresh_inventory

    - name: print
      debug:
        msg: "hello"
~

When i run this playbook

ansible-playbook hello.yml -i hosts

it doesnot run task for added host

 ansible-playbook -i hosts hello.yml

PLAY [all] ************************************************************************************************************************

TASK [add host] *******************************************************************************************************************
changed: [localhost]

TASK [print] **********************************************************************************************************************
ok: [localhost] => {
    "msg": "hello"
}


PLAY RECAP ************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

I dont see task for testserver.

Any idea what am i missing ?

Vladimir Botka

unread,
Jan 22, 2020, 11:16:05 AM1/22/20
to Punit Jain, ansible...@googlegroups.com
On Wed, 22 Jan 2020 21:32:54 +0530
Punit Jain <contactp...@gmail.com> wrote:

> - hosts: all
> connection: local
> gather_facts: no
>
> tasks:
> - name: add host
> add_host:
> name: 'testserver'
> groups: all
>
> - name: refresh
> meta: refresh_inventory
>
> - name: print
> debug:
> msg: "hello"
> [...]
> it doesnot run task for added host
> I dont see task for testserver.

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

Punit Jain

unread,
Jan 22, 2020, 12:46:37 PM1/22/20
to Vladimir Botka, ansible...@googlegroups.com
In below usecase it is not working:

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

- name: refresh
  meta: refresh_inventory

and other task file
bye.yml

- name: print
  debug:
    msg: "bye"

On running

ansible-playbook -i hosts run.yml

PLAY [all] ************************************************************************************************************************

TASK [include role task] **********************************************************************************************************

TASK [role1 : add host] ***********************************************************************************************************
changed: [localhost]

PLAY [all] ************************************************************************************************************************

TASK [include role task] **********************************************************************************************************

TASK [role1 : print] **************************************************************************************************************
ok: [localhost] => {
    "msg": "bye"
}


PLAY RECAP ************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

other play is called only on 1 host than new one added.

Regards,
Punit

Kai Stian Olstad

unread,
Jan 22, 2020, 1:15:51 PM1/22/20
to ansible...@googlegroups.com
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.

--
Kai Stian Olstad

Vladimir Botka

unread,
Jan 22, 2020, 1:56:24 PM1/22/20
to Kai Stian Olstad, ansible...@googlegroups.com
On Wed, 22 Jan 2020 19:15:27 +0100
Kai Stian Olstad <ansible-pr...@olstad.com> wrote:

> > - 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.

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

-vlado

Kai Stian Olstad

unread,
Jan 22, 2020, 3:09:40 PM1/22/20
to ansible...@googlegroups.com
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.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages