在 Ansible 中,有多种方法可以将 Playbook 的执行限制在单个主机上,尤其是在执行可能有害的任务时。以下是三种常见的方法:
- 使用
--limit参数 在运行时通过命令行参数限制执行的主机。 - 在 Playbook 中指定主机名
在 Playbook 的
hosts声明中直接指定主机名。 - 使用变量动态指定主机 在 Playbook 中使用变量来指定主机名,这样可以在运行时动态传递。
示例:限制 Ansible Playbook 执行到 demo.example.com 主机
1. 使用 --limit 参数
Playbook 文件: playbook.yml
---
- name: harmful playbook
hosts: all
tasks:
- name: harmful task
ansible.builtin.debug:
msg: "harmful task"
执行命令:
$ ansible-playbook --limit demo.example.com -i limit/inventory playbook.yml
输出结果:
PLAY [harmful playbook] ***********************************
TASK [Gathering Facts] ************************************
ok: [demo.example.com]
TASK [harmful task] ***************************************
ok: [demo.example.com] => {
"msg": "harmful task"
}
PLAY RECAP *************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2. 在 Playbook 中指定主机名
Playbook 文件: playbook2.yml
---
- name: harmful playbook
hosts: demo.example.com
tasks:
- name: harmful task
ansible.builtin.debug:
msg: "harmful task"
执行命令:
$ ansible-playbook -i limit/inventory playbook2.yml
输出结果:
PLAY [harmful playbook] ***********************************
TASK [Gathering Facts] ************************************
ok: [demo.example.com]
TASK [harmful task] ***************************************
ok: [demo.example.com] => {
"msg": "harmful task"
}
PLAY RECAP *************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
3. 使用变量动态指定主机
Playbook 文件: playbook3.yml
---
- name: harmful playbook
hosts: "{{ HOSTS }}"
tasks:
- name: harmful task
ansible.builtin.debug:
msg: "harmful task"
执行命令:
$ ansible-playbook -i limit/inventory -e "HOSTS=demo.example.com" playbook3.yml
输出结果:
PLAY [harmful playbook] ***********************************
TASK [Gathering Facts] ************************************
ok: [demo.example.com]
TASK [harmful task] ***************************************
ok: [demo.example.com] => {
"msg": "harmful task"
}
PLAY RECAP *************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
错误执行示例
如果忘记使用 --limit 参数,可能会导致意外的结果:
$ ansible-playbook -i limit/inventory playbook.yml
输出结果:
PLAY [harmful playbook] ***********************************
TASK [Gathering Facts] ************************************
ok: [demo.example.com]
ok: [demo2.example.com]
TASK [harmful task] ***************************************
ok: [demo.example.com] => {
"msg": "harmful task"
}
ok: [demo2.example.com] => {
"msg": "harmful task"
}
PLAY RECAP *************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
demo2.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
总结
- 使用
--limit参数:最简单的方法,适合临时限制执行主机。 - 在 Playbook 中指定主机名:适合固定的单个主机执行。
- 使用变量动态指定主机:灵活性高,适合在运行时动态传递主机名。
根据你的需求选择合适的方法,以确保 Ansible Playbook 的安全和有效执行。