假设我的主机具有多个 (DNS) 名称/IP,例如因为它们有多个 NIC,因此有到达它们的路由。
我想玩一本剧本,以防其中一条路线失败。由于我不知道哪一个有效,我想 ansible 尝试所有这些,然后只为该主机播放这本书一次。将所有主机的名称放入 list 并让它运行是很容易的,但是随后 playbook 将为每个主机名称执行一次。
问题:有没有办法指定备用主机名或告诉 ansible 仅在每个组的一台主机上运行 playbook?
最佳答案
可以实现
to run the playbook only on one host per group
请参阅下面的示例。
- hosts: jails-01
strategy: linear
vars:
lock_file: /var/lock/my_ansible_hostname.lock
tasks:
- name: delete lock_file
file:
path: "{{ lock_file }}"
state: absent
run_once: true
delegate_to: localhost
- name: select host
shell: "echo {{ ansible_hostname }} > {{ lock_file }}"
args:
creates: "{{ lock_file }}"
delegate_to: localhost
- name: winner takes it all
fail:
msg: "Too late. Other thread is running. End of play."
when: lookup('file', lock_file) != ansible_hostname
- name: go ahead
debug:
msg: "{{ ansible_hostname }} goes ahead ... "
# ansible-playbook playbook.yml | grep msg
fatal: [test_01]: FAILED! => {"changed": false, "msg": "Too late. Other thread is running. End of play."}
fatal: [test_03]: FAILED! => {"changed": false, "msg": "Too late. Other thread is running. End of play."}
"msg": "test_02 goes ahead ... "
关于Ansible - 同一主机的多个/备用主机名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54199116/