# Single cloudformation playbook for multiple regions

**URL:** <https://forum.ansible.com/t/single-cloudformation-playbook-for-multiple-regions/13025>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [June 27, 2013, 12:32am UTC](https://forum.ansible.com/t/single-cloudformation-playbook-for-multiple-regions/13025 "2013-06-27T00:32:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Boris2](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@Boris2](https://forum.ansible.com/u/Boris2)\
**Post date:** [June 27, 2013, 12:32am UTC](https://forum.ansible.com/t/single-cloudformation-playbook-for-multiple-regions/13025/1 "2013-06-27T00:32:09Z")

</div>

Hi,

I am new to ansible, so please forgive me if there is an obvious answer. Basically there is a same system setup - a couple of cloudformation stacks in each region and they are all the same, except region-specific variables - VPC, subnet IDs, etc.  
Can I make a single playbook that can be used for any AWS region, by passing the region parameter and collecting region-specific parameters in group variables or something?

Thanks,  
-Boris

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [June 27, 2013, 1:19am UTC](https://forum.ansible.com/t/single-cloudformation-playbook-for-multiple-regions/13025/2 "2013-06-27T01:19:18Z")

</div>

Sure thing. So most of the cloud provisioning modules in Ansible, ok, all of them, are generally executed in a block that looks like this:

- hosts: localhost  
connection: local  
tasks:

- …

- add\_host: … # add the hosts temporarily to inventory until the inventory script caching resets and picks them up, or alternatively, invalidate the cache…

- hosts: somegroup  
tasks:

- …

Which means to say it doesn’t need the hosts defined in inventory before it builds them. it’s something that runs on the local machine, talks to the cloud API, and makes machines exist.Thus, also with the cloud formation module, yes, I’d probably recommend passing in a parameter.

It doesn’t make sense to put these in inventory variables to me, but passing them in with “-e / --extra-vars” might be appropriate, or if you had seperate playbooks that used common includes you could perhaps even use a role or task parameter.

–Michael

---

<div class="post-metadata">

**Author:** ![James\_Martin2](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@James\_Martin2](https://forum.ansible.com/u/James_Martin2)\
**Post date:** [June 27, 2013, 2:46am UTC](https://forum.ansible.com/t/single-cloudformation-playbook-for-multiple-regions/13025/3 "2013-06-27T02:46:04Z")

</div>

Boris,

To be cloudformation specific, yes you can do just that. As long as your cloudformation template has parameters, you can pass them to the template. Here’s an example:

So lets say you have a groupvars/all file that looked something like this:

stacks:

- stack\_name: mona  
region: us-east-1  
instance\_type: m1.small
- stack\_name: lisa  
region: us-west-1  
instance\_type: m1.small

and a playbook that had the following task:

- name: launch stacks  
cloudformation:  
stack\_name={{ item.stack\_name }} state=present  
region={{ item.region }} disable\_rollback=yes wait\_for=yes  
template=mycfnstack.json  
args:  
template\_parameters:  
KeyName: “{{ item.key\_name }}”  
InstanceType: “{{ item.app\_disk\_type }}”  
with\_items: stacks

---

<div class="post-metadata">

**Author:** ![Boris2](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@Boris2](https://forum.ansible.com/u/Boris2)\
**Post date:** [June 27, 2013, 3:42am UTC](https://forum.ansible.com/t/single-cloudformation-playbook-for-multiple-regions/13025/4 "2013-06-27T03:42:53Z")

</div>

That makes sense, I try that. And how would I build up only a single region?

---

<div class="post-metadata">

**Author:** ![James\_Martin2](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@James\_Martin2](https://forum.ansible.com/u/James_Martin2)\
**Post date:** [June 27, 2013, 5:45pm UTC](https://forum.ansible.com/t/single-cloudformation-playbook-for-multiple-regions/13025/5 "2013-06-27T17:45:07Z")

</div>

> That makes sense, I try that. And how would I build up only a single  
> region?

Then you would only have one -stack\_name in the group\_vars/all:

&nbsp;&nbsp;stacks:  
&nbsp;&nbsp;- stack\_name: lisa  
&nbsp;&nbsp;&nbsp;&nbsp;region: us-west-1  
&nbsp;&nbsp;&nbsp;&nbsp;instance\_type: m1.small

- James

---

<div class="post-metadata">

**Author:** ![Boris2](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@Boris2](https://forum.ansible.com/u/Boris2)\
**Post date:** [June 28, 2013, 6:50pm UTC](https://forum.ansible.com/t/single-cloudformation-playbook-for-multiple-regions/13025/6 "2013-06-28T18:50:45Z")

</div>

Are there any playbook examples with multiple plays, one of which is cloudformation and others modifying instances provisioned by cloudformation?

I am trying to figure out how to add hosts provisioned by cloudformation, so that subsequent play can do post-provisioning configuration on them.

I think I can get this done with two playbooks - one with cloudformation task and using a file with localhost as inventory, and another one with ec2.py inventory script running tasks on a tagged group.  
But is there a way to have a single playbook doing both? I am not sure how to make each play use a separate inventory. May be I am going about it wrong.

Thanks,  
-Boris

---

<div class="post-metadata">

**Author:** ![James\_Martin2](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@James\_Martin2](https://forum.ansible.com/u/James_Martin2)\
**Post date:** [June 28, 2013, 8:15pm UTC](https://forum.ansible.com/t/single-cloudformation-playbook-for-multiple-regions/13025/7 "2013-06-28T20:15:44Z")

</div>

Boris,

I generally split them up into 2 separate runs of ansible-playbook. The first run will run the cloudformation plays, the second run will do the other work you want done on them. The reason behind this is that the inventory (ec2.py) will have no idea what the hosts are that you are operating on if you have only one ansible-playbook run.

ec2.py will generate group names based around various AWS items, including tags. More information can be found here:

[http://www.ansibleworks.com/docs/api.html/#example-aws-ec2-external-inventory-script](http://www.ansibleworks.com/docs/api.html/#example-aws-ec2-external-inventory-script)

- James
