基于事实的可循环重新启动Kafka连接器失败的任务



这里我想重启Kafka连接任务,如果他们在失败的状态使用ansible-playbook,我已经获取连接器任务状态使用'set_fact'我想在收集的事实上创建一个循环,使用连接器名称和任务id重新启动Kafka连接器任务。

tasks:
- name: Gethering Connector Names
uri:
url: "{{scheme }}://{{ server }}:{{ port_no }}/connectors"
user: "{{ username }}"
password: "{{ password }}"
method: GET
force_basic_auth: yes
status_code: 200
register: conn_stat
- name: Checking for Connector status
uri:
url: "{{scheme }}://{{ server }}:{{ port_no }}/connectors/{{ abc_conn_name }}/status"
user: "{{ username }}"
password: "{{ password }}"
method: GET
force_basic_auth: yes
loop: "{{ conn_name }}"
loop_control:
loop_var: abc_conn_name
vars:
conn_name: "{{ conn_stat.json }}"
register: conn_stat_1
- name: Gethering Failed task id
set_fact:
failed_connector_name_task_id: "{{ conn_stat_1 | json_query('results[].json[].{name: name ,id: [tasks[?state == `RUNNING`].id [] | [0] ]}')  }}"
- name: Failed connector name with tasks id
ansible.builtin.debug:
var:  failed_connector_name_task_id

从事实中获取以下值,我需要将其推入循环

"failed_connector_name_task_id": [
{
"id": [
0
1
],
"name": "test-connector-sample"
},
{
"id": [
0
1
],
"name": "confluent-connect"
},
{
"id": [
0
1
2
],
"name": "confluent-test-1"
}
]
},
"changed": false
}

value需要被张贴

- name: Restart Connector Failed tasks 
uri:
url: "{{scheme }}://{{ server }}:{{ port_no }}/connectors/**{{name of connector}}**/tasks/**{{task ID}}**/restart"
user: "{{ username }}"
password: "{{ password }}"
method: POST
force_basic_auth: yes
status_code: 200
register: conn_stat

连接器名称,<任务ID/strong>想要使用in循环

在上面,我需要为任务设置循环。正如我们在上面看到的,连接器' confluence -test-1'有三个任务处于失败状态,所以它需要用任务' confluence -test-1'迭代三次。

这是一种典型的情况,您希望通过前面提到的过滤器或查找来使用子元素。下面是一个使用过滤器

的示例
- name: Restart Connector Failed tasks 
uri:
url: "{{scheme }}://{{ server }}:{{ port_no }}/connectors/{{ item.0.name }}/tasks/{{ item.1 }}/restart"
user: "{{ username }}"
password: "{{ password }}"
method: POST
force_basic_auth: yes
status_code: 200
loop: "{{ failed_connector_name_task_id | subelements('id', skip_missing=True) }}"

值得阅读的参考文献:

  • ansible循环
  • <
  • 子元素过滤器/gh>

您实际上可以删除最后一个不必要的set_fact任务,例如以下构造:

- name: Restart Connector Failed tasks
vars:
failed_connector_name_task_id: "{{ conn_stat_1 | json_query('results[].json[].{name: name ,id: [tasks[?state == `RUNNING`].id [] | [0] ]}') }}"
uri:
url: "{{scheme }}://{{ server }}:{{ port_no }}/connectors/{{ item.0.name }}/tasks/{{ item.1 }}/restart"
user: "{{ username }}"
password: "{{ password }}"
method: POST
force_basic_auth: yes
status_code: 200
loop: "{{ failed_connector_name_task_id | subelements('id', skip_missing=True) }}"

相关内容

  • 没有找到相关文章

最新更新