在日常使用ansible playbook的过程中,我们有时候希望做一下补救性的操作,做一些判断,
例如:
1 2 3 4 5 6 7 8 9 10 11 |
tasks: - block: - debug: msg='i execute normally' - command: /bin/false - debug: msg='i never execute, cause ERROR!' rescue: - debug: msg='I caught an error' - command: /bin/false - debug: msg='I also never execute :-(' always: - debug: msg="this always executes" |
如上的代码,第一部分出错后,会被rescue捕捉到,然后做一些补救性的工作,这个时候我们可以做一些有趣的任务例如:
1:我们要对httpd 的配置文件进行一些修改
如果http没有安装,我们肯定会报错,没有办法正常运行,这个时候我们使用rescue
2: rescue里我们安装httpd 然后 做一些修改
3: always 是无论时间时候都执行的,所以,我们一般第三步是把保证服务的状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
tasks: - block: - name: Create {{ maildir_path }} copy: src: "{{ maildir }}" dest: "{{ maildir_path }}" mode: 0755 register: command_output rescue: - name: Install mail packages yum: name: "{{ item }}" state: latest with_items: - "{{ mail_package }}" - dovecot always: - name: start the mail service service: name: "{{ mail_service }}" state: restarted |
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021
广州网站建设 2018/07/24 10:10