Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Ansible中的处理程序处理失败时的回滚?_Ansible - Fatal编程技术网

如何使用Ansible中的处理程序处理失败时的回滚?

如何使用Ansible中的处理程序处理失败时的回滚?,ansible,Ansible,我在下面写了yml文件,该文件将安装SSM和cloudwatch代理,但我希望在安装过程中出现任何故障时回滚安装。我尝试使用失败,但不起作用请告知 --- # tasks file for SSMAgnetInstall - name: status check command: systemctl status amazon-ssm-agent register: s_status - debug: msg: "{{ s_status }}" - name: Get CPU

我在下面写了yml文件,该文件将安装SSM和cloudwatch代理,但我希望在安装过程中出现任何故障时回滚安装。我尝试使用失败,但不起作用请告知

---
# tasks file for SSMAgnetInstall
- name: status check
  command: systemctl status amazon-ssm-agent
  register: s_status
- debug:
    msg: "{{ s_status  }}"

- name: Get CPU architecture
  command: getconf LONG_BIT
  register: cpu_arch
  changed_when: False
  check_mode: no
  when: s_status.stdout == ""
  ignore_errors: true

- name: Install rpm file for Redhat Family (Amazon Linux, RHEL, and CentOS) 32/64-bit
  yum:
    name: "https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_386/amazon-ssm-agent.rpm"
    state: present
  when: s_status.stdout == ""
  become: yes
  ignore_errors: true

- name: cloud status check
  command: systemctl status amazon-cloudwatch-agent
  register: cld_status
  become: yes
- debug:
    msg: "{{ cld_status  }}"

- name: Register to cloud watch service
  become: yes
  become_user: root
  service:
    name: amazon-ssm-agent
    enabled: yes
    state: started

- name: copy the output to a local file
  copy:
    content: "{{ myshell_output.stdout }}"
    dest: "/home/ansible/rama/output.txt"
  delegate_to: localhost

您应该看看,更具体地说,是错误处理部分。这是一个过于简单的例子的总体思路,您必须适应您的具体情况

test.yml
playbook

---
-主机:本地主机
收集事实:错误
任务:
-区块:
-姓名:我是一个可能失败的任务
调试:
msg:“I{{gen_fail}默认值(false){bool}三元('failed','succeed')}”
失败时间:gen_fail | default(false)| bool
-姓名:我是一个永远不会失败的任务
调试:
我成功了
救援:
-名称:我是一个任务,在发生故障时在块中播放
调试:
消息:救援任务
始终:
-姓名:我是一个任务,无论发生什么,我都会执行
调试:
msg:总是任务
正常播放(无故障)

被迫失败

$ ansible-playbook test.yml -e gen_fail=true

PLAY [localhost] ************************************************************************

TASK [I am a task that can fail] ********************************************************
fatal: [localhost]: FAILED! => {
    "msg": "I failed"
}

TASK [I am a task in a block played when a failure happens] *****************************
ok: [localhost] => {
    "msg": "rescue task"
}

TASK [I am a task always played whatever happens] ***************************************
ok: [localhost] => {
    "msg": "always task"
}

PLAY RECAP ******************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=1    ignored=0
$ ansible-playbook test.yml -e gen_fail=true

PLAY [localhost] ************************************************************************

TASK [I am a task that can fail] ********************************************************
fatal: [localhost]: FAILED! => {
    "msg": "I failed"
}

TASK [I am a task in a block played when a failure happens] *****************************
ok: [localhost] => {
    "msg": "rescue task"
}

TASK [I am a task always played whatever happens] ***************************************
ok: [localhost] => {
    "msg": "always task"
}

PLAY RECAP ******************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=1    ignored=0