ansible.builtin.cloudformation (v2.9.13) — module

Create or delete an AWS CloudFormation stack

| "added in version" 1.1 of ansible.builtin"

Authors: James S. Martin (@jsmartin)

stableinterface | supported by core

Install Ansible via pip

Install with pip install ansible==2.9.13

Description

Launches or updates an AWS CloudFormation stack and waits for it complete.


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: create a cloudformation stack
  cloudformation:
    stack_name: "ansible-cloudformation"
    state: "present"
    region: "us-east-1"
    disable_rollback: true
    template: "files/cloudformation-example.json"
    template_parameters:
      KeyName: "jmartin"
      DiskType: "ephemeral"
      InstanceType: "m1.small"
      ClusterSize: 3
    tags:
      Stack: "ansible-cloudformation"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Basic role example
- name: create a stack, specify role that cloudformation assumes
  cloudformation:
    stack_name: "ansible-cloudformation"
    state: "present"
    region: "us-east-1"
    disable_rollback: true
    template: "roles/cloudformation/files/cloudformation-example.json"
    role_arn: 'arn:aws:iam::123456789012:role/cloudformation-iam-role'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: delete a stack
  cloudformation:
    stack_name: "ansible-cloudformation-old"
    state: "absent"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Create a stack, pass in template from a URL, disable rollback if stack creation fails,
# pass in some parameters to the template, provide tags for resources created
- name: create a stack, pass in the template via an URL
  cloudformation:
    stack_name: "ansible-cloudformation"
    state: present
    region: us-east-1
    disable_rollback: true
    template_url: https://s3.amazonaws.com/my-bucket/cloudformation.template
    template_parameters:
      KeyName: jmartin
      DiskType: ephemeral
      InstanceType: m1.small
      ClusterSize: 3
    tags:
      Stack: ansible-cloudformation
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Create a stack, passing in template body using lookup of Jinja2 template, disable rollback if stack creation fails,
# pass in some parameters to the template, provide tags for resources created
- name: create a stack, pass in the template body via lookup template
  cloudformation:
    stack_name: "ansible-cloudformation"
    state: present
    region: us-east-1
    disable_rollback: true
    template_body: "{{ lookup('template', 'cloudformation.j2') }}"
    template_parameters:
      KeyName: jmartin
      DiskType: ephemeral
      InstanceType: m1.small
      ClusterSize: 3
    tags:
      Stack: ansible-cloudformation
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Pass a template parameter which uses Cloudformation's UsePreviousValue attribute
# When use_previous_value is set to True, the given value will be ignored and
# Cloudformation will use the value from a previously submitted template.
# If use_previous_value is set to False (default) the given value is used.
- cloudformation:
    stack_name: "ansible-cloudformation"
    state: "present"
    region: "us-east-1"
    template: "files/cloudformation-example.json"
    template_parameters:
      DBSnapshotIdentifier:
        use_previous_value: True
        value: arn:aws:rds:es-east-1:000000000000:snapshot:rds:my-db-snapshot
      DBName:
        use_previous_value: True
    tags:
      Stack: "ansible-cloudformation"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Enable termination protection on a stack.
# If the stack already exists, this will update its termination protection
- name: enable termination protection during stack creation
  cloudformation:
    stack_name: my_stack
    state: present
    template_url: https://s3.amazonaws.com/my-bucket/cloudformation.template
    termination_protection: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Configure TimeoutInMinutes before the stack status becomes CREATE_FAILED
# In this case, if disable_rollback is not set or is set to false, the stack will be rolled back.
- name: enable termination protection during stack creation
  cloudformation:
    stack_name: my_stack
    state: present
    template_url: https://s3.amazonaws.com/my-bucket/cloudformation.template
    create_timeout: 5
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Configure rollback behaviour on the unsuccessful creation of a stack allowing
# CloudFormation to clean up, or do nothing in the event of an unsuccessful
# deployment
# In this case, if on_create_failure is set to "DELETE", it will clean up the stack if
# it fails to create
- name: create stack which will delete on creation failure
  cloudformation:
    stack_name: my_stack
    state: present
    template_url: https://s3.amazonaws.com/my-bucket/cloudformation.template
    on_create_failure: DELETE

