Automate OpenStack using Ansible

Automate OpenStack using Ansible
Photo by 辰曦 / Unsplash

Flexibility is key in this time and Age, In my previous submission I've talked about how to manage OpenStack using Terraform, but most use case we really don't need a blueprint of our infrastucture and maintaining a state really takes a toll on how people and organization manages their infrastructure.

If the people manages infrastructure as comodities and wants to have a documment or blueprint of your Infra, IaC with a state would really good for this use case which is like Terraform check out my previous entries about managing your OpenStack using Terraform, but if the use case is more organic in such as way that provisioning the infrastructure like a timed resource and delete it with a schedule that is where a Declarative language such as Ansible comes to the picture.

The use cases of using Ansible instead of Terraform:

1.) Test environment (which is timed and can be deleted after declared usage)

2.) Training environments (which is given to users for a specific ammount of time and destroy)

3.) Moving from a Manual Infra to an Automated Infrastructure (Moving to Terraform will be a hard task especially on how Terraform will map your existing infra thus highly recommend your IaC journey to Ansible)

So if you are like me who uses his infrastructure as a teaching environment Ansible is a good tool for you.

Installation of Ansible and OpenStack SDK

Jumpstarting we need to install Ansible and its SDK

You need to have docker or podman installed

for starters what I use is the pip installation of ansible,

[aj@opensource]# python3 -m pip install ansible-navigator --user

ansible-navigator is a new command that is used by AWX or Red Hat Ansible Automation Platform to run Ansible playbooks, because unlike the previous ansible-playbook or ansible addhoc commands, it creates a container that we call and execution environment, the execution environment helps you to divide your libraries of running your Ansible playbooks.

you need to build your exeecution environment with an openstacksdk, there is an article by Gineesh Madapparambath on how to create an execution environment

After creating you can use it to run your to configure your image use

[aj@opensource]# ansible-navigator images

to choose which image to use then run a playbook

[aj@opensource]# ansible-navigator run main.yaml --stdout

Sample Playbook

For a sample playbook I have prepared the same structure with what I used in Terraform:

[aj@opensource]# cat >> main.yaml << EOF
---
- hosts: localhost
  vars:
    flavors:
      - name: "small"
        ram: 4096
        vcpus: 1
      - name: "medium"
        ram: 8096
        vcpus: 2
      - name: "large"
        ram: 16384
        vcpus: 4
      - name: "xlarge"
        ram: 32768
        vcpus: 8

  tasks:
  - name: create flavors
    openstack.cloud.compute_flavor:
      state: present
      name: "{{ item.name }}"
      ram: "{{ item.ram }}"
      vcpus: "{{ item.vcpus }}"
      disk: 0
    loop: 
      - flavors

  - name: create external network
    openstack.cloud.network:
      state: present
      name: "external-network"
      provider_network_type: "flat"
      provider_physical_network: "physnet1"
      external: true

  - name: create external subnet
    openstack.cloud.subnet:
      state: present
      name: "external-subnet"
      network_name: "external-network"
      cidr: "10.0.0.0/8"
      gateway_ip: "10.0.0.1"
      dns_nameservers:
        - "10.0.0.254"
        - "10.0.0.253"
      allocation_pool_start: "10.0.0.2"
      allocation_pool_end: "10.0.254.254"

  - name: create external router
    openstack.cloud.router:
      name: "external-router"
      state: present
      network: "external-network"

  - name: create Cirros image
    openstack.cloud.image:
      name: cirros
      container_format: bare
      disk_format: qcow2
      state: present
      filename: cirros-0.6.1-x86_64-disk.img

  - name: create Demo project
    openstack.cloud.project:
      state: present
      name: "Demo"
      enabled: True

  - name: create demo-user
    openstack.cloud.user:
      state: present
      name: "demo-user"
      password: "demo"
      default_project: "Demo"
EOF

Results

By running the playbook this are the results:

Networks
Networks

Flavors
Flavors

Images
Images

Project
Project

Your thoughs ?

What are your thoughts ? If you missed my article about Managing OpenStack using Terraform to see it click in this [link]https://ajohnsc.com/terraform-to-manage-openstack//), What do you preffer Ansible or Terraform ? Let me know your thoughts.