Single cloudformation playbook for multiple regions

612 views
Skip to first unread message

Boris

unread,
Jun 26, 2013, 8:32:09 PM6/26/13
to ansible...@googlegroups.com
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

Michael DeHaan

unread,
Jun 26, 2013, 9:19:18 PM6/26/13
to ansible...@googlegroups.com
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





--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

James Martin

unread,
Jun 26, 2013, 10:46:04 PM6/26/13
to ansible...@googlegroups.com
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


This would loop through the stacks and launch each one.  

- James

On Wed, Jun 26, 2013 at 8:32 PM, Boris <boris_e...@intuit.com> wrote:
Message has been deleted

Boris

unread,
Jun 26, 2013, 11:42:53 PM6/26/13
to ansible...@googlegroups.com
That makes sense, I try that. And how would I build up only a single region?

James Martin

unread,
Jun 27, 2013, 1:45:07 PM6/27/13
to ansible...@googlegroups.com
On Wed, Jun 26, 2013 at 11:42 PM, Boris <boris_e...@intuit.com> wrote:
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:

  stacks:
  - stack_name: lisa
    region: us-west-1
    instance_type: m1.small

- James 
Message has been deleted

Boris

unread,
Jun 28, 2013, 2:50:45 PM6/28/13
to ansible...@googlegroups.com
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

James Martin

unread,
Jun 28, 2013, 4:15:44 PM6/28/13
to ansible...@googlegroups.com
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:


- James
Reply all
Reply to author
Forward
0 new messages