我使用ansible来管理几个AWS资源,比如DMS。我想通过变量图来控制。将“恢复处理”或“停止”的dmsTask映射到变量,并相应地将命令传递给任务:
/vars/dms.yml
my_dms_tasks:
resumeProcessing:
mainCommand: |
aws dms start-replication-task
--replication-task-arn "{{item.RepTaskArn}}"
--start-replication-task-type resume-processing
description: Resume processing
waitCommand: aws dms wait replication-task-running --filters '[{"Name":"replication-task-arn","Values":{{replicationTaskArns}}}]'
stopping:
mainCommand: |
aws dms stop-replication-task
--replication-task-arn "{{item.RepTaskArn}}"
description: Stopping
waitCommand: aws dms wait replication-task-stopped --filters '[{"Name":"replication-task-arn","Values":{{replicationTaskArns}}}]这是我的根剧本:
---
- hosts: localhost
vars:
endpointIdentifiers: "{{endpointIdentifiers}}"
dmsTask: "{{dmsTask}}"
tasks:
- name: Include vars
include_vars: dms.yml
register: my_dms_tasks
- debug:
var: dms_tasks
##### Get the relevant endpoints for identifying the corresponding dms tasks #####
##### Transform the result of 'get endpoint arn' into a Json object #####
##### Get relevant dms tasks via the aws cli #####
- import_tasks: tasks/task_get_dms.yml
##### Transfom the result of 'get dms tasks' into a Json object #####
- set_fact:
replicationTaskArnsJson: "{{replicationTaskArns.results[0].stdout | from_json}}"
##### Based on the passed dmsTask act accordingly #####
- import_tasks: tasks/task_modify_dms.yml任务剧本如下所示:
tasks/task_modify_dms.yml
- name: Manage DMS tasks
block:
- name: "Resume processing DMS tasks"
command: "{{my_dms_tasks[dmsTask].mainCommand}}"
loop: "{{replicationTaskArnsJson.RepTasks}}"
register: tasksResult
- name: "Get the 'replication-task-arns' as list"
set_fact:
replicationTaskArns: "{{tasksResult | json_query('results[*].item.RepTaskArn') | to_json}}"
- name : "Show replicationTaskArns as Json"
debug:
var: replicationTaskArns
# Wait for the tasks to be finished, then proceed
- name: "Wait until DMS tasks have finished"
command: "{{my_dms_task[dmsTask].waitCommand"
rescue:
- debug:
msg: "Nothing to change"错误是:
failed: [localhost] (item={u'Status': u'stopped', u'RepTaskIdentifier': u'guidewire-qa-pc01-gsec-ongoing', u'RepTaskArn': u'arn:aws:dms:eu-central-1:118628429710:task:MCH3GI2YVW77Y7M5N5V5KB5TSU'}) => {"ansible_loop_var": "item", "changed": true, "cmd": ["aws", "dms", "stop-replication-task", "--replication-task-arn", "{{item.RepTaskArn}}"]...为什么“{{item.RepTaskArn}”不被解决?
发布于 2020-03-01 23:24:43
我找到了解决办法。我不得不用"import_tasks"代替"include_tasks".
https://stackoverflow.com/questions/60477661
复制相似问题