|
- ---
- - name: Restart httpd if postfix running
- hosts: webgroup
- tasks:
- - name: Get postfix Server Status
- shell: "systemctl is-active postfix"
- ignore_errors: yes
- register: v_result
-
- # - name: debug info
- # debug:
- # var: v1
- - name: Restart httpd Based on Server Status
- service:
- name: httpd
- state: restarted
- when: v_result.failed == true
-
复制代码 上面这个剧本,改进后:
- ---
- - name: Restart httpd if postfix running
- hosts: webgroup
- vars:
- v_service:
- - postfix
- - dovecot
- tasks:
- - name: Get postfix Server Status
- shell: "systemctl is-active {{ item }}"
- ignore_errors: yes
- loop:
- "{{ v_service }}"
- register: v_result
- # - name: debug info
- # debug:
- # var: v_result
- - name: Restart httpd Based on Server Status
- service:
- name: httpd
- state: restarted
- when: item.failed == true
- loop: "{{ v_result.results }}"
复制代码 输出:
[student@classroom ansible]$ ansible-playbook v5-7_webgroup.yml
PLAY [Restart httpd if postfix running] ********************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [server2.example.com]
ok: [server1.example.com]
TASK [Get postfix Server Status] ***************************************************************************************************************************************
changed: [server2.example.com] => (item=postfix)
changed: [server1.example.com] => (item=postfix)
changed: [server2.example.com] => (item=dovecot)
changed: [server1.example.com] => (item=dovecot)
TASK [Restart httpd Based on Server Status] ****************************************************************************************************************************
skipping: [server1.example.com] => (item={'cmd': 'systemctl is-active postfix', 'stdout': 'active', 'stderr': '', 'rc': 0, 'start': '2020-08-12 20:22:28.782331', 'end': '2020-08-12 20:22:28.801580', 'delta': '0:00:00.019249', 'changed': True, 'invocation': {'module_args': {'_raw_params': 'systemctl is-active postfix', '_uses_shell': True, 'warn': True, 'stdin_add_newline': True, 'strip_empty_ends': True, 'argv': None, 'chdir': None, 'executable': None, 'creates': None, 'removes': None, 'stdin': None}}, 'stdout_lines': ['active'], 'stderr_lines': [], 'failed': False, 'item': 'postfix', 'ansible_loop_var': 'item'})
skipping: [server1.example.com] => (item={'cmd': 'systemctl is-active dovecot', 'stdout': 'active', 'stderr': '', 'rc': 0, 'start': '2020-08-12 20:22:30.475434', 'end': '2020-08-12 20:22:30.500832', 'delta': '0:00:00.025398', 'changed': True, 'invocation': {'module_args': {'_raw_params': 'systemctl is-active dovecot', '_uses_shell': True, 'warn': True, 'stdin_add_newline': True, 'strip_empty_ends': True, 'argv': None, 'chdir': None, 'executable': None, 'creates': None, 'removes': None, 'stdin': None}}, 'stdout_lines': ['active'], 'stderr_lines': [], 'failed': False, 'item': 'dovecot', 'ansible_loop_var': 'item'})
skipping: [server2.example.com] => (item={'cmd': 'systemctl is-active postfix', 'stdout': 'active', 'stderr': '', 'rc': 0, 'start': '2020-08-12 20:22:28.558531', 'end': '2020-08-12 20:22:28.582796', 'delta': '0:00:00.024265', 'changed': True, 'invocation': {'module_args': {'_raw_params': 'systemctl is-active postfix', '_uses_shell': True, 'warn': True, 'stdin_add_newline': True, 'strip_empty_ends': True, 'argv': None, 'chdir': None, 'executable': None, 'creates': None, 'removes': None, 'stdin': None}}, 'stdout_lines': ['active'], 'stderr_lines': [], 'failed': False, 'item': 'postfix', 'ansible_loop_var': 'item'})
skipping: [server2.example.com] => (item={'cmd': 'systemctl is-active dovecot', 'stdout': 'active', 'stderr': '', 'rc': 0, 'start': '2020-08-12 20:22:30.207918', 'end': '2020-08-12 20:22:30.262214', 'delta': '0:00:00.054296', 'changed': True, 'invocation': {'module_args': {'_raw_params': 'systemctl is-active dovecot', '_uses_shell': True, 'warn': True, 'stdin_add_newline': True, 'strip_empty_ends': True, 'argv': None, 'chdir': None, 'executable': None, 'creates': None, 'removes': None, 'stdin': None}}, 'stdout_lines': ['active'], 'stderr_lines': [], 'failed': False, 'item': 'dovecot', 'ansible_loop_var': 'item'})
PLAY RECAP *************************************************************************************************************************************************************
server1.example.com : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
server2.example.com : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
[student@classroom ansible]$ ansible webgroup -m shell -a 'systemctl stop postfix; systemctl stop dovecot'
server2.example.com | CHANGED | rc=0 >>
server1.example.com | CHANGED | rc=0 >>
[student@classroom ansible]$ ansible-playbook v5-7_webgroup.yml
PLAY [Restart httpd if postfix running] ********************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************
ok: [server1.example.com]
ok: [server2.example.com]
TASK [Get postfix Server Status] ***************************************************************************************************************************************
failed: [server1.example.com] (item=postfix) => {"ansible_loop_var": "item", "changed": true, "cmd": "systemctl is-active postfix", "delta": "0:00:00.024278", "end": "2020-08-12 20:23:59.992670", "item": "postfix", "msg": "non-zero return code", "rc": 3, "start": "2020-08-12 20:23:59.968392", "stderr": "", "stderr_lines": [], "stdout": "inactive", "stdout_lines": ["inactive"]}
failed: [server2.example.com] (item=postfix) => {"ansible_loop_var": "item", "changed": true, "cmd": "systemctl is-active postfix", "delta": "0:00:00.041624", "end": "2020-08-12 20:23:59.904393", "item": "postfix", "msg": "non-zero return code", "rc": 3, "start": "2020-08-12 20:23:59.862769", "stderr": "", "stderr_lines": [], "stdout": "inactive", "stdout_lines": ["inactive"]}
failed: [server2.example.com] (item=dovecot) => {"ansible_loop_var": "item", "changed": true, "cmd": "systemctl is-active dovecot", "delta": "0:00:00.027463", "end": "2020-08-12 20:24:01.706834", "item": "dovecot", "msg": "non-zero return code", "rc": 3, "start": "2020-08-12 20:24:01.679371", "stderr": "", "stderr_lines": [], "stdout": "inactive", "stdout_lines": ["inactive"]}
...ignoring
failed: [server1.example.com] (item=dovecot) => {"ansible_loop_var": "item", "changed": true, "cmd": "systemctl is-active dovecot", "delta": "0:00:00.020183", "end": "2020-08-12 20:24:01.851202", "item": "dovecot", "msg": "non-zero return code", "rc": 3, "start": "2020-08-12 20:24:01.831019", "stderr": "", "stderr_lines": [], "stdout": "inactive", "stdout_lines": ["inactive"]}
...ignoring
TASK [Restart httpd Based on Server Status] ****************************************************************************************************************************
changed: [server2.example.com] => (item={'msg': 'non-zero return code', 'cmd': 'systemctl is-active postfix', 'stdout': 'inactive', 'stderr': '', 'rc': 3, 'start': '2020-08-12 20:23:59.862769', 'end': '2020-08-12 20:23:59.904393', 'delta': '0:00:00.041624', 'changed': True, 'failed': True, 'invocation': {'module_args': {'_raw_params': 'systemctl is-active postfix', '_uses_shell': True, 'warn': True, 'stdin_add_newline': True, 'strip_empty_ends': True, 'argv': None, 'chdir': None, 'executable': None, 'creates': None, 'removes': None, 'stdin': None}}, 'stdout_lines': ['inactive'], 'stderr_lines': [], 'item': 'postfix', 'ansible_loop_var': 'item'})
changed: [server1.example.com] => (item={'msg': 'non-zero return code', 'cmd': 'systemctl is-active postfix', 'stdout': 'inactive', 'stderr': '', 'rc': 3, 'start': '2020-08-12 20:23:59.968392', 'end': '2020-08-12 20:23:59.992670', 'delta': '0:00:00.024278', 'changed': True, 'failed': True, 'invocation': {'module_args': {'_raw_params': 'systemctl is-active postfix', '_uses_shell': True, 'warn': True, 'stdin_add_newline': True, 'strip_empty_ends': True, 'argv': None, 'chdir': None, 'executable': None, 'creates': None, 'removes': None, 'stdin': None}}, 'stdout_lines': ['inactive'], 'stderr_lines': [], 'item': 'postfix', 'ansible_loop_var': 'item'})
changed: [server2.example.com] => (item={'msg': 'non-zero return code', 'cmd': 'systemctl is-active dovecot', 'stdout': 'inactive', 'stderr': '', 'rc': 3, 'start': '2020-08-12 20:24:01.679371', 'end': '2020-08-12 20:24:01.706834', 'delta': '0:00:00.027463', 'changed': True, 'failed': True, 'invocation': {'module_args': {'_raw_params': 'systemctl is-active dovecot', '_uses_shell': True, 'warn': True, 'stdin_add_newline': True, 'strip_empty_ends': True, 'argv': None, 'chdir': None, 'executable': None, 'creates': None, 'removes': None, 'stdin': None}}, 'stdout_lines': ['inactive'], 'stderr_lines': [], 'item': 'dovecot', 'ansible_loop_var': 'item'})
changed: [server1.example.com] => (item={'msg': 'non-zero return code', 'cmd': 'systemctl is-active dovecot', 'stdout': 'inactive', 'stderr': '', 'rc': 3, 'start': '2020-08-12 20:24:01.831019', 'end': '2020-08-12 20:24:01.851202', 'delta': '0:00:00.020183', 'changed': True, 'failed': True, 'invocation': {'module_args': {'_raw_params': 'systemctl is-active dovecot', '_uses_shell': True, 'warn': True, 'stdin_add_newline': True, 'strip_empty_ends': True, 'argv': None, 'chdir': None, 'executable': None, 'creates': None, 'removes': None, 'stdin': None}}, 'stdout_lines': ['inactive'], 'stderr_lines': [], 'item': 'dovecot', 'ansible_loop_var': 'item'})
PLAY RECAP *************************************************************************************************************************************************************
server1.example.com : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
server2.example.com : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
|
|