2017-03-21 21 views
3

は私が正しくので、パラメータ(http://docs.ansible.com/ansible/cloudformation_module.htmlAnsibleのcloudformationモジュール

をtemplate_parametersどのように使用するか、正しく理解していない私は、テンプレートにいくつかのparamを上書きするために、このPARAMを使用することができますように、見えます。非常にシンプルな構成は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 

あるしかし、私は次のエラーました:additioで

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

をnは、私が使用しようとすると、例えば

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

また、Ansibleためcloudformationモジュールを使用するのが最善の方法とアドバイスをしてください、このエラーを得ました。たぶん最善の方法は、雲の形成テンプレートを生成し、次のステップでそれを使用ですか? Like ...

- 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" 

ありがとうございます!

答えて

5

template_parametersを使用すると、パラメータをCloudFormationテンプレートに渡すことができます。テンプレートでは、Refを使用してパラメータを参照します。あなたのケースでは:

脚本:

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

テンプレート:

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

は、AWS CloudFormation SecurityGroupテンプレートの例については http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-security-group.html#w1ab2c19c12d282c19 を参照してください。代わりにAnsible cloudformationモジュールでtemplate_parameters引数を使用しての

、私はあなたがあなたの質問の最後に提案同じように、Ansible templateモジュールを使用してCloudFormationテンプレートに変換し、それは便利なJinja2のテンプレートを作成することが分かってきました。この方法を使用すると、cloudformationモジュールコールでargstemplate_parametersを省略できます。例えば

- 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 

そして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 }} 
... 
+0

どうもありがとう!これは非常に便利です – BattleLoot

関連する問題