如何判断一个脚本之前是否运行使用过Ansible?
How to determine whether a script has previously run using Ansible?
我正在使用 Ansible 将框架部署(Git 克隆,运行 安装脚本)到服务器。安装步骤意味着 运行 像这样安装 install.sh
脚本:
- name: Install Foo Framework
shell: ./install.sh
args:
chdir: ~/foo
如何判断是否在上一个运行的Ansible中执行过这一步?我想在此步骤中添加一个 when
条件,该条件仅在 install.sh
脚本之前未被 运行 时执行。
install.sh
脚本做了一些事情(替换了用户主目录中的一些文件),但仅仅看一下文件就不清楚脚本是否是 运行 之前的. ~/foo.sh
文件可能之前就存在,不清楚是被安装脚本替换了还是之前就存在。
Ansible 有没有办法在服务器上存储一个值,让我确定这个特定任务之前是否已经执行过?或者我应该只在用户的主目录中创建一个标记文件(例如 ~/foo-installed
),以便我在以后调用剧本时检查?
我建议改用 script
模块。该模块有一个 creates
参数:
a filename, when it already exists, this step will not be run. (added in Ansible 1.5)
因此您的脚本可以简单地 touch
一个文件,该文件将阻止在后续调用中执行脚本。
最后我是这样解决的。使用 creates
选项的指针有帮助:
- name: Install Foo Framework
shell: ./install.sh && touch ~/foo_installed
args:
chdir: ~/foo
creates: ~/foo_installed
使用这种方法,~/foo_installed
文件仅在安装脚本无错误完成时创建。
我正在使用 Ansible 将框架部署(Git 克隆,运行 安装脚本)到服务器。安装步骤意味着 运行 像这样安装 install.sh
脚本:
- name: Install Foo Framework
shell: ./install.sh
args:
chdir: ~/foo
如何判断是否在上一个运行的Ansible中执行过这一步?我想在此步骤中添加一个 when
条件,该条件仅在 install.sh
脚本之前未被 运行 时执行。
install.sh
脚本做了一些事情(替换了用户主目录中的一些文件),但仅仅看一下文件就不清楚脚本是否是 运行 之前的. ~/foo.sh
文件可能之前就存在,不清楚是被安装脚本替换了还是之前就存在。
Ansible 有没有办法在服务器上存储一个值,让我确定这个特定任务之前是否已经执行过?或者我应该只在用户的主目录中创建一个标记文件(例如 ~/foo-installed
),以便我在以后调用剧本时检查?
我建议改用 script
模块。该模块有一个 creates
参数:
a filename, when it already exists, this step will not be run. (added in Ansible 1.5)
因此您的脚本可以简单地 touch
一个文件,该文件将阻止在后续调用中执行脚本。
最后我是这样解决的。使用 creates
选项的指针有帮助:
- name: Install Foo Framework
shell: ./install.sh && touch ~/foo_installed
args:
chdir: ~/foo
creates: ~/foo_installed
使用这种方法,~/foo_installed
文件仅在安装脚本无错误完成时创建。