XCP-ng

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups

    Ansible with Xen Orchestra

    News
    4
    6
    842
    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.
    • olivierlambert
      olivierlambert Vates 🪐 Founder & CEO 🦸 last edited by

      VirtOps #3: Ansible with Xen Orchestra

      With the release of Ansible Community 4.1.0 came a new inventory plugin for Xen Orchestra. This plugin allows the listing and grouping of XOA virtual machines, hosts and pools.

      For more details, read the blog post: https://xen-orchestra.com/blog/virtops3-ansible-with-xen-orchestra

      Your feedback

      Test it, comment it, ask for features, this is the place!

      D 1 Reply Last reply Reply Quote 0
      • D
        d1rtym0nk3y @olivierlambert last edited by

        Looks interesting!

        We started using the xo terraform provider around 12 months ago, and then built a small http service (node/typescript) that talks to the xo-api to generate our ansible inventory. We've been using both in production since then, and i'll share some of the details for you here.

        We took the approach of implementing this as a service on our network and then leveraging ansible's ability to execute a shell script to retrieve the inventory.

        In our environment, we decided it was ok for the inventory to only include vm's (or hosts) that have an ip address - i mean if they don't ansible can't really work with them so thats ok for us. So the inventory service has a couple of env vars to provide a filter for which entities and ips to pick

         // no tag required by default 
         required_tag: env.get('REQUIRED_TAG').default('').asString(),
         // any ip is valid for the inventory
         management_subnet: env.get('MANAGEMENT_SUBNETS').default('0.0.0.0/0').asArray(),
        

        First off we can require any vm or host to have a tag, e.g. ansible_managed:true to appear in the inventory
        Then it must have an ip in our management subnet, if more than one ip is available (e.g. management and public) the service will filter them.

        The http api for the inventory service uses the same filtering as xen-orchestra, so we can construct urls to retrieve partial inventories. This is useful for example as we have dev, production, etc, pools, and it gives us an easy way to target

        https://inventory.internal/inventory?filter=env:monitoring%20mytag:foo
        

        The response for the above request would look like this

        {
           "all":{
              "hosts":[
                 "monitoring-1.internal"
              ]
           },
           "_meta":{
              "hostvars":{
                 "monitoring-1.internal":{
                    "mytag":"foo",
                    "ansible_group":"prometheus",
                    "env":"monitoring",
                    "inventory_name":"monitoring-1.internal",
                    "ansible_host":"10.0.12.51",
                    "xo_pool":"monitoring-pool",
                    "xo_type":"VM",
                    "xo_id":"033f8b6d-88e2-92e4-3c3e-bcaa01213772"
                 }
              }
           },
           "prometheus":{
              "hosts":[
                 "monitoring-1.internal"
              ]
           }
        }
        

        This vm has these tags in xen orchestra
        53a5eb09-b946-4fd3-b288-2d7eb780b14d-image.png

        ansible_group can be repeated, and places the vm/host into this group in the inventory. Other tags get split into key=value and placed into the host vars

        the xo_* are added from the info in the api
        ansible_host will be our management ip
        inventory_name is a slugified version of the vm name, but by convention our names are sane

        We also include hosts in the inventory, as we have various playbooks to run against them. All the same tagging and grouping applies to hosts as it does to VM's

        {
           ...
              "hostvars":{
                 "xcp-001":{
                    "ansible_group":"xen-host",
                    "inventory_name":"xcp-001",
                    "ansible_host":"10.0.55.123",
                    "xo_pool":"monitoring-pool",
                    "xo_type":"host",
                    "xo_id":"92c1c2ab-fd1e-46e9-85f7-70868f1e9106",
                    "xo_version":"8.2.0",
                    "xo_product":"XCP-ng"
                 }
              }
           ...
        }
        

        When we setup some infra for management by terraform/ansible we'll typically use a combination of shell script inventory, static grouping and then extra group_vars if needed. For example our /inventory directory

        01_inventory.sh

        #!/bin/bash
        curl -k https://inventory.internal/inventory?filter=k8s-cluster:admin 2>/dev/null
        

        02_kubespray - which has its own group name convention, so we map them between our tags and their group names

        [kube-master:children]
        k8s-master
        
        [etcd:children]
        k8s-master
        
        [kube-node:children]
        k8s-node
        k8s-monitoring
        
        [k8s-cluster:children]
        kube-master
        kube-node
        

        Executing ansible-playbook -i /inventory where /inventory is a directory will then combine all the shell scripts and ini files to make the final inventor . nice!

        I did think about trying to package this api directly as a plugin for xo, but haven't had time to look into that yet. But let me know if any of this looks interesting.

        1 Reply Last reply Reply Quote 0
        • S
          shinuza last edited by

          Hi there. Author of said inventory plugin.

          If you ever wish to migrate you should be able to retain most of what you did on XOA side (I'm thinking tags), but you'll have something that's more standard and require less setup as long as the XOA API is accessible to the machine running the playbook.

          You can keep the groups with the composable groups in your inventory plugin configuration:

          simple_config_file:
              plugin: community.general.xen_orchestra
              api_host: 192.168.1.255
              user: xo
              password: xo_pwd
              validate_certs: true
              use_ssl: true
              groups:
                  kube-master: "name_label == 'kube-master'"
              compose:
                  ansible_port: 2222
          

          https://docs.ansible.com/ansible/devel/collections/community/general/xen_orchestra_inventory.html#ansible-collections-community-general-xen-orchestra-inventory

          1 Reply Last reply Reply Quote 1
          • wowi42
            wowi42 last edited by

            Hey,

            I'm using this plugin, and I spent a few minutes (around 30 minutes) to find the issue:

            pip3 install websocket-client
            

            Could be nice to add it in the doc/article.

            Regards

            S 1 Reply Last reply Reply Quote 1
            • olivierlambert
              olivierlambert Vates 🪐 Founder & CEO 🦸 last edited by

              Nice catch, let me ping @shinuza so he can fix the doc 👍

              1 Reply Last reply Reply Quote 0
              • S
                shinuza @wowi42 last edited by olivierlambert

                @wowi42 Hi there. It's already in the documentation:

                https://docs.ansible.com/ansible/devel/collections/community/general/xen_orchestra_inventory.html#requirements

                Also, you should see an error message if it's not installed.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post