ansible jar检查是否已安装

时间:2017-04-07 23:21:47

标签: tomcat jar ansible

我有一个jar文件,用一个网站安装tomcat。所以我有以下任务download jar fileinstall jar文件和delete jar file。但是我想要ansible告诉它已经安装而不是下载并尝试再次安装。我怎么能实现这样的目标?

- name: Downloading installer
  get_url:
  url: http://[URl]/installfile.jar
  dest: "/opt"

- name: Installing jar file
  shell: java -jar "/opt/installfile.jar"

- name: Removing installer
  file:
  state: absent
  path: "/opt/installfile.jar"

1 个答案:

答案 0 :(得分:0)

您至少可以在安装任务上使用creates arg,这样只有当某个文件尚不存在时,java程序才会运行。 Ansible将在第一次运行任务时创建此类文件。

类似的东西:

- name: Installing jar file
  shell: java -jar "/opt/installfile.jar"
  args:
    creates: /opt/installfile-check

或者,如果您想要仅在未安装Tomcat时运行三个任务,则需要首先运行一个程序来检查它是否已安装,并将其结果注册到其他任务可用于确定它们是否可用的变量中需要使用when运行。

- name: Check to see if Tomcat is installed
  shell: "command --that --checks" # Just an example, obviously
  register: tomcat_is_installed

然后,只有在未安装Tomcat playbook时才能包含它:

- name: This playbook will only be included when Tomcat is not installed
  include: tomcat-playbook.yml
  when: tomcat_is_installed.rc != 0