botang 发表于 2020-8-15 15:52:43

ANSIBLE14(block-rescue-always)

---
- name: ansible (block & resuce & always)
hosts:srvgroup
gather_facts: no
tasks:
- name: block & rescue
    block:
    - name: test file is exists
      shell: "ls /tmp/XXX"

    rescue:
    - name: create file
      copy:
      content: "welcome to XXX\n"
      dest: "/tmp/XXX"

    always:
    - name: welcome
      shell: "cat /tmp/XXX"
      register: v_result

    - name: Display
      debug:
      msg: "{{ v_result.stdout }}"

对照每一步的输出:

---
- name: ansible (block & resuce & always)
hosts:srvgroup
gather_facts: no



$ ansible-playbookv6-8_srvgroup.yml

PLAY *********************************************************************************************************************************

tasks:
- name: block & rescue
    block:
    - name: test file is exists
      shell: "ls /tmp/XXX"



TASK ***********************************************************************************************************************************************
fatal: : FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": true, "cmd": "ls /tmp/XXX", "delta": "0:00:00.009540", "end": "2020-08-15 16:22:07.181198", "msg": "non-zero return code", "rc": 2, "start": "2020-08-15 16:22:07.171658", "stderr": "ls: cannot access '/tmp/XXX': No such file or directory", "stderr_lines": ["ls: cannot access '/tmp/XXX': No such file or directory"], "stdout": "", "stdout_lines": []}
fatal: : FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": true, "cmd": "ls /tmp/XXX", "delta": "0:00:00.025590", "end": "2020-08-15 16:22:07.338628", "msg": "non-zero return code", "rc": 2, "start": "2020-08-15 16:22:07.313038", "stderr": "ls: cannot access '/tmp/XXX': No such file or directory", "stderr_lines": ["ls: cannot access '/tmp/XXX': No such file or directory"], "stdout": "", "stdout_lines": []}
fatal: : FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"}, "changed": true, "cmd": "ls /tmp/XXX", "delta": "0:00:00.007247", "end": "2020-08-15 16:22:07.136683", "msg": "non-zero return code", "rc": 2, "start": "2020-08-15 16:22:07.129436", "stderr": "ls: cannot access '/tmp/XXX': No such file or directory", "stderr_lines": ["ls: cannot access '/tmp/XXX': No such file or directory"], "stdout": "", "stdout_lines": []}


    rescue:
    - name: create file
      copy:
      content: "welcome to XXX\n"
      dest: "/tmp/XXX"
TASK *******************************************************************************************************************************************************
changed:
changed:
changed:

    always:
    - name: welcome
      shell: "cat /tmp/XXX"
      register: v_result


TASK ***********************************************************************************************************************************************************
changed:
changed:
changed:




    - name: Display
      debug:
      msg: "{{ v_result.stdout }}"




TASK ***********************************************************************************************************************************************************
ok: => {
    "msg": "welcome to XXX"
}
ok: => {
    "msg": "welcome to XXX"
}
ok: => {
    "msg": "welcome to XXX"
}

另外一个例子:
---
- name: 处理任务失败的实验
hosts: server3.example.com
tasks:
- name: install services
    block:
    - name: install
      yum:
      name: http
      state: latest

    rescue:
    - name: install mysql
      yum:
      name: mariadb-server
      state: latest

    always:
    - name: restart mariadb
      service:
      name: mariadb
      state: started





页: [1]
查看完整版本: ANSIBLE14(block-rescue-always)