The outermost function of each condition must be defined by Fn::And, Fn::Or, Fn::Not, or Fn::Equals. Other conditions, parameter values, or mappings can be referenced in each condition. You can associate a condition with a task by specifying the When field in the Tasks section of a template.
Syntax
The declaration of a condition consists of a condition name and a condition statement. The condition name is of the STRING type. You can use the following functions to define condition statements. You can reference other conditions in a condition. Each condition name must be unique.
You can use built-in functions when you define conditions. However, the outermost function can only be one of the preceding four functions.
Examples
Reference template parameters
You can directly reference the parameters that are declared in the Parameters section of a template. In this example, the
buildTypeparameter that is declared in a template is referenced.Conditions: NewTemporaryEcs: Fn::Equals: - '{{ buildType }}' - NewTemporaryEcsReference task output parameters
You can reference the output parameters of the prerequisite task of a task as part of a judgment condition. In this example, the output parameter
existedof thecheckPolicyExisttask is referenced.Conditions: createStack: Fn::Equals: - '{{ checkPolicyExist.existed }}' - 'false' Tasks: - Name: checkPolicyExist Action: ACS::CheckFor Description: en: Check for the inexistence of policy zh-cn: the description in Chinese Properties: Service: RAM API: GetPolicy Parameters: PolicyType: 'Custom' PolicyName: '{{ policyName }}' DesiredValues: - 'true' - 'false' PropertySelector: '.DefaultPolicyVersion != null|tostring' Outputs: existed: Type: String ValueSelector: .DefaultPolicyVersion == null|tostringNote: If you reference dynamic parameters in a condition, the task whose output parameters are referenced must be the prerequisite task of the task to which the condition is associated.
Multi-level nested functions
You can embed multi-level functions in conditions for judgment in complex scenarios.
Conditions: downloadWindowsFile: Fn::And: - Fn::Equals: - '{{ queryInstanceOSType.OSType }}' - windows - Fn::Not: Fn::Equals: - '{{ artifactsUrl }}' - ''
Sample template
Associate a condition with a task. In this example, the task to be executed is determined based on the operating system.
FormatVersion: OOS-2019-06-01 Description: name-en: ACS::ECS::CleanUpDisk name-zh-cn: the description in Chinese en: Cleanup disk by run command zh-cn: the description in Chinese # Configure the conditions in the Conditions section. Conditions: Linux: Fn::Equals: - '{{ queryInstanceOSType.OSType }}' - linux Windows: Fn::Equals: - '{{ queryInstanceOSType.OSType }}' - windows Parameters: regionId: Description: en: The ID of region zh-cn: the description in Chinese Type: String Default: '{{ ACS::RegionId }}' instanceId: Description: en: The ID of ECS instance zh-cn: the description in Chinese Type: String Tasks: - Name: queryInstanceOSType Action: 'ACS::ExecuteApi' Description: en: Queries ECS instance OS type zh-cn: the description in Chinese Properties: Service: ECS API: DescribeInstances Parameters: RegionId: '{{ regionId }}' InstanceIds: - '{{ instanceId }}' Outputs: OSType: Type: String ValueSelector: 'Instances.Instance[].OSType' - Name: cleanupLinuxInstanceDisk # Check whether the operating system is Linux. When: Linux Action: 'ACS::ECS::RunCommand' Description: en: Runs cleanup disk command on linux instance zh-cn: the description in Chinese Properties: regionId: '{{ regionId }}' commandContent: |- #!/bin/bash cleanUpInfos="{{cleanUpInfos}}" # Run the command to clear data in the disk. instanceId: '{{ instanceId }}' commandType: RunShellScript Outputs: result: Type: String ValueSelector: .invocationOutput - Name: cleanupWindowsInstanceDisk # Check whether the operating system is Windows. When: Windows Action: 'ACS::ECS::RunCommand' Description: en: Runs cleanup disk command on windows instance zh-cn: the description in Chinese Properties: regionId: '{{ regionId }}' commandContent: |- #!/bin/bash cleanUpInfos="{{cleanUpInfos}}" # Run the command to clear data in the disk. instanceId: '{{ instanceId }}' commandType: RunPowerShellScript Outputs: result: Type: String ValueSelector: .invocationOutput Outputs: commandOutput: Type: String Value: 'Fn::If': - 'Fn::Equals': - linux - '{{ queryInstanceOSType.OSType }}' - '{{ cleanupLinuxInstanceDisk.result }}' - '{{ cleanupWindowsInstanceDisk.result }}'