ansible v2_runner_on_start 能写一些什么东西,给些案例
时间: 2024-01-17 12:05:16 浏览: 127
当 Ansible 开始执行任务时,可以在 playbook 或 role 中使用 `v2_runner_on_start` 这个钩子(hook)来执行一些额外的操作。下面是一些使用 `v2_runner_on_start` 钩子的案例:
1. 在任务开始时发送通知
```
- name: Send notification on task start
hosts: all
tasks:
- name: Notify on task start
local_action:
module: slack
token: "{{ slack_token }}"
msg: "Ansible task {{ ansible_playbook_name }} started on {{ inventory_hostname }}"
delegate_to: localhost
handlers:
- name: Restart service
service:
name: myservice
state: restarted
```
2. 在任务开始时记录日志
```
- name: Log task start
hosts: all
tasks:
- name: Log task start
local_action:
module: log
msg: "Ansible task {{ ansible_playbook_name }} started on {{ inventory_hostname }}"
delegate_to: localhost
handlers:
- name: Restart service
service:
name: myservice
state: restarted
```
3. 在任务开始时设置全局变量
```
- name: Set global variable on task start
hosts: all
tasks:
- name: Set global variable
set_fact:
myvar: "myvalue"
delegate_to: localhost
run_once: true
handlers:
- name: Restart service
service:
name: myservice
state: restarted
```
这些案例只是示例,实际上你可以在 `v2_runner_on_start` 钩子中执行任何 Ansible 模块或自定义模块,只要它们不会影响 Ansible 任务的正常执行即可。
阅读全文