Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am creating a ansible playbook for configuring our build systems. Part of it I started of writing roles for installing java (open JDK and Oracle JDK) for CentOS. Open JDK is available through YUM package manager so no issues in idempotency there. For oracle Java, I need to download, install, symlink it and clean up. 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. What I am not liking is using When statement for all four steps in installing jdk. 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?

- 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
like image 417
Krish Avatar asked Sep 26 '15 21:09

Krish


2 Answers

In Ansible 2.x, you can do it like this:

- 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
like image 81
Arbab Nazar Avatar answered Oct 22 '22 19:10

Arbab Nazar


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). On 1.x you could package your java things into a role and do a "when" statement on the role.

like image 37
Trondh Avatar answered Oct 22 '22 19:10

Trondh