# Ansible For Openstack

**URL:** <https://forum.ansible.com/t/ansible-for-openstack/30913>\
**Category:** Ansible Project\
**Created:** [July 31, 2019, 7:36am UTC](https://forum.ansible.com/t/ansible-for-openstack/30913 "2019-07-31T07:36:51Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [July 31, 2019, 7:36am UTC](https://forum.ansible.com/t/ansible-for-openstack/30913/1 "2019-07-31T07:36:51Z")

</div>

Hi All,  
I am trying to create Openstack instances using Ansible for Openstack.  
I have clouds.yaml as below:  
clouds:  
mycloud:  
auth:  
auth\_url: https://:/v3  
project\_name: mycloud  
username: mycloud  
project\_domain\_name: Default  
user\_domain\_name: Default  
password: mycloudpwd  
region\_name: regionOne  
interface: public  
identity\_api\_version:  
and Main Playbook is as below:

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [July 31, 2019, 9:26am UTC](https://forum.ansible.com/t/ansible-for-openstack/30913/2 "2019-07-31T09:26:25Z")

</div>

You are using the literal string 'cloud'.  
Change that entry in the os\_network task to:

cloud: "{{ mycloud }}"

---

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [July 31, 2019, 10:04am UTC](https://forum.ansible.com/t/ansible-for-openstack/30913/3 "2019-07-31T10:04:19Z")

</div>

Thanks Dick. It worked, I forgot this by mistake.

---

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [August 1, 2019, 8:03am UTC](https://forum.ansible.com/t/ansible-for-openstack/30913/4 "2019-08-01T08:03:05Z")

</div>

sorry to come back Dick. It is still not working from clouds.yaml  
“msg”: “The task includes an option with an undefined variable. The error was: ‘mycloud’ is undefined\n\nThe error appears to be in ‘/root/tas/simpledemo/Main-Playbook.yaml’: line 5, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: create network\n ^ here\n”

Though i defined: cloud: “{{ mycloud }}”

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [August 1, 2019, 8:20am UTC](https://forum.ansible.com/t/ansible-for-openstack/30913/5 "2019-08-01T08:20:39Z")

</div>

Actually, it's this:

cloud: "{{ clouds.mycloud }}"

---

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [August 1, 2019, 8:25am UTC](https://forum.ansible.com/t/ansible-for-openstack/30913/6 "2019-08-01T08:25:44Z")

</div>

Thanks dick but now it is complaining clouds is undefined.😔

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [August 1, 2019, 9:47am UTC](https://forum.ansible.com/t/ansible-for-openstack/30913/7 "2019-08-01T09:47:57Z")

</div>

You should actually include clouds.yaml in the main playbook.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Meyer](https://avatars.discourse-cdn.com/v4/letter/s/779978/32.png) [@Sebastian\_Meyer](https://forum.ansible.com/u/Sebastian_Meyer)\
**Post date:** [August 1, 2019, 10:07am UTC](https://forum.ansible.com/t/ansible-for-openstack/30913/8 "2019-08-01T10:07:11Z")

</div>

Hi Rahul,

> fatal: [localhost]: FAILED! =\> {"changed": false, "msg": "Cloud mycloud was  
> not found."}.  
> Notice clouds.yaml and Main Playbook is in same location.

from the module docs:

> Auth information is driven by openstacksdk, which means that values

can come from a yaml config file in /etc/ansible/openstack.yaml,  
/etc/openstack/clouds.yaml or ~/.config/openstack/clouds.yaml

Sebastian

---

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [August 1, 2019, 10:19am UTC](https://forum.ansible.com/t/ansible-for-openstack/30913/9 "2019-08-01T10:19:41Z")

</div>

Thanks Sebastien and Dick.  
Yes , clouds.yaml can be kept in /etc/openstack/clouds.yaml or ~/.config/openstack/clouds.yaml but not in current  
working directory (where playbook resides).  
Also we can reference the named cloud directly and no need of clouds.mycloud variables etc.  
I directly used like cloud: mycloud and it worked while keeping my clouds.yaml in /etc/openstack/clouds.yaml

My complete instance got created (with network ,subnet,SG,Router etc).

I have below question to both experts dick and sebastien .

1. whatever resources(net,subnet,sg,router etc) are provisioned as part of this single instance creation , lets say i want to clear all those resources in one shot , how can i do this ?
2. Does order of tasks matter in playbook. For example: To create instance , we need network , so first we will provision network and then other dependent resources.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Meyer](https://avatars.discourse-cdn.com/v4/letter/s/779978/32.png) [@Sebastian\_Meyer](https://forum.ansible.com/u/Sebastian_Meyer)\
**Post date:** [August 1, 2019, 10:36am UTC](https://forum.ansible.com/t/ansible-for-openstack/30913/10 "2019-08-01T10:36:12Z")

</div>

Hi Rahul,

> 1. whatever resources(net,subnet,sg,router etc) are provisioned as part of  
> this single instance creation , lets say i want to clear all those  
> resources in one shot , how can i do this ?

Each module (os\_server, os\_network ...) should only create the specified  
resource. For deprovisioning set the state to absent. One exception  
would be e.g. a floating IP automatically assigned to the instance. That  
IP isn't assigned anymore afterwards, but still created.

> 2. Does order of tasks matter in playbook. For example: To create instance  
> , we need network , so first we will provision network and then other  
> dependent resources.

Order matters, yes. Playbooks are run from top down, so the first task  
in the list is executed first. Beware that you need a different order  
for creation and destruction, afaik you cant remove a network, if theres  
still an instance on it.

---

<div class="post-metadata">

**Author:** ![Rahul\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rahul\_Kumar1](https://forum.ansible.com/u/Rahul_Kumar1)\
**Post date:** [August 1, 2019, 10:49am UTC](https://forum.ansible.com/t/ansible-for-openstack/30913/11 "2019-08-01T10:49:34Z")

</div>

Thanks much Sebastien,  
Good inputs.  
I tried same playbook by making state = absent and got error as expected as below:  
fatal: [localhost]: FAILED! =\> {“changed”: false, “msg”: “ConflictException: 409: Client Error for url: https://:/v2.0/networks/c4e67037-e32d-4e06-b0ba-7c07f6e1ec63.json, Unable to complete operation on network c4e67037-e32d-4e06-b0ba-7c07f6e1ec63. There are one or more ports still in use on the network.”}

So again some questions:

1. Do we need to write different playbook for deleting resources or either flip the order in the playbook as needed and make state=absent?
2. I tried this just to create 1 nova instnace . Assume we have multiple resources and we need to make sure right resource is attached to particular one.  
For example:  
Assume 2 resources:

- name: Create Networks  
os\_network:  
name: “{{ [item.name](http://item.name) }}”  
state: present  
external: false  
wait: yes  
with\_items: “{{ networks }}”

os\_server:  
name: “{{ prefix }}-{{ [item.name](http://item.name) }}”  
state: present  
key\_name: “{{ item.key }}”  
availability\_zone: “{{ item.availability\_zone }}”  
nics: “{{ item.nics }}”  
image: “{{ item.image }}”  
flavor: “{{ item.flavor }}”  
with\_items: “{{ servers }}”  
register: “os\_hosts”

Assume network is need to provision instance . Now say we have multiple instances and multiple networks .Which we can define using networks and servers variables as shown in above snippet.  
How do i ensure that one particular network is assigned to particular instance? do we need to manage this at  
variables side defination only or is there any better way to handle same in playbook code?

Its just scaling use case when we have multiple resources and we want that expected resource is attached to right one.
