XCP-ng
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    XO Ansible Dynamic Inventory Plugin - Group Variables Support?

    Scheduled Pinned Locked Moved Solved Xen Orchestra
    5 Posts 4 Posters 963 Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G Offline
      guiand888
      last edited by

      Hello!

      I'm working with the community.general.xen_orchestra dynamic inventory plugin and trying to implement group-specific variables through the tagging function.

      For instance, here's an example of a static inventory I currently have. Nothing fancy.

      gp1:
        vars:
          ansible_user: admin
        hosts:
          server1.example.com:
          server2.example.com:
      gp2:
        vars:
          ansible_user: anotheradmin
        hosts:
          server3.example.com:
          server4.example.com:
      

      The following dynamic inventory configuration works fine:

      plugin: community.general.xen_orchestra
      api_host: xo.example.com
      user: admin
      password: mypass
      validate_certs: true
      use_ssl: true
      groups:
          gp1: "'gp1' in tags"
          gp2: "'gp2' in tags"
      

      But adding group vars breaks it:

      
      groups:
          gp1: "'gp1' in tags"
              vars:
                  ansible_user: admin
          gp2: "'gp2' in tags"
              vars:
                  ansible_user: anotheradmin
      

      I'm wondering if the plugin supports adding group-specific variables directly in the dynamic inventory configuration.

      Would appreciate hearing if anyone has made this work and the most elegant and supported solution. I looked extensively through the forum, blog posts, etc. and could not figure it out.

      Thanks in advance for your help!

      nathanael-hN S 2 Replies Last reply Reply Quote 0
      • olivierlambertO Offline
        olivierlambert Vates 🪐 Co-Founder CEO
        last edited by

        Adding @shinuza and @nathanael-h in the loop

        1 Reply Last reply Reply Quote 0
        • nathanael-hN Offline
          nathanael-h Vates 🪐 DevOps Team @guiand888
          last edited by

          Hello @guiand888

          You could use compose in your inventory file like this:

          plugin: community.general.xen_orchestra
          api_host: xo.example.com
          user: admin
          password: mypass
          validate_certs: true
          use_ssl: true
          groups:
              gp1: "'gp1' in tags"
              gp2: "'gp2' in tags"
          compose:
            ansible_user: "'admin' if 'gp1' in tags else 'anotheradmin' if 'gp2' in tags"
          

          I made some local tests, this should do what you need. Maybe @shinuza has a better suggestion for writing the Jinja2 expression in the compose key.

          1 Reply Last reply Reply Quote 1
          • S Offline
            shinuza @guiand888
            last edited by

            @guiand888 Hey, the plugin does support composite vars. However I believe the idiomatic way to do what you want is this:

            group_vars/
              gp1.yml
              gp2.yml
            

            And then:

            # gp1.yml
            ansible_user: admin
            
            # gp2.yml
            ansible_user: anotheradmin
            
            1 Reply Last reply Reply Quote 2
            • G Offline
              guiand888
              last edited by

              Many thanks @nathanael-h & @shinuza , both approaches indeed work!

              1 Reply Last reply Reply Quote 3
              • olivierlambertO olivierlambert marked this topic as a question on
              • olivierlambertO olivierlambert has marked this topic as solved on

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              • First post
                Last post