我想執行一個命令以整數格式獲取當前的 AWS EC2 啟動模板版本,以便我可以對其進行基本算術以在后續查詢/洗掉中使用。
例如:
tasks:
- name: Lookup current default version of EC2 launch template
command: aws ec2 describe-launch-template-versions --launch-template-id lt-xxx --filters Name=is-default-version,Value=true --query 'LaunchTemplateVersions[*].[VersionNumber]'
delegate_to: localhost
register: result
- name: Show results
debug:
msg: '{{ result.stdout }}'
delegate_to: localhost
如果此輸出為“5”,我想從中減去 1,以便執行附加命令來執行以下操作:
aws ec2 delete-launch-template-versions --launch-template-id lt-xxx --versions {{result - 1}}
我意識到這不會像寫的那樣作業,但這是我要去的前提。
uj5u.com熱心網友回復:
將字串轉換為整數。例如
- command: echo 5
register: result
- command: "echo {{ result.stdout|int - 1 }}"
register: result
- debug:
var: result.stdout
給
result.stdout: '4'
命令回傳值的屬性stdout的型別是字串。請參閱下面的結果
- command: echo 5
register: result
- debug:
msg: |-
result.stdout: {{ result.stdout }}
result.stdout|type_debug: {{ result.stdout|type_debug }}
result.stdout|int|type_debug: {{ result.stdout|int|type_debug }}
msg: |-
result.stdout: 5
result.stdout|type_debug: AnsibleUnsafeText
result.stdout|int|type_debug: int
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/476135.html
上一篇:允許時間流:描述執行
下一篇:添加VPC資源時SCP行為突然
