我正在创建一个用于配置构建系统的ansible playbook。部分原因是我开始为CentOS编写安装java(开放JDK和Oracle JDK)的角色。通过YUM包管理器可以打开JDK,因此在幂等方面没有问题。对于oracle Java,我需要下载,安装,符号链接和清理。为了创造幂等性,我正在寻找更简洁的方法,例如这里是我的代码。基本上我正在检查符号链接以确定是否安装了java,并注册一个变量以便稍后在WHEN模块中使用它。我不喜欢的是在安装jdk的所有四个步骤中使用 When 语句。如何将所有四个步骤(下载,安装,符号链接和清理)分组到一个块中,并根据一个何时语句使它们全部运行?
- name: Check if Java 8 is instaled
stat: path=~/java/oraclejdk8
register: oraclejdk8_sym
- name: Download Java 8
command: "wget --no-cookies -O {{ jdk_download_path }}/{{ oraclejdk8.jdk_rpm_file }} --no-check-certificate --header 'Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie' {{ oraclejdk8.jdk_rpm_url }}"
when: oraclejdk8_sym.stat.islnk is not defined
- name: Install Java 8
yum: name={{ java_archive }} state=present
when: oraclejdk8_sym.stat.islnk is not defined
- name: Symlink to ~/java/oraclejdk8
file: path=~/java/ state=directory mode=0755
command: "ln -s /usr/java/jdk{{ oraclejdk8.jdk_version }} ~/java/oraclejdk8"
when: oraclejdk8_sym.stat.islnk is not defined
- name: Clean up
file: state=absent path={{ jdk_download_path}}/{{ oraclejdk8.jdk_rpm_file }}
when: oraclejdk8_sym.stat.islnk is not defined
答案 0 :(得分:13)
在Ansible 2.x中,你可以这样做:
- name: Check if Java 8 is instaled
stat: path=~/java/oraclejdk8
register: oraclejdk8_sym
- block:
- name: Download Java 8
command: "wget --no-cookies -O {{ jdk_download_path }}/{{ oraclejdk8.jdk_rpm_file }} --no-check-certificate --header 'Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie' {{ oraclejdk8.jdk_rpm_url }}"
- name: Install Java 8
yum: name={{ java_archive }} state=present
- name: Symlink to ~/java/oraclejdk8
file: path=~/java/ state=directory mode=0755
- command: "ln -s /usr/java/jdk{{ oraclejdk8.jdk_version }} ~/java/oraclejdk8"
- name: Clean up
file: state=absent path={{ jdk_download_path}}/{{ oraclejdk8.jdk_rpm_file }}
when: oraclejdk8_sym.stat.islnk is not defined
答案 1 :(得分:4)
如果您使用的是Ansible 2.0,则可以使用新的"块"功能(请参阅此处的新功能演示:http://www.slideshare.net/jimi-c/whats-new-in-v2-ansiblefest-london-2015)。在1.x上,你可以将你的java东西打包成一个角色并做一个"当"关于角色的声明。