当某些主机无法访问时,我正在尝试打印自定义消息。我的问题是,当主机无法访问时,下一个任务将跳过它,因此永远不会触发失败模块。并且可到达的主机也会由于when condition are not met.
我试过了ignore_unreachable: false,还是一样。任何想法将不胜感激。
---
- hosts: prod
gather_facts: no
tasks:
- name: fail when not reachable
action: ping
register: ping_result
# any_errors_fatal: true
- fail:
msg: "please make sure all server up and run again"
when: ping_result.unreachable is defined
any_errors_fatal: true
Run Code Online (Sandbox Code Playgroud) 我想知道如何循环多个任务直到满足条件。
#main.yml
- set_fact:
num: 1
req_num: 10
- name: Start to unregister entities
include_tasks: output.yml
loop: "{{ range(num, req_num + 1)|list }}"
Run Code Online (Sandbox Code Playgroud)
#输出.yml
- name: get status
raw: cat /tmp/output
register: rawoutput
- name: copy to localhost
copy:
content: "{{rawoutput.stdout}}"
dest: /tmp/output1
delegate_to: localhost
- name: reg output2
shell: awk something /tmp/output1 |awk '/something/,0' |head -n something |tail -n something > /tmp/output2 ; cat /tmp/output2
register: output2
delegate_to: localhost
- name: compare output2
debug:
msg: "{{item}}"
with_items: "{{ output2.stdout_lines …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用delegate_to localhost或connection: local在远程主机上运行某些任务和其他任务。但是,当我使用“delegate_to:localhost”时,该任务会在本地主机上多次执行
我的库存
localhost ansible_host=127.0.0.1 ansible_connection=local ansible_python_interpreter="{{ansible_playbook_python}}"
[master1]
ip-10-90-148-195.ap-southeast-1.compute.internal
[master2]
ip-10-90-149-130.ap-southeast-1.compute.internal
[master3]
ip-10-90-150-239.ap-southeast-1.compute.internal
[master:children]
master1
master2
master3
[worker]
ip-10-90-148-206.ap-southeast-1.compute.internal
ip-10-90-149-178.ap-southeast-1.compute.internal
ip-10-90-150-86.ap-southeast-1.compute.internal
[all:vars]
ansible_user="core"
ansible_ssh_private_key_file="~/.ssh/id_rsa"
Run Code Online (Sandbox Code Playgroud)
我的任务:
- name: host name
shell: echo `hostname` >> /tmp/123
#delegate_to: localhost
#connection: local
Run Code Online (Sandbox Code Playgroud)
如果我的评论delegate_to: localhost和connection: local,我会用它里面自己的主机名的每个远程主机上得到一个文件/ tmp / 123。预期结果。
但是,如果我取消注释其中任何一个,该任务将在本地主机上执行 6 次。这意味着 localhost 上的 /tmp/ls 将在其中打印 6 次 localhost 的主机名。
我的目标很简单,我只想按照 playbook 中的定义在所有主机上运行某些任务hosts: groupa:groupb,并在 localhost上运行某些任务,但仅执行 1 次。我认为这是直截了当的,但我一直在花费数小时但没有结果。