[英]How to make sure an ansible playbook runs completely
我正在运行这本有趣的剧本:
---
- hosts: localhost
remote_user: root
tasks:
- name : update system
apt : update_cache=yes
- name : install m4
apt : name=m4 state=present
- name : install build-essential
apt : name=build-essential state=present
- name : install gcc
apt : name=gcc state=present
- name : install gfortran
apt : name=gfortran state=present
- name : install libssl-dev
apt : name=libssl-dev state=present
- name : install python-software-properties
apt : name=python-software-properties state=present
- name : add sage ppa repo
apt_repository: repo='ppa:aims/sagemath'
- name : update system
apt : update_cache=yes
- name : install dvipng
apt : name=dvipng state=present
- name : install sage binary
apt : name=sagemath-upstream-binary state=present
- name : invoke create_sagenb script
command: /usr/bin/screen -d -m sudo /root/databases-and-datamining-iiith/python-scripts/create_sagenb -i -y
- name : invoke start_sage script
command: /usr/bin/screen -d -m sudo /root/databases-and-datamining-iiith/python-scripts/start_sage -i -y
该剧本在任务“ install build-essential
”期间失败,并因要求运行dpkg --configure -a
错误而停止。 如何通过运行命令确保在遇到此错误后剧本再次运行
dpkg --configure -a
首先,然后继续其他任务。
Ansible通常是幂等的。 这意味着您可以在解决问题后无冲突地简单地再次运行剧本。
这并非总是如此。 如果您根据另一个任务的结果进行更复杂的游戏并执行任务,则这很容易中断,失败的任务会使您陷入不易被Ansible修复的状态。 但是您提供的任务并非如此。
如果要加快速度并跳过所有未失败的任务和/或主机,则可以使用--limit
和/或--start-at-task
:
当剧本失败时,您可能会注意到Ansible会显示一条消息,其中包含一条命令,该命令使您可以将播放限制在失败的主机上。 因此,如果仅一台主机发生故障,则无需在所有主机上运行剧本:
ansible-playbook ... --limit @/Users/your-username/name-of-playbook.retry
要开始特定任务,可以使用--start-at-task
。 因此,如果您的剧本在“ install build-essential”任务上失败,则可以从此任务重新开始,并跳过所有先前的任务:
ansible-playbook ... --start-at-task="install build-essential"
附带说明一下,apt模块经过优化可与循环一起使用。 您可以通过将任务合并为一个单独的apt任务来加快播放速度:
tasks:
- name: Install packages that we need for need for apt_repository
apt: update_cache=yes
name={{ item }}
state=present
cache_valid_time=3600
with_items:
- python-software-properties
- python-software-properties-common
- name: add sage ppa repo
apt_repository: repo='ppa:aims/sagemath'
- name: Install packages
apt: update_cache=yes
cache_valid_time=3600
name={{ item }}
state=present
with_items:
- m4
- build-essential
- gcc
- gfortran
- libssl-dev
- dvipng
- sagemath-upstream-binary
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.