【问题标题】:How to make sure an ansible playbook runs completely如何确保 ansible playbook 完全运行
【发布时间】:2015-08-31 07:12:53
【问题描述】:

我正在运行这个 ansible playbook:

---
- 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

此 playbook 在任务“install build-essential”期间失败,并因要求运行 dpkg --configure -a 的错误而停止。 如何通过运行命令确保在遇到此错误后再次运行 playbook

dpkg --configure -a

首先,然后继续其他任务。

【问题讨论】:

    标签: bash debian ansible ansible-playbook dpkg


    【解决方案1】:

    Ansible 通常是幂等的。这意味着您可以在解决问题后再次运行您的剧本,而不会发生冲突。

    这并不总是正确的。如果你有一个更复杂的游戏并根据另一个任务的结果执行任务,这很容易中断,一个失败的任务会让你进入一个不容易用 Ansible 修复的状态。但您提供的任务并非如此。

    如果您想加快速度并跳过所有未失败的任务和/或主机,您可以使用--limit 和/或--start-at-task

    当 playbook 失败时,您可能会注意到 Ansible 显示一条消息,其中包含一条命令,该命令使您能够将播放限制为失败的主机。因此,如果只有 1 台主机发生故障,则无需在所有主机上运行 playbook:

    ansible-playbook ... --limit @/Users/your-username/name-of-playbook.retry
    

    要从特定任务开始,您可以使用--start-at-task。因此,如果您的剧本在“安装构建必备”任务中失败,您可以从该任务重新开始并跳过所有以前的任务:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      • 2013-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多