Adding new host and group gives error

362 views
Skip to first unread message

Punit Jain

unread,
Jan 30, 2020, 6:30:32 AM1/30/20
to ansible...@googlegroups.com
Hi,

I have a playbook requirement where new hosts come and leave. Based on it i plan to create groups dynamically. However below playbook gives an error:

---
- hosts: all
  connection: local
  gather_facts: no
  tasks:
    - name: add new host and group dynamically
      add_host:
        hostname: "test"
        ansible_host: "10.1.1.2"
        ansible_ipaddress: "10.1.1.2"
        groups: 'dynamicgroup'
 
- hosts: dynamicgroup
  connection: local
  gather_facts: no
  tasks:
    - name: all hosts in dynamicgroup
      debug:
        msg: "{{play_hosts}}"

    - name: flush
      meta: refresh_inventory

- hosts: newdynamicgroup
  connection: local
  gather_facts: no
  tasks:
    - name: add new host and group dynamically
      add_host:
        hostname: "test1"
        ansible_host: "10.1.1.3"
        ansible_ipaddress: "10.1.1.3"
        groups: 'newdynamicgroup'


     - name: all hosts in newdynamicgroup
       debug:
         msg: "{{play_hosts}}"

The first dynamic host and group gets added succesfully but 2nd one fails with below error:


ansible-playbook -i hosts testrole.yml -vvv
ansible-playbook 2.9.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/ansible/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible-playbook
  python version = 2.7.5 (default, Aug  7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
Using /etc/ansible/ansible.cfg as config file
host_list declined parsing /home/ansible/hosts as it did not pass its verify_file() method
script declined parsing /home/ansible/hosts as it did not pass its verify_file() method
auto declined parsing /home/ansible/hosts as it did not pass its verify_file() method
Parsed /home/ansible/hosts inventory source with ini plugin

PLAYBOOK: testrole.yml *************************************************************************************************************************
3 plays in testrole.yml

PLAY [all] *************************************************************************************************************************************
META: ran handlers

TASK [add new host and group dynamically] ******************************************************************************************************
task path: /home/ansible/testrole.yml:6
creating host via 'add_host': hostname=test
changed: [localhost] => {
    "add_host": {
        "groups": [
            "dynamicgroup"
        ],
        "host_name": "test",
        "host_vars": {
            "ansible_host": "10.1.1.2",
            "ansible_ipaddress": "10.1.1.2"
        }
    },
    "changed": true
}
META: ran handlers
META: ran handlers

PLAY [dynamicgroup] ****************************************************************************************************************************
META: ran handlers

TASK [all hosts in dynamicgroup] ***************************************************************************************************************
task path: /home/ansible/testrole.yml:17
ok: [test] => {
    "msg": [
        "test"
    ]
}
host_list declined parsing /home/ansible/hosts as it did not pass its verify_file() method
script declined parsing /home/ansible/hosts as it did not pass its verify_file() method
auto declined parsing /home/ansible/hosts as it did not pass its verify_file() method
Parsed /home/ansible/hosts inventory source with ini plugin
[WARNING]: Could not match supplied host pattern, ignoring: dynamicgroup

META: inventory successfully refreshed
[WARNING]: Could not match supplied host pattern, ignoring: newdynamicgroup



PLAY [newdynamicgroup] *************************************************************************************************************************
skipping: no hosts matched

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

any clue how to fix it ?

Regards,
Punit

Stefan Hornburg (Racke)

unread,
Jan 30, 2020, 7:38:17 AM1/30/20
to ansible...@googlegroups.com
On 1/30/20 12:30 PM, Punit Jain wrote:
> Hi,
>
> I have a playbook requirement where new hosts come and leave. Based on it i plan to create groups dynamically. However
> below playbook gives an error:
>

Hello Punit,

that's not an error. "newdynamicgroup" is empty so it doesn't execute the tasks in that play.

Regards
Racke

> ---
> - hosts: all
>   connection: local
>   gather_facts: no
>   tasks:
>     - name: add new host and group dynamically
>  *    add_host:
>         hostname: "test"
>         ansible_host: "10.1.1.2"
>         ansible_ipaddress: "10.1.1.2"
>         groups: 'dynamicgroup'
> * 
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAFXWBQLdS51hmSYC5fztZnN2AgCCCPxmxYodp%3D2J2PbOS7aMBQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAFXWBQLdS51hmSYC5fztZnN2AgCCCPxmxYodp%3D2J2PbOS7aMBQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Punit Jain

unread,
Jan 30, 2020, 9:10:57 AM1/30/20
to ansible...@googlegroups.com
But i was adding host in that play:

add_host:
        hostname: "test1"
        ansible_host: "10.1.1.3"
        ansible_ipaddress: "10.1.1.3"
        groups: 'newdynamicgroup'

This should add host as well to this group isinit or am I doing something wrong here ?

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3228be64-370e-10a1-36d9-fae791f1d715%40linuxia.de.

Stefan Hornburg (Racke)

unread,
Jan 30, 2020, 12:25:06 PM1/30/20
to ansible...@googlegroups.com
On 1/30/20 3:10 PM, Punit Jain wrote:
> But i was adding host in that play:
>
> add_host:
>         hostname: "test1"
>         ansible_host: "10.1.1.3"
>         ansible_ipaddress: "10.1.1.3"
>         groups: 'newdynamicgroup'
>
> This should add host as well to this group isinit or am I doing something wrong here ?

That doesn't matter as the host list (group newdynamicgroup) is empty when Ansible looks at your play. Which means it
skips all the tasks.

Regards
Racke
> > ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>>.
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/ansible-project/CAFXWBQLdS51hmSYC5fztZnN2AgCCCPxmxYodp%3D2J2PbOS7aMBQ%40mail.gmail.com
> >
> <https://groups.google.com/d/msgid/ansible-project/CAFXWBQLdS51hmSYC5fztZnN2AgCCCPxmxYodp%3D2J2PbOS7aMBQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/3228be64-370e-10a1-36d9-fae791f1d715%40linuxia.de.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAFXWBQ%2Bt5uMLXeae6THev8BCXoiwyy8fM36s%2B7vAd2X8ROO38Q%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAFXWBQ%2Bt5uMLXeae6THev8BCXoiwyy8fM36s%2B7vAd2X8ROO38Q%40mail.gmail.com?utm_medium=email&utm_source=footer>.
signature.asc
Reply all
Reply to author
Forward
0 new messages