Skip to main content
Add example.
Source Link

The add_host task works as expected. For example (simplified a bit), given the list of the groups

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

Example of a complete playbook for testing

- hosts: localhost

  vars:

    my_groups: [cloned_group]

  tasks:

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

    - debug:
        var: groups.cloned_group

Q: "No hosts added to the cloned_group in the inventory."

A: If you by "in the inventory" mean "in the inventory file" then this is correct. No hosts will be added to an inventory file.

For example, given the below inventory file

shell> cat hosts
[test]
test_01
test_02
test_03

[test:vars]
ansible_user=admin
ansible_become=true
ansible_python_interpreter=/usr/local/bin/python3.8
ansible_perl_interpreter=/usr/local/bin/perl

If you want to update the inventory file:

  • Get the path to the file. See the special variable ansible_inventory_sources. There might be more files in the list, hence take the first one
  my_file: "{{ ansible_inventory_sources.0 }}"

gives

  my_file: /scratch/tmp1/test-024/hosts
  • Create the block with your group(s)
  my_inventory: |
    {% for group in my_groups %}
    [{{ group }}]
    {% for host in groups[group] %}
    {{ host }}
    {% endfor %}
    {% endfor %}

gives

  my_inventory: |-
    [cloned_group]
    cm_1
    cm_2
    cm_3
  • Backup the inventory file
    - command:
        cmd: "cp {{ my_file }} {{ my_file }}.backup"
        creates: "{{ my_file }}.backup"
  • Update the inventory file
    - blockinfile:
        path: "{{ my_file }}"
        block: "{{ my_inventory }}"

gives what you (probably) want

shell> cat hosts
[test]
test_01
test_02
test_03

[test:vars]
ansible_user=admin
ansible_become=true
ansible_python_interpreter=/usr/local/bin/python3.8
ansible_perl_interpreter=/usr/local/bin/perl
# BEGIN ANSIBLE MANAGED BLOCK
[cloned_group]
cm_1
cm_2
cm_3
# END ANSIBLE MANAGED BLOCK

Example of a complete playbook for testing

- hosts: localhost

  vars:

    my_file: "{{ ansible_inventory_sources.0 }}"
    my_groups: [cloned_group]
    my_inventory: |
      {% for group in my_groups %}
      [{{ group }}]
      {% for host in groups[group] %}
      {{ host }}
      {% endfor %}
      {% endfor %}

  tasks:

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

    - debug:
        var: groups.cloned_group

    - debug:
        var: ansible_inventory_sources
    - debug:
        var: my_file
    - debug:
        var: my_inventory

    - command:
        cmd: "cp {{ my_file }} {{ my_file }}.backup"
        creates: "{{ my_file }}.backup"

    - blockinfile:
        path: "{{ my_file }}"
        block: "{{ my_inventory }}"

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]

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

The add_host task works as expected. For example (simplified a bit), given the list of the groups

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

Example of a complete playbook for testing

- hosts: localhost

  vars:

    my_groups: [cloned_group]

  tasks:

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

    - debug:
        var: groups.cloned_group

Q: "No hosts added to the cloned_group in the inventory."

A: If you by "in the inventory" mean "in the inventory file" then this is correct. No hosts will be added to an inventory file.

For example, given the below inventory file

shell> cat hosts
[test]
test_01
test_02
test_03

[test:vars]
ansible_user=admin
ansible_become=true
ansible_python_interpreter=/usr/local/bin/python3.8
ansible_perl_interpreter=/usr/local/bin/perl

If you want to update the inventory file:

  • Get the path to the file. See the special variable ansible_inventory_sources. There might be more files in the list, hence take the first one
  my_file: "{{ ansible_inventory_sources.0 }}"

gives

  my_file: /scratch/tmp1/test-024/hosts
  • Create the block with your group(s)
  my_inventory: |
    {% for group in my_groups %}
    [{{ group }}]
    {% for host in groups[group] %}
    {{ host }}
    {% endfor %}
    {% endfor %}

gives

  my_inventory: |-
    [cloned_group]
    cm_1
    cm_2
    cm_3
  • Backup the inventory file
    - command:
        cmd: "cp {{ my_file }} {{ my_file }}.backup"
        creates: "{{ my_file }}.backup"
  • Update the inventory file
    - blockinfile:
        path: "{{ my_file }}"
        block: "{{ my_inventory }}"

gives what you (probably) want

shell> cat hosts
[test]
test_01
test_02
test_03

[test:vars]
ansible_user=admin
ansible_become=true
ansible_python_interpreter=/usr/local/bin/python3.8
ansible_perl_interpreter=/usr/local/bin/perl
# BEGIN ANSIBLE MANAGED BLOCK
[cloned_group]
cm_1
cm_2
cm_3
# END ANSIBLE MANAGED BLOCK

Example of a complete playbook for testing

- hosts: localhost

  vars:

    my_file: "{{ ansible_inventory_sources.0 }}"
    my_groups: [cloned_group]
    my_inventory: |
      {% for group in my_groups %}
      [{{ group }}]
      {% for host in groups[group] %}
      {{ host }}
      {% endfor %}
      {% endfor %}

  tasks:

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

    - debug:
        var: groups.cloned_group

    - debug:
        var: ansible_inventory_sources
    - debug:
        var: my_file
    - debug:
        var: my_inventory

    - command:
        cmd: "cp {{ my_file }} {{ my_file }}.backup"
        creates: "{{ my_file }}.backup"

    - blockinfile:
        path: "{{ my_file }}"
        block: "{{ my_inventory }}"
added 346 characters in body
Source Link

The add_hostadd_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

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

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

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
Source Link

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

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