Using Ansible and AWX to deploy VMs on Xenserver
This article builds upon my previous one, “Using Ansible to automate VM creation on XenServer.” This time we deploy Ansible AWX (Open Source version of Ansible Tower) to docker via Docker Compose, and use the playbook we created last time to build an “automation portal” for deploying new VM’s on Xenserver.
We start by creating a docker-compose.yml file, for running AWX in containers on a local Docker instance. As this is just a test/POC, we use local volumes. This docker compose file can easily be changed to be deployed on Rancher fx.
Next, we just start the AWX service stack, with:
docker-compose up -d
It takes a while for the AWX stack to start, as it has to download all the docker images, start the PostgreSQL server, start the containers and load in the database scheme. Once its finished, you can access the web interface on http://localhost and you will be met by a login dialog:
After you’ve logged in the first thing to do is create a new project. Go to Projects and create a new one. Here, I’ve named it Xenserver VM Deploy, and chosen the Git SCM type, added the URL of the playbook repo from GitHub, set some SCM Update Options and a cache timout of 4 hours.
Next up we need a credential for connecting to the Xenservers. Here you have to navigate to Credentials and create a new one. Choose Machine as the credential type (SSH key) and press Select.
Now you have to provide the private key, of a keypair that has full access to your Xenservers. You can choose to have AWX prompt you for the passphrase for the private key on job launch, or type it in at let AWX save it in encrypted form in the database. Save the key.
When it’s saved, you can’t see or copy the private key anymore, only replace it.
Now we have to create an inventory of Xenservers to deploy to. As you may recall, our Github repo actually contains an inventory file that we’ll use, directly from the project. Go to Inventories and create a new one, give it a name and save it.
Then go to the Sources tab and press Add Source.
Then give Inventory souce a name, choose Sourced from a Project, the Project to source from (the project that we created earlier) and the specific file to use as inventory file (here we use the hosts file). Then choose Update on Project Change and press Save.
We now have to create a new Job template. Go to Job Templates and press Add->New Job Template. Give the new job template a name, choose Run as Job Type, the inventory we created earlier, the project we created earlier and which playbook to run. Then select the credential we’ve created, set the options you want and press Save.
Note: I had problems with AWX not recognising that I’d chosen a credential for the job. To solve this, I checked the Prompt on Launch checkbox besides the Credential box. This let’s you choose which credential you want to use, when running the job. More on that later.
To ease the deployment process, especially for non-tech savvy people, we’ll create a survey, that asks for input from the user running the job. Click on Add Survey.
We only add the fields that is required for deploying af VM using this playbook. Add a survey prompt for the Hostname. Press Add.
Add a survey prompt for the Domain Name. Press Add.
Add a survey prompt for the Gateway. Press Add.
Add a survey prompt for the VM Name. Press Add.
Add a survey prompt for the IP Address. Press Add.
Add a survey prompt for the VM Type. Press Add.
Add a survey prompt for the Storage Repository. Adjust the choices to match the different types you have on your servers — this has to be the exact names of the SR’s. Press Add.
Add a survey prompt for the DNS Servers. Press Add.
Add a survey prompt for the VM Template. Again, these template names should match the names of the templates on your servers exactly. Press Add.
Now save the whole survey. Press Save in the bottom right corner.
Now you can run the job from the Portal Mode. Just click the Rocket icon to the right of the Job template name.
Because of the problem I explained earlier with the credentials, we now have to choose a credential from a list. Choose one and press Next.
Then we have to choose the Xenserver to deploy to. This list is pulled from the inventory file (hosts file). Choose a Xenserver and press Next.
We’re now presented with the survey that we’ve created earlier. If you’ve entered sane defaults for everything, you can just press launch, but I think as a minimum you have to change the IP address for the new VM, after which you can press Launch.
And now your play should run, and you should end up with a fresh, new VM.
Enjoy!