ansibleansible-inventory

Different inventories, same group name


I want to run an Ansible playbook with more inventories and I know it can be done by specifying before each inventory -i command. But I have in two different inventories a group with the same name, but different hosts in the group. Is it possible to target all the machines from both inventories without modifying the inventories?

I couldn't try it because the machines are not available right now, I would like to know if it is possible


Solution

  • Q: "I have in two different inventories a group with the same name, but different hosts in the group."

    For example inventory files like

    example.com.ini

    [test]
    one.example.com
    two.example.com
    

    and

    example.net.ini

    [test]
    one.example.net
    two.example.net
    

    Q: "Is it possible to target all the machines from both inventories without modifying the inventories?"

    Yes, of course, since a minimal example playbook example.ini.yml

    ---
    - hosts: test
      become: false
      gather_facts: false
    
      tasks:
    
        - debug:
    

    called via

    ansible-playbook -i example.com.ini -i example.net.ini example.ini.yml
    

    will result into an output of

    TASK [debug] ********************
    ok: [one.example.com] =>
      msg: Hello world!
    ok: [two.example.com] =>
      msg: Hello world!
    ok: [one.example.net] =>
      msg: Hello world!
    ok: [two.example.net] =>
      msg: Hello world!
    
    PLAY RECAP **********************
    one.example.com            : ok=1
    one.example.net            : ok=1
    two.example.com            : ok=1
    two.example.net            : ok=1