Creating your first machine

Since OpenStack is very flexible, many of its components can have many different implementations, which means they may differ slightly in terms of their behavior. To be able to accommodate all the various cases, the Ansible modules that manage OpenStack tend to have a lower level of abstraction compared to the ones for many public clouds.

So, to create a machine, you will need to ensure that the public SSH key is known to OpenStack and ensure that the OS image is present as well. After doing this, you can set up networks, subnetworks, and routers to ensure that the machine we are going to create can communicate via the network. Then, you can create the security group and its rules so that the machine can receive connections (pings and SSH traffic, in this case). Finally, you can create a machine instance.

To complete all the steps we've just described, you need to create a file called openstack.yaml with the following content:

---
- hosts: localhost
tasks:
- name: Ensure the SSH key is present on OpenStack
os_keypair:
state: present
name: ansible_key
public_key_file: "{{ '~' | expanduser }}/.ssh/id_rsa.pub"
- name: Ensure we have a CentOS image
get_url:
url: http://cloud.centos.org/centos/8/x86_64/images/CentOS-8-GenericCloud-8.1.1911-20200113.3.x86_64.qcow2
dest: /tmp/CentOS-8-GenericCloud-8.1.1911-20200113.3.x86_64.qcow2
- name: Ensure the CentOS image is in OpenStack
os_image:
name: centos
container_format: bare
disk_format: qcow2
state: present
filename: /tmp/CentOS-8-GenericCloud-8.1.1911-20200113.3.x86_64.qcow2
- name: Ensure the Network is present
os_network:
state: present
name: mynet
external: False
shared: False
register: net_out
- name: Ensure the Subnetwork is present
os_subnet:
state: present
network_name: "{{ net_out.id }}"
name: mysubnet
ip_version: 4
cidr: 192.168.0.0/24
gateway_ip: 192.168.0.1
enable_dhcp: yes
dns_nameservers:
- 8.8.8.8
- name: Ensure the Router is present
os_router:
state: present
name: myrouter
network: nova
external_fixed_ips:
- subnet: nova
interfaces:
- mysubnet
- name: Ensure the Security Group is present
os_security_group:
state: present
name: mysg
- name: Ensure the Security Group allows ICMP traffic
os_security_group_rule:
security_group: mysg
protocol: icmp
remote_ip_prefix: 0.0.0.0/0
- name: Ensure the Security Group allows SSH traffic
os_security_group_rule:
security_group: mysg
protocol: tcp
port_range_min: 22
port_range_max: 22
remote_ip_prefix: 0.0.0.0/0
- name: Ensure the Instance exists
os_server:
state: present
name: myInstance
image: centos
flavor: m1.small
security_groups: mysg
key_name: ansible_key
nics:
- net-id: "{{ net_out.id }}"

Now, you can run it, as follows:

$ ansible-playbook openstack.yaml

The output should be as follows:

PLAY [localhost] **********************************************************************************

TASK [Gathering Facts] ****************************************************************************
ok: [localhost]

TASK [Ensure the SSH key is present on OpenStack] *************************************************
changed: [localhost]

TASK [Ensure we have a CentOS image] **************************************************************
changed: [localhost]

TASK [Ensure the CentOS image is in OpenStack] ****************************************************
changed: [localhost]

TASK [Ensure the Network is present] **************************************************************
changed: [localhost]

TASK [Ensure the Subnetwork is present] ***********************************************************
changed: [localhost]

TASK [Ensure the Router is present] ***************************************************************
changed: [localhost]

TASK [Ensure the Security Group is present] *******************************************************
changed: [localhost]

TASK [Ensure the Security Group allows ICMP traffic] **********************************************
changed: [localhost]

TASK [Ensure the Security Group allows SSH traffic] ***********************************************
changed: [localhost]

TASK [Ensure the Instance exists] *****************************************************************
changed: [localhost]

PLAY RECAP ****************************************************************************************
localhost : ok=11 changed=10 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

As you can see, this process was longer than the public cloud ones we covered. However, you did get to upload the image that you wanted to run, which is something many clouds do not allow (or allow with very complex processes).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset