Launching an instance using Ansible is a convenient, platform agnostic method. While we have to specify how to do this for OpenStack, as a particular task, an Ansible playbook could be extended to allow a user to use the same Ansible command to launch an instance on any cloud. This recipe is a very basic introduction to the use of Ansible with OpenStack.
Ensure that you are logged on to a correctly configured OpenStack client and can access the OpenStack environment that has Ansible installed.
Ansible executes tasks in what is known as a playbook. In this example, we will create a simple task that launches a specific instance called cookbook1
:
launch-instance.yml
on your client, in a directory of your choosing:- name: Launch instance on OpenStack hosts: localhost gather_facts: false tasks: - name: Deploy an instance os_server: state: present name: cookbook1 image: xenial-image key_name: demokey timeout: 200 flavor: m1.tiny network: private-net verify: false
ansible-playbook
command, as follows:source openrc ansible-playbook launch-instance.yml
This will bring back the familiar Ansible output like the following:
openstack server list
This will bring back an output like the following:
Note that the task automatically assigned a public floating IP address from the GATEWAY_NET
network. This is an important detail as Ansible can do much more than just launch instances. If we want to be able to install and configure instances, Ansible must be able to SSH from the client to the running instance. Private tenant networks are generally not accessible; therefore, Ansible would use the public routed network to access the instance, just like you would if you were to SSH to it.
Launching an instance using Ansible uses the os_server
Ansible module. This is available from Ansible 2.0 onwards. The os_server
module takes a number of parameters that describe the usual parameters you would expect when launching instances using the command line.
Note that we didn't specify any authentication details as part of the task. This is because this module interprets our shell environment variables, just like we would use when executing OpenStack client tools.
You will notice that one of the entries in the task denotes the following:
os_server: state: present
This has a specific intent in Ansible, as Ansible is designed to give state consistency of running that task, despite how many times that task may get run. This simple statement basically says that this instance must be present. If not, it will launch that instance. Once it has launched, it satisfies that predicate. In the Ansible output, you will see that the overall run says ok=1 changed=1. This means that it changes the state of this environment. In other words, it launched the instance (which is a change of state).
However, if we run the task again, we get the following subtle change in the output denoting that the task didn't need to run to satisfy the fact the instance needed to be "present" (that is, running):
Note that the task executed successfully, but didn't need to change anything as denoted by the changed=0 output in the PLAY RECAP line.
source openrc
method, visit https://docs.openstack.org/shade/latest/ to set up a clouds.yaml
cloud environment file18.223.171.168