xcp-ng / xcp

Entry point for issues and wiki. Also contains some scripts and sources.
https://xcp-ng.org
1.27k stars 74 forks source link

XCP-ng in Ansible working group #375

Open BerkhanBerkdemir opened 4 years ago

BerkhanBerkdemir commented 4 years ago

Hi there,

Thank you for this opportunity to make this turnkey solution available for everybody. We are not very big XCP-ng user but we rely on. In our infrastructure, we are managing host machines with Ansible and sometimes we have questions to ask. I would like to ask idea of creating Ansible working group about XCP-ng. So, people can ask questions, develop Ansible modules and share for public use. So far, 4 modules are related with XenServer.

I also want to add that we can use these modules to manage host/guest machines on XCP-ng with no issue :bow:

I would like to hear more from @stormi and @olivierlambert.

stormi commented 4 years ago

Hi. I'm not using it for now. Where and how would you like to create such a group? Within ansible user community? Within XCP-ng user community? If the latter, I simply suggest to create a dedicated thread on the forum to gather the users who manage their XCP-ng hosts with ansible.

beshleman commented 4 years ago

I would love to see more happening in the infrastructure as code / configuration management arena for XCP-ng. tuning in

olivierlambert commented 4 years ago

I think an even better opportunity would be to rely on Xen Orchestra as central point to manage everything:

BerkhanBerkdemir commented 4 years ago

I think an even better opportunity would be to rely on Xen Orchestra as central point to manage everything:

  • API is more clear than XAPI
  • you don't have to know XCP-ng specifics
  • you have a pool abstraction
  • it's easier to make XO API evolve than XAPI

Thank you for heads up. I love Xen Orchestra, but so far, I manage the old XenServer(s) with bash scripts. We are slowly migrating things to Ansible, Terraform and Packer for 6-7 months. Many plugins, providers and modules are either out of dated or no support at all. I still believe many XenServer users don't have XO installed in their pool.

I would love to see more happening in the infrastructure as code/configuration management arena for XCP-ng. tuning in

I knew I'm not the only one :laughing:

BerkhanBerkdemir commented 4 years ago

Hi. I'm not using it for now. Where and how would you like to create such a group? Within ansible user community? Within XCP-ng user community? If the latter, I simply suggest to create a dedicated thread on the forum to gather the users who manage their XCP-ng hosts with ansible.

Ansible is changing how module development works (stated in this document if curious). This is so new--a month old idea. Ansible calls collections to these vendor/community driven modules. I try my best to answer, as an Ansible and XCP-ng user, what I know about this change.

chenull commented 4 years ago

hi just want to share, i did a small hack for VM to be able to be network provisioned (ip, gateway, dns, etc) by ansible's xenserver_guest module using these hacks:

  1. prepare a VM to be created as a template (i did only test with centos 8 though)
  2. install xenguest tool inside the VM
  3. install nova-agent on the VM. more information below
  4. enable nova-agent service
  5. prepare a script to convert xenstore-data prepared by ansible xenserver_guest module to a format that rackspace's nova-agent will understand. example script below
  6. create a oneshot systemd service to start that script. this script must be run before the network interfaces up. example below
  7. disable the NetworkManager (centos/rhel) for the interface
  8. convert that VM to a template
  9. provision a VM using ansible using networks configuration. example below

how to install nova-agent

the required packages are:

for example:

yum install https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/l/libtomcrypt-1.18.2-5.el8.x86_64.rpm \
  https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/l/libtommath-1.1.0-1.el8.x86_64.rpm \
  https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/n/nova-agent-2.1.21-1.el8.noarch.rpm \
  https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/p/python3-pycryptodomex-3.9.7-1.el8.x86_64.rpm \
  https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/p/python3-pyxs-0.4.1-9.el8.noarch.rpm

example script to convert ansible metadata to rackspace metadata

#!/bin/bash
# /usr/local/sbin/ansible-init
# This script is a helper for ansible provisioned VM to be understood by rckspace nova-agent

PATH=/usr/libexec/xe-guest-utilities:$PATH

# set provider=rackspace
xenstore-write vm-data/provider_data/provider rackspace
# delete previous provisioned networking configs
xenstore-rm vm-data/networking

