简体   繁体   English

如何将一组任务分组到一个可用的剧本块中?

[英]How to group set of tasks into a block in ansible playbook?

I am creating a ansible playbook for configuring our build systems. 我正在创建一个用于配置构建系统的ansible playbook。 Part of it I started of writing roles for installing java (open JDK and Oracle JDK) for CentOS. 部分原因是我开始为CentOS编写安装java(开放JDK和Oracle JDK)的角色。 Open JDK is available through YUM package manager so no issues in idempotency there. 通过YUM包管理器可以打开JDK,因此在幂等方面没有问题。 For oracle Java, I need to download, install, symlink it and clean up. 对于oracle Java,我需要下载,安装,符号链接和清理。 In order to create idempotency I am looking for neater way of doing it, for example here is my code. 为了创造幂等性,我正在寻找更简洁的方法,例如这里是我的代码。 Basically I am checking for a symlink to determine if java is installed or not and register a variable to use it in WHEN module later. 基本上我正在检查符号链接以确定是否安装了java,并注册一个变量以便稍后在WHEN模块中使用它。 What I am not liking is using When statement for all four steps in installing jdk. 我不喜欢的是在安装jdk的所有四个步骤中使用When语句。 how can I group all the four steps (download, install, symlink and cleanup) into a block and make them all run based on one when statement? 如何将所有四个步骤(下载,安装,符号链接和清理)分组到一个块中,并根据一个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

In Ansible 2.x, you can do it like this: 在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

If you're on Ansible 2.0 you could use the new "block" feature (see presentation of the new features here: http://www.slideshare.net/jimi-c/whats-new-in-v2-ansiblefest-london-2015 ). 如果您使用的是Ansible 2.0,则可以使用新的“阻止”功能(请参阅此处的新功能演示文稿: http ://www.slideshare.net/jimi-c/whats-new-in-v2-ansiblefest-london -2015 )。 On 1.x you could package your java things into a role and do a "when" statement on the role. 在1.x上,您可以将Java内容打包成一个角色,并对该角色执行“when”语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM