2017-03-21 5 views
3

Ich verstehe nicht richtig, wie man richtig verwenden template_parameters Parameter (http://docs.ansible.com/ansible/cloudformation_module.html)ansible Cloudformation-Modul

So, sieht aus wie ich diesen param verwenden könnte einige param in Vorlage zu überschreiben. Sehr einfache Konfiguration ist

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 

Aber ich habe nächsten Fehler:

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

In additio n, habe ich diesen Fehler, zu verwenden, wenn versuchen, zum Beispiel

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

Sie bitte auch, mit dem besten Ansatz beraten Cloudformation-Modul für ansible zu verwenden. Vielleicht ist der beste Weg, Cloud-Formationsvorlage zu generieren und im nächsten Schritt zu verwenden? Wie ..

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

Vielen Dank im Voraus!

Antwort

5

Sie können verwenden, um Parameter an eine CloudFormation-Vorlage zu übergeben. In der Vorlage verweisen Sie auf die Parameter mit Ref. In Ihrem Fall:

Textbuch:

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

Vorlage:

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

Siehe http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-security-group.html#w1ab2c19c12d282c19 für Beispiele von AWS Cloudformation Security Vorlagen.

Statt mit template_parameters Argumente in ansible cloudformation Modul, habe ich fand es praktisch, eine Jinja2 Template zu erstellen, wandeln es in eine Cloudformation-Vorlage ansible template Modul, wie Sie am Ende Ihrer Frage vorgeschlagen. Mit diesem Ansatz können Sie args und template_parameters in cloudformation Modul Aufruf weglassen.

Zum Beispiel:

- 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 

Und 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

Vielen Dank! Das ist sehr nützlich – BattleLoot