Ansible cloudformation模块

Bat*_*oot 3 aws-cloudformation ansible

我不明白如何正确使用template_parameters参数(http://docs.ansible.com/ansible/cloudformation_module.html)

所以,看起来我可以使用这个参数覆盖模板中的一些参数.配置非常简单

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"
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

但我得到了下一个错误:

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

另外,如果尝试使用,我会收到此错误

  template_parameters:
      KeyName: "{{ aws_default_keypair }}"
      InstanceType: "{{ aws_default_instance_type }}"
Run Code Online (Sandbox Code Playgroud)

另外,请为Ansible使用cloudformation模块的最佳方法提供建议.也许最好的方法是生成云形成模板,并在下一步使用它?喜欢 ..

- 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"
Run Code Online (Sandbox Code Playgroud)

提前致谢!

Pas*_*i H 6

您可以使用template_parameters将参数传递给CloudFormation模板.在模板中,您将参考使用的参数Ref.在你的情况下:

剧本:

...
args:
  template_parameters:
    GroupDescriptionParam: "Sandbox Security Group"
...
Run Code Online (Sandbox Code Playgroud)

模板:

...
Resources:
  SandboxSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription:
        Ref: GroupDescriptionParam
...
Run Code Online (Sandbox Code Playgroud)

有关 AWS SecurityGroup CloudFormation模板的示例,请参见 http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-security-group.html#w1ab2c19c12d282c19.

我发现创建一个Jinja2模板,使用Ansible 模块将其转换为CloudFormation 模板,就像你在问题的最后建议一样,而不是template_parameters在Ansible cloudformation模块中使用参数template.使用此方法,您可以省略argstemplate_parameterscloudformation模块调用中.

例如:

- 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
Run Code Online (Sandbox Code Playgroud)

和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 }}
...
Run Code Online (Sandbox Code Playgroud)