The outermost function of each condition must be defined by Fn::And, Fn::Or, Fn::Not, or Fn::Equals. Each condition can reference other conditions, parameter values, or mappings. To associate a condition with a task, specify the When field in the Tasks section of a template.
Syntax
A condition declaration consists of a condition name and a condition statement. The condition name is of the STRING type and must be unique. The following functions can be used to define condition statements. A condition can reference other conditions.
You can use built-in functions within conditions, but the outermost function must be one of the preceding four functions.
Examples
-
Reference template parameters
You can reference parameters declared in the Parameters section of a template. The following example references the
buildTypeparameter.Conditions: NewTemporaryEcs: Fn::Equals: - '{{ buildType }}' - NewTemporaryEcs -
Reference task output parameters
You can reference the output parameters of a prerequisite task in a condition. The following example references the
existedoutput parameter of thecheckPolicyExisttask.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 referenced task must be the prerequisite task of the task to which the condition is associated.
-
Multi-level nested functions
You can nest multiple levels of functions in conditions for complex evaluation logic.
Conditions: downloadWindowsFile: Fn::And: - Fn::Equals: - '{{ queryInstanceOSType.OSType }}' - windows - Fn::Not: Fn::Equals: - '{{ artifactsUrl }}' - ''
Sample template
-
The following template associates conditions with tasks to determine which task to execute 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 }}'