由于某种原因,我无法使用 wait_for 来检查带有ansible的mongodb上的特定自定义端口。不确定是否是iptables,但我不会更改iptables。
尽管如此,我还是使用netstat检查端口
- name: netstat
shell: /bin/netstat -tln | grep ":{{ item }} " | sed -e 's/.*\///'
register:
netstat_result
with_items:
- 27018
changed_when: false
我如何使用失败
检查是否找到端口?
答案 0 :(得分:0)
这是解析results
数组的一种方法。您需要注意netstat_result
数组的结构(可以添加调试任务以打印变量)。
Playbook(添加了一些其他端口来运行netstat):
[ilias@orangehat-29 temp]$ cat test.yml
---
- hosts: localhost
gather_facts: false
tasks:
- name: netstat
shell: /bin/netstat -tln | grep ":{{ item }} " | sed -e 's/.*\///'
register:
netstat_result
with_items:
- 27018
- 22
- 80
changed_when: false
- name: print result
debug:
msg: "for port: {{ item.item }}, netstat stdout was: {{ item.stdout }}"
with_items:
- "{{ netstat_result.results }}"
[ilias@orangehat-29 temp]$
输出:
[ilias@orangehat-29 temp]$ ansible-playbook test.yml
PLAY [localhost] ***************************************************************************************************************************************
TASK [netstat] *****************************************************************************************************************************************
Wednesday 13 March 2019 06:46:53 +0200 (0:00:00.160) 0:00:00.160 *******
ok: [localhost] => (item=27018)
ok: [localhost] => (item=22)
ok: [localhost] => (item=80)
TASK [print result] ************************************************************************************************************************************
Wednesday 13 March 2019 06:46:55 +0200 (0:00:01.387) 0:00:01.548 *******
ok: [localhost] => (item={'cmd': '/bin/netstat -tln | grep ":27018 " | sed -e \'s/.*\\///\'', 'stdout': '', 'stderr': '', 'rc': 0, 'start': '2019-03-13 06:46:54.575733', 'end': '2019-03-13 06:46:54.584105', 'delta': '0:00:00.008372', 'changed': False, 'invocation': {'module_args': {'_raw_params': '/bin/netstat -tln | grep ":27018 " | sed -e \'s/.*\\///\'', '_uses_shell': True, 'warn': True, 'argv': None, 'chdir': None, 'executable': None, 'creates': None, 'removes': None, 'stdin': None}}, '_ansible_parsed': True, 'stdout_lines': [], 'stderr_lines': [], '_ansible_no_log': False, 'failed': False, 'item': 27018, '_ansible_item_result': True, '_ansible_ignore_errors': None, '_ansible_item_label': 27018}) => {
"msg": "for port: 27018, netstat stdout was: "
}
ok: [localhost] => (item={'cmd': '/bin/netstat -tln | grep ":22 " | sed -e \'s/.*\\///\'', 'stdout': 'tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN \ntcp6 0 0 :::22 :::* LISTEN ', 'stderr': '', 'rc': 0, 'start': '2019-03-13 06:46:54.898268', 'end': '2019-03-13 06:46:54.906919', 'delta': '0:00:00.008651', 'changed': False, 'invocation': {'module_args': {'_raw_params': '/bin/netstat -tln | grep ":22 " | sed -e \'s/.*\\///\'', '_uses_shell': True, 'warn': True, 'argv': None, 'chdir': None, 'executable': None, 'creates': None, 'removes': None, 'stdin': None}}, '_ansible_parsed': True, 'stdout_lines': ['tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN ', 'tcp6 0 0 :::22 :::* LISTEN '], 'stderr_lines': [], '_ansible_no_log': False, 'failed': False, 'item': 22, '_ansible_item_result': True, '_ansible_ignore_errors': None, '_ansible_item_label': 22}) => {
"msg": "for port: 22, netstat stdout was: tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN \ntcp6 0 0 :::22 :::* LISTEN "
}
ok: [localhost] => (item={'cmd': '/bin/netstat -tln | grep ":80 " | sed -e \'s/.*\\///\'', 'stdout': '', 'stderr': '', 'rc': 0, 'start': '2019-03-13 06:46:55.228628', 'end': '2019-03-13 06:46:55.236853', 'delta': '0:00:00.008225', 'changed': False, 'invocation': {'module_args': {'_raw_params': '/bin/netstat -tln | grep ":80 " | sed -e \'s/.*\\///\'', '_uses_shell': True, 'warn': True, 'argv': None, 'chdir': None, 'executable': None, 'creates': None, 'removes': None, 'stdin': None}}, '_ansible_parsed': True, 'stdout_lines': [], 'stderr_lines': [], '_ansible_no_log': False, 'failed': False, 'item': 80, '_ansible_item_result': True, '_ansible_ignore_errors': None, '_ansible_item_label': 80}) => {
"msg": "for port: 80, netstat stdout was: "
}
PLAY RECAP *********************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
Wednesday 13 March 2019 06:46:55 +0200 (0:00:00.226) 0:00:01.775 *******
===============================================================================
netstat ----------------------------------------------------------------------------------------------------------------------------------------- 1.39s
print result ------------------------------------------------------------------------------------------------------------------------------------ 0.23s
[ilias@orangehat-29 temp]$
希望有帮助