Use ansible to create a virtual machine

The goal is to automate the creation of a virtual machine and run a script at startup. Could be used for CI/CD, deployment of application… This was done with ansible 2.9.17 and python3-openstacksdk-0.46.0-2 (minimal : openstacksdk, openstacksdk >= 0.12.0, python >= 2.7)

Create first a files clouds.yaml with in :

       auth_url: ""
       project_name: $(Your gandi v5 username or Organisation)
       username: $(Your gandi v5 username)
       password: $(Your gandi v5 password)
       user_domain_name: public
       project_domain_name: public
     interface: "public"
     identity_api_version: 3

This file will be used to be authenticated. This file must be in the current folder where you store your playbook.
For the ansible playbook as playbook.yaml, create a new file as following

 - hosts: localhost
     insname: test03
   - name: start an instance
       key_name: MyAwesomeKey
       name: "{{ insname }}"
       state: present
       cloud: gandi
       image: Debian 10 Buster
       flavor: "gos-4"
       network: "public"
       boot_from_volume: true
       volume_size: 5
       terminate_volume: yes
       userdata: |
         {%- raw -%}#!/bin/bash
         apt update ; apt upgrade -y
         apt install  -y wordpress #to be continued...
         {% endraw %}

Of course you can change flavor, volume_size, image based and the final script.
The run now :

ansible-playbook playbook.yaml 

Result expected :

  PLAY [localhost] *****************************************************************************************************************************************************************************
  TASK [Gathering Facts] ***********************************************************************************************************************************************************************
  ok: [localhost]           
  TASK [start an instance] *********************************************************************************************************************************************************************
  changed: [localhost]                                                                                                                                                                          
  PLAY RECAP ***********************************************************************************************************************************************************************************
  localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

To find public IPs setup :

   openstack --os-cloud gandi server list
  | ID                                   | Name    | Status | Networks                                                    | Image | Flavor |
  | c18a3488-4f71-43cc-9864-20310dc15809 | test03  | ACTIVE | public=4x.2xx.1xx.2xx, 2001:4b98:dc0:43:xxxx:xxxx:xxxx:xxxx |       | gos-4  |
1 Like

Thanks for sharing this short but useful guide :slight_smile: