Ansible: Installing Google Chrome on Ubuntu

09 Mar 2014 in TIL

Just a quick playbook for installing Google Chrome on Ubuntu. The playbook checks if the Google Chrome apt repo is added, then if it isn't it adds Google's public key, creates the file and then updates the apt cache. Finally, it tries to install Google Chrome.

This playbook's interesting as it uses a combination of register, ignore_errors and when. If the file doesn't exist, the return code of the test would be 1, indicating that there was an error. I then use that as the when condition of the following steps to only perform the commands when needed.

yaml
---
- hosts: all
sudo: True
vars:
- apt_file: /etc/apt/sources.list.d/google-chrome.list
tasks:
- name: Does the Google apt file exist?
command: test -f
register: google_apt_exists
ignore_errors: True
- name: Add Google Chrome key
shell: wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
when: google_apt_exists.rc == 1
- name: Add Google Chrome repo
copy: content="deb http://dl.google.com/linux/chrome/deb/ stable main" dest= owner=root group=root mode=644
when: google_apt_exists.rc == 1
- name: Update apt cache
apt: update_cache=yes
when: google_apt_exists.rc == 1
- name: Install Google Chrome
apt: pkg=google-chrome-stable state=installed