Bo's Oracle Station

查看: 725|回复: 0

ANSIBLE10(ignore_errors和它的输出)

[复制链接]

1005

主题

1469

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
12012
发表于 2020-8-12 20:01:30 | 显示全部楼层 |阅读模式
  1. ---
  2. - name: Restart httpd if postfix running
  3.   hosts: webgroup
  4.   tasks:
  5.   - name: Get postfix Server Status
  6.     shell: "systemctl is-active postfix"
  7.     ignore_errors: yes
  8.     register: v_result
  9.   
  10. #  - name:  debug info
  11. #    debug:
  12. #      var: v1
  13.   - name: Restart httpd Based on Server Status
  14.     service:
  15.       name: httpd
  16.       state: restarted
  17.     when: v_result.failed == true

复制代码
上面这个剧本,改进后:
  1. ---
  2. - name: Restart httpd if postfix running
  3.   hosts: webgroup
  4.   vars:
  5.     v_service:
  6.       - postfix
  7.       - dovecot
  8.   tasks:
  9.   - name: Get postfix Server Status
  10.     shell: "systemctl is-active {{ item }}"
  11.     ignore_errors: yes
  12.     loop:
  13.       "{{ v_service }}"
  14.     register: v_result

  15. #  - name:  debug info
  16. #    debug:
  17. #      var: v_result

  18.   - name: Restart httpd Based on Server Status
  19.     service:
  20.       name: httpd
  21.       state: restarted
  22.     when: item.failed == true
  23.     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   






回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Bo's Oracle Station   

GMT+8, 2024-5-9 20:10 , Processed in 0.045423 second(s), 24 queries .

快速回复 返回顶部 返回列表