if ! (xenstore-exists vm-data/networks); then
  echo "No vm-data/networks"
  exit 0
fi

# read networks data provisioned by ansible
GATEWAYS=""
DNS="8.8.8.8 8.8.4.4"

NETWORKS=$(xenstore-list vm-data/networks)
for net in $NETWORKS; do
  name=$(xenstore-read vm-data/networks/$net/name 2>/dev/null)
  jiji=$(xenstore-read vm-data/networks/$net/jiji 2>/dev/null)
  gateway=$(xenstore-read vm-data/networks/$net/gateway 2>/dev/null)
  netmask=$(xenstore-read vm-data/networks/$net/netmask 2>/dev/null)
  prefix=$(xenstore-read vm-data/networks/$net/prefix 2>/dev/null)
  ip=$(xenstore-read vm-data/networks/$net/ip 2>/dev/null)
  type=$(xenstore-read vm-data/networks/$net/type 2>/dev/null)
  mac=$(xenstore-read vm-data/networks/$net/mac 2>/dev/null)
  M_A_C=$(echo $mac 2>/dev/null | tr [a-z] [A-Z])
  MAC=$(echo $M_A_C | tr -d :)
  GATEWAYS="$GATEWAY $gateway"

  # set vm-data/networking/MAC
  networking=$(cat <<- NETCONF
  '{\
  "dns":["8.8.8.8", "8.8.4.4"],\
  "gateway":"$gateway",\
  "label":"$name",\
  "mac":"$mac",\
  "ips":\
    [{"enabled":"1",\
      "gateway":"$gateway",\
      "ip":"$ip",\
      "netmask":"$netmask"\
    }]
   }'
NETCONF
)
  eval xenstore-write vm-data/networking/$MAC $networking

done

# tell nova-agent to do network restart
xenstore-write data/host/658556C5-6EC5-4949-895E-ADEA5BA1AD29 '{"name":"resetnetwork","value":""}'

example oneshot systemd service / kinda like old school rc.local

[Unit]
Description=Helper for ansible provisioned VM to enable rackspace like VM initialization
ConditionVirtualization=xen
After=syslog.target xe-linux-distribution.target
Before=nova-agent.target

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/ansible-init

[Install]
WantedBy=multi-user.target

example ansible playbook

---
- name: create testvm_1
  hosts: localhost
  gather_facts: no
  tasks:
    - name: Test Deploy
      delegate_to: localhost
      xenserver_guest:
        hostname: "https://sriwijaya"
        username: "root"
        password: "password"
        validate_certs: no
        name: testvm_1
        state: poweredon
        linked_clone: yes
        template: centos8-dev
        hardware:
          num_cpus: 2
          memory_mb: 2048
        networks:
          - name: LAN
            type: static
            ip: 192.168.1.118/24
            gateway: 192.168.1.247

when the VM up, it will have 192.168.1.118 as IP address

BerkhanBerkdemir commented 4 years ago

That's a wonderful example @chenull. Loved it. Also, consider to use ansible-pull when you first time provision the guest machine.

chenull commented 4 years ago

That's a wonderful example @chenull. Loved it. Also, consider to use ansible-pull when you first time provision the guest machine.

oh.. i didn't know about ansible-pull. thank you. but i assume the netwok should've been initialized first using something like dhcp.

olivierlambert commented 4 years ago

Thank you for heads up. I love Xen Orchestra, but so far, I manage the old XenServer(s) with bash scripts. We are slowly migrating things to Ansible, Terraform and Packer for 6-7 months. Many plugins, providers and modules are either out of dated or no support at all. I still believe many XenServer users don't have XO installed in their pool.

Xen Orchestra is becoming the standard way to manage pools. It's also capable of doing stuff XAPI can't directly because of its design (see https://xen-orchestra.com/docs/architecture.html)

That's why I think it might be better to use it. We also had discussion with other "layers" to work through XO instead of managing each pool individually.

But anyway, it's not incompatible. However, ideally, I'd like to have community efforts focused on the same thing rather individual work at various place. But it's entirely up to you!