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
  vars:
    - apt_file: /etc/apt/sources.list.d/google-chrome.list
  tasks:
    - 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 - 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={{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

Michael is a polyglot software engineer, committed to reducing complexity in systems and making them more predictable. Working with a variety of languages and tools, he shares his technical expertise to audiences all around the world at user groups and conferences. You can follow @mheap on Twitter

Thoughts on this post

Steve 2017-12-05

Will this work on a Mac?

Michael Heap 2017-12-12

I’m afraid not. If you’re on a mac I’d recommend using Homebrew and `brew cask`. You can run this via Ansible too if you like. There’s an example in my dotfiles

Leave a comment?

Leave a Reply