Search code examples
ansibleansible-2.xansible-inventory

Can I dump the inventory to a remote machine in a task?


I have an Ansible role where I need to create an inventory file on the remote machine. I want to use the current inventory file, but with one single group removed.

For example, if I have the following inventory:

[operator]
host1.example.com

[control]
host2.example.com
host3.example.com

[compute]
host4.example.com some_var=value
host5.example.com

# some comment
[foo:children]
control
compute

[bar:vars]
var1=value
#var2=value

Then I need something that copies this:

- hosts: operator
  tasks:
    - name: Create inventory
      ...

With the following result on host1.example.com:

[control]
host2.example.com
host3.example.com

[compute]
host4.example.com some_var=value
host5.example.com

# some comment
[foo:children]
control
compute

[bar:vars]
var1=value
#var2=value

Solution

    1. Split the current inventory file into two:

      • one containing the operator group only,
      • one containing the rest.
    2. Copy only the latter to the target system.


    You know that you can point to a directory containing multiple files as your inventory, right?