Inputs

    
tags:
    description:
    - Dictionary of tags to associate with stack and its resources during stack creation.
      Can be updated later, updating tags removes previous entries.
    version_added: '1.4'
    version_added_collection: ansible.builtin

state:
    choices:
    - present
    - absent
    default: present
    description:
    - If state is "present", stack will be created.  If state is "present" and if stack
      exists and template has changed, it will be updated. If state is "absent", stack
      will be removed.

region:
    aliases:
    - aws_region
    - ec2_region
    description:
    - The AWS region to use.
    - For global services such as IAM, Route53 and CloudFront, I(region) is ignored.
    - The C(AWS_REGION) or C(EC2_REGION) environment variables may also be used.
    - See the Amazon AWS documentation for more information U(http://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_region).
    - The C(ec2_region) alias has been deprecated and will be removed in a release after
      2024-12-01
    - Support for the C(EC2_REGION) environment variable has been deprecated and will
      be removed in a release after 2024-12-01.
    type: str

profile:
    aliases:
    - aws_profile
    description:
    - A named AWS profile to use for authentication.
    - See the AWS documentation for more information about named profiles U(https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html).
    - The C(AWS_PROFILE) environment variable may also be used.
    - The I(profile) option is mutually exclusive with the I(aws_access_key), I(aws_secret_key)
      and I(security_token) options.
    type: str

role_arn:
    description:
    - The role that AWS CloudFormation assumes to create the stack. See the AWS CloudFormation
      Service Role docs U(https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-iam-servicerole.html)
    version_added: '2.3'
    version_added_collection: ansible.builtin

template:
    description:
    - The local path of the cloudformation template.
    - This must be the full path to the file, relative to the working directory. If using
      roles this may look like "roles/cloudformation/files/cloudformation-example.json".
    - If 'state' is 'present' and the stack does not exist yet, either 'template', 'template_body'
      or 'template_url' must be specified (but only one of them). If 'state' is 'present',
      the stack does exist, and neither 'template', 'template_body' nor 'template_url'
      are specified, the previous template will be reused.

access_key:
    aliases:
    - aws_access_key_id
    - aws_access_key
    - ec2_access_key
    description:
    - AWS access key ID.
    - See the AWS documentation for more information about access tokens U(https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys).
    - The C(AWS_ACCESS_KEY_ID), C(AWS_ACCESS_KEY) or C(EC2_ACCESS_KEY) environment variables
      may also be used in decreasing order of preference.
    - The I(aws_access_key) and I(profile) options are mutually exclusive.
    - The I(aws_access_key_id) alias was added in release 5.1.0 for consistency with the
      AWS botocore SDK.
    - The I(ec2_access_key) alias has been deprecated and will be removed in a release
      after 2024-12-01.
    - Support for the C(EC2_ACCESS_KEY) environment variable has been deprecated and will
      be removed in a release after 2024-12-01.
    type: str

aws_config:
    description:
    - A dictionary to modify the botocore configuration.
    - Parameters can be found in the AWS documentation U(https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html#botocore.config.Config).
    type: dict

secret_key:
    aliases:
    - aws_secret_access_key
    - aws_secret_key
    - ec2_secret_key
    description:
    - AWS secret access key.
    - See the AWS documentation for more information about access tokens U(https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys).
    - The C(AWS_SECRET_ACCESS_KEY), C(AWS_SECRET_KEY), or C(EC2_SECRET_KEY) environment
      variables may also be used in decreasing order of preference.
    - The I(secret_key) and I(profile) options are mutually exclusive.
    - The I(aws_secret_access_key) alias was added in release 5.1.0 for consistency with
      the AWS botocore SDK.
    - The I(ec2_secret_key) alias has been deprecated and will be removed in a release
      after 2024-12-01.
    - Support for the C(EC2_SECRET_KEY) environment variable has been deprecated and will
      be removed in a release after 2024-12-01.
    type: str

stack_name:
    description:
    - name of the cloudformation stack
    required: true

capabilities:
    default:
    - CAPABILITY_IAM
    - CAPABILITY_NAMED_IAM
    description:
    - Specify capabilities that stack template contains.
    - Valid values are CAPABILITY_IAM, CAPABILITY_NAMED_IAM and CAPABILITY_AUTO_EXPAND.
    type: list
    version_added: '2.8'
    version_added_collection: ansible.builtin

endpoint_url:
    aliases:
    - ec2_url
    - aws_endpoint_url
    - s3_url
    description:
    - URL to connect to instead of the default AWS endpoints.  While this can be used
      to connection to other AWS-compatible services the amazon.aws and community.aws
      collections are only tested against AWS.
    - The  C(AWS_URL) or C(EC2_URL) environment variables may also be used, in decreasing
      order of preference.
    - The I(ec2_url) and I(s3_url) aliases have been deprecated and will be removed in
      a release after 2024-12-01.
    - Support for the C(EC2_URL) environment variable has been deprecated and will be
      removed in a release after 2024-12-01.
    type: str

events_limit:
    default: 200
    description:
    - Maximum number of CloudFormation events to fetch from a stack when creating or updating
      it.
    version_added: '2.7'
    version_added_collection: ansible.builtin

stack_policy:
    description:
    - the path of the cloudformation stack policy. A policy cannot be removed once placed,
      but it can be modified. for instance, allow all updates U(https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/protect-stack-resources.html#d0e9051)
    version_added: '1.9'
    version_added_collection: ansible.builtin

template_url:
    description:
    - Location of file containing the template body. The URL must point to a template
      (max size 307,200 bytes) located in an S3 bucket in the same region as the stack.
    - If 'state' is 'present' and the stack does not exist yet, either 'template', 'template_body'
      or 'template_url' must be specified (but only one of them). If 'state' is present,
      the stack does exist, and neither 'template', 'template_body' nor 'template_url'
      are specified, the previous template will be reused.
    version_added: '2.0'
    version_added_collection: ansible.builtin

aws_ca_bundle:
    description:
    - The location of a CA Bundle to use when validating SSL certificates.
    - The C(AWS_CA_BUNDLE) environment variable may also be used.
    type: path

backoff_delay:
    default: 3
    description:
    - Number of seconds to wait for the next retry.
    required: false
    type: int
    version_added: '2.8'
    version_added_collection: ansible.builtin

session_token:
    aliases:
    - aws_session_token
    - security_token
    - aws_security_token
    - access_token
    description:
    - AWS STS session token for use with temporary credentials.
    - See the AWS documentation for more information about access tokens U(https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys).
    - The C(AWS_SESSION_TOKEN), C(AWS_SECURITY_TOKEN) or C(EC2_SECURITY_TOKEN) environment
      variables may also be used in decreasing order of preference.
    - The I(security_token) and I(profile) options are mutually exclusive.
    - Aliases I(aws_session_token) and I(session_token) were added in release 3.2.0, with
      the parameter being renamed from I(security_token) to I(session_token) in release
      6.0.0.
    - The I(security_token), I(aws_security_token), and I(access_token) aliases have been
      deprecated and will be removed in a release after 2024-12-01.
    - Support for the C(EC2_SECRET_KEY) and C(AWS_SECURITY_TOKEN) environment variables
      has been deprecated and will be removed in a release after 2024-12-01.
    type: str

template_body:
    description:
    - Template body. Use this to pass in the actual body of the Cloudformation template.
    - If 'state' is 'present' and the stack does not exist yet, either 'template', 'template_body'
      or 'template_url' must be specified (but only one of them). If 'state' is present,
      the stack does exist, and neither 'template', 'template_body' nor 'template_url'
      are specified, the previous template will be reused.
    version_added: '2.5'
    version_added_collection: ansible.builtin

changeset_name:
    description:
    - Name given to the changeset when creating a changeset, only used when create_changeset
      is true. By default a name prefixed with Ansible-STACKNAME is generated based on
      input parameters. See the AWS Change Sets docs U(https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-changesets.html)
    version_added: '2.4'
    version_added_collection: ansible.builtin

create_timeout:
    description:
    - The amount of time (in minutes) that can pass before the stack status becomes CREATE_FAILED
    version_added: '2.6'
    version_added_collection: ansible.builtin

validate_certs:
    default: true
    description:
    - When set to C(false), SSL certificates will not be validated for communication with
      the AWS APIs.
    - Setting I(validate_certs=false) is strongly discouraged, as an alternative, consider
      setting I(aws_ca_bundle) instead.
    type: bool

backoff_retries:
    default: 10
    description:
    - Number of times to retry operation.
    - AWS API throttling mechanism fails Cloudformation module so we have to retry a couple
      of times.
    required: false
    type: int
    version_added: '2.8'
    version_added_collection: ansible.builtin

template_format:
    choices:
    - json
    - yaml
    default: json
    description:
    - (deprecated) For local templates, allows specification of json or yaml format. Templates
      are now passed raw to CloudFormation regardless of format. This parameter is ignored
      since Ansible 2.3.
    version_added: '2.0'
    version_added_collection: ansible.builtin

create_changeset:
    default: 'no'
    description:
    - 'If stack already exists create a changeset instead of directly applying changes.
      See the AWS Change Sets docs U(https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-changesets.html).
      WARNING: if the stack does not exist, it will be created without changeset. If the
      state is absent, the stack will be deleted immediately with no changeset.'
    type: bool
    version_added: '2.4'
    version_added_collection: ansible.builtin

disable_rollback:
    default: 'no'
    description:
    - If a stacks fails to form, rollback will remove the stack
    type: bool

backoff_max_delay:
    default: 30
    description:
    - Maximum amount of time to wait between retries.
    required: false
    type: int
    version_added: '2.8'
    version_added_collection: ansible.builtin

notification_arns:
    description:
    - The Simple Notification Service (SNS) topic ARNs to publish stack related events.
    version_added: '2.0'
    version_added_collection: ansible.builtin

on_create_failure:
    choices:
    - DO_NOTHING
    - ROLLBACK
    - DELETE
    description:
    - Action to take upon failure of stack creation. Incompatible with the disable_rollback
      option.
    version_added: '2.8'
    version_added_collection: ansible.builtin

template_parameters:
    default: {}
    description:
    - A list of hashes of all the template variables for the stack. The value can be a
      string or a dict.
    - Dict can be used to set additional template parameter attributes like UsePreviousValue
      (see example).

termination_protection:
    description:
    - enable or disable termination protection on the stack. Only works with botocore
      >= 1.7.18.
    type: bool
    version_added: '2.5'
    version_added_collection: ansible.builtin

debug_botocore_endpoint_logs:
    default: false
    description:
    - Use a C(botocore.endpoint) logger to parse the unique (rather than total) C("resource:action")
      API calls made during a task, outputing the set to the resource_actions key in the
      task results. Use the C(aws_resource_action) callback to output to total list made
      during a playbook.
    - The C(ANSIBLE_DEBUG_BOTOCORE_LOGS) environment variable may also be used.
    type: bool

Outputs

events:
  description: Most recent events in Cloudformation's event log. This may be from
    a previous run in some cases.
  returned: always
  sample:
  - StackEvent AWS::CloudFormation::Stack stackname UPDATE_COMPLETE
  - StackEvent AWS::CloudFormation::Stack stackname UPDATE_COMPLETE_CLEANUP_IN_PROGRESS
  type: list
log:
  description: Debugging logs. Useful when modifying or finding an error.
  returned: always
  sample:
  - updating stack
  type: list
stack_outputs:
  description: A key:value dictionary of all the stack outputs currently defined.
    If there are no stack outputs, it is an empty dictionary.
  returned: state == present
  sample:
    MySg: AnsibleModuleTestYAML-CFTestSg-C8UVS567B6NS
  type: dict
stack_resources:
  description: AWS stack resources and their status. List of dictionaries, one dict
    per resource.
  returned: state == present
  sample:
  - last_updated_time: '2016-10-11T19:40:14.979000+00:00'
    logical_resource_id: CFTestSg
    physical_resource_id: cloudformation2-CFTestSg-16UQ4CYQ57O9F
    resource_type: AWS::EC2::SecurityGroup
    status: UPDATE_COMPLETE
    status_reason: null
  type: list