Ansible: Installing Google Chrome on Ubuntu

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.

- hosts: all
  sudo: True
    - apt_file: /etc/apt/sources.list.d/google-chrome.list
    - name: Does the Google apt file exist?
      command: test -f {{apt_file}}
      register: google_apt_exists
      ignore_errors: True

    - name: Add Google Chrome key
      shell: wget -q -O - | apt-key add -
      when: google_apt_exists.rc == 1

    - name: Add Google Chrome repo
      copy: content="deb stable main" dest={{apt_file}} 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