Skip to main content
2 of 3
added 346 characters in body

The add_host task works as expected. For example (simplified a bit),

    - add_host:
        name: "{{ item }}"
        groups: cloned_group
      loop: [cm_1, cm_2, cm_3]

will create the inventory group cloned_group. Let's take a look

    - debug:
        var: groups.cloned_group

gives as expected

  groups.cloned_group:
  - cm_1
  - cm_2
  - cm_3

See the special variable group. Quoting:

A dictionary/map with all the groups in inventory and each group has the list of hosts that belong to it


Example of a complete playbook for testing

- hosts: localhost

  tasks:

    - add_host:
        name: "{{ item }}"
        groups: cloned_group
      loop: [cm_1, cm_2, cm_3]

    - debug:
        var: groups.cloned_group