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

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAFXWBQLdS51hmSYC5fztZnN2AgCCCPxmxYodp%3D2J2PbOS7aMBQ%40mail.gmail.com.

Reply via email to