Single cloudformation playbook for multiple regions

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

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

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

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

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

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

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

  • James