团队
我的目标是在fqdn上运行dig命令并获取其IP地址。然后根据IP返回或不打印消息。有点像如果,否则。这种方法是正确的,因为第一次出错,第二次运行有效。
-
name: Test connectivity to target servers
hosts: all
vars_files:
- vars.yaml
tasks:
- name: Ping test
ping:
- name: DNS test
command: "dig +short {{ target_dns_host }} a"
register: dns_result
- name: "Validate DNS server"
debug:
msg: "Could not resolve DNS server '{{ target_dns_host }}' "
when: dns_result.stdout == ""
- name: "Validate DNS server"
debug:
msg: "Resolved DNS server '{{ target_dns_host }}' '{{ dns_result.stdout }}' "
when: dns_result.stdout != ""
下面第一次运行的输出
ansible-playbook playbook-ping-dns-test.yaml -i inventory.txt
PLAY [Test connectivity to target servers] ********************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************
ok: [target1]
TASK [Ping test] **********************************************************************************************************************************************
ok: [target1]
TASK [DNS test] ***********************************************************************************************************************************************
fatal: [target1]: FAILED! => {"changed": true, "cmd": ["dig", "+short", "www.google.com", "a"], "delta": "0:00:15.011879", "end": "2019-09-25 16:25:23.808746", "msg": "non-zero return code", "rc": 9, "start": "2019-09-25 16:25:08.796867", "stderr": "", "stderr_lines": [], "stdout": ";; connection timed out; no servers could be reached", "stdout_lines": [";; connection timed out; no servers could be reached"]}
to retry, use: --limit @/home/osboxes/ansible-validate-maglev/playbook-ping-dns-test.retry
PLAY RECAP ****************************************************************************************************************************************************
target1 : ok=2 changed=0 unreachable=0 failed=1
第二次以下运行的输出
PLAY [Test connectivity to target servers] ********************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************
ok: [target1]
TASK [Ping test] **********************************************************************************************************************************************
ok: [target1]
TASK [DNS test] ***********************************************************************************************************************************************
changed: [target1]
TASK [Validate DNS server] ************************************************************************************************************************************
skipping: [target1]
TASK [Validate DNS server] ************************************************************************************************************************************
ok: [target1] => {
"msg": "Resolved DNS server 'www.google.com' '172.217.11.164' "
}
PLAY RECAP ****************************************************************************************************************************************************
target1 : ok=4 changed=1 unreachable=0 failed=0