Anomalous cloud module

I don't understand how to use the template_parameters parameter correctly ( http://docs.ansible.com/ansible/cloudformation_module.html )

So it looks like I could use this parameter to override some parameter in the template. Very simple configuration

sandbox_cloudformation.yml

---

- name: Sandbox CloudFormation
  hosts: localhost
  connection: local
  gather_facts: false

  tasks:

   - name: Launch Ansible CloudFormation Stack
     cloudformation:
       aws_access_key: "{{ aws_access_key }}"
       aws_secret_key: "{{ aws_secret_key }}"
       stack_name: "{{ aws_default_cloudformation_stack_name }}"
       state: "present"
       region: "{{ aws_default_region }}"
       disable_rollback: true
       template: "files/cloudformation.yml"
     args:
       template_parameters:
         GroupDescription: "Sandbox Security Group"

      

cloudformation.yml

---
AWSTemplateFormatVersion: '2010-09-09'
Description: Sandbox Stack
Resources:
  SandboxSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: "DEMO"
      SecurityGroupIngress:
      - IpProtocol: tcp
        FromPort: '80'
        ToPort: '80'
        CidrIp: 0.0.0.0/0
      - IpProtocol: tcp
        FromPort: '22'
        ToPort: '22'
        CidrIp: 0.0.0.0/0

      

But I got the following error:

fatal: [127.0.0.1]: FAILED! => {"changed": false, "failed": true, "msg": "Parameter values specified for a template which does not require them."}

      

Also, I got this error if I try to use for example

  template_parameters:
      KeyName: "{{ aws_default_keypair }}"
      InstanceType: "{{ aws_default_instance_type }}"

      

Also, consult the best approach for using the cloudformation module for Ansible. Maybe the best way is to create a cloud formation template and use it in the next step? How..

- name: Render Cloud Formation Template
  template: src=cloudformation.yml.j2 dest=rendered_templates/cloudformation.yml

- name: Launch Ansible CloudFormation Stack
         cloudformation:
           template: "rendered_templates/cloudformation.yml"

      

Thanks in advance!

+3


source to share


1 answer


You can use template_parameters

to pass parameters to the CloudFormation template. In the template, you will refer to the parameters with Ref

. In your case:

Playbook:

...
args:
  template_parameters:
    GroupDescriptionParam: "Sandbox Security Group"
...

      

template:

...
Resources:
  SandboxSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription:
        Ref: GroupDescriptionParam
...

      

See http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-security-group.html#w1ab2c19c12d282c19  for CloudSormation AWS SecurityGroup template examples.



Instead of using arguments template_parameters

in the Ansible module, cloudformation

I found it handy to create a Jinja2 template, convert it to a CloudFormation template using the Ansible module template

, as at the end of your question. Using this approach, you can skip args

and template_parameters

the module cloudformation

.

For example:

- name: Generate CloudFormation template
  become: no
  run_once: yes
  local_action: 
    module: template
    src: "mycloudformation.yml.j2"
    dest: "{{ cf_templ_dir }}/mycloudformation.yml"
  tags:
    - cloudformation
    - cf_template

- name: Deploy the CloudFormation stack
  become: no
  run_once: yes
  local_action: 
    module: cloudformation
    stack_name: "{{ cf_stack_name }}"
    state: present
    region: "{{ ec2_default_region }}"
    template: "{{ cf_templ_dir }}/mycloudformation.yml"
  register: cf_result
  tags:
    - cloudformation

- name: Show CloudFormation output
  run_once: yes
  become: no
  debug: var=cf_result
  tags:
    - cloudformation

      

And mycloudformation.yml.j2:

---
AWSTemplateFormatVersion: '2010-09-09'
Description: Sandbox Stack
Resources:
  SandboxSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: {{ group_desc_param_defined_somewhere_in_ansible }}
...

      

+5


source







All Articles