Adding custom Firefox config with Ansible

Posted: 2023-01-26 21:05:42 by Alasdair Keyes

Direct Link | RSS feed


I've been writing Ansible playbooks to build my laptop and I came across a problem with applying my custom Firefox user.js config file to a new build.

Firefox won't create config/profile directory until it's started for the first time. During this first start a random profile directory is created in which the user.js file needs to be placed e.g /.mozilla/firefox/wxyz1234.default-release/.

As such you can't just install user.js with a basic file copy.

To counter this I wrote the following play so that if there was no Firefox profile folder, Ansible will start Firefox, allow it to create the profile folder and then kill it. It will then search for the newly created profile folder and install the file.

# Install Firefox
- name: Install Firefox
  become: true
  package:
    name:
      - firefox
    state: present

# Check if a profile folder exists
- name: Check Firefox config folder
  find:
    paths: "{{ ansible_user_dir }}/.mozilla/firefox"
    patterns: '^.*\.default-release'
    use_regex: true
    file_type: directory
  register: firefox_config_folder_search

# If profile folder doesn't exist, start Firefox
- name: Starting Firefox
  shell:
    cmd: "firefox &"
  changed_when: false
  when: firefox_config_folder_search.matched == 0

- name: Waiting for Firefox to start
  command: sleep 10s
  changed_when: false
  when: firefox_config_folder_search.matched == 0

# Kill Firefox
- name: Killing Firefox
  shell:
    cmd: kill $(pidof firefox)
  changed_when: false
  when: firefox_config_folder_search.matched == 0

# Search for the newly created profile directory
- name: Check Firefox config folder again
  find:
    paths: "{{ ansible_user_dir }}/.mozilla/firefox"
    patterns: '^.*\.default-release'
    use_regex: true
    file_type: directory
  register: firefox_config_folder_search

# Set a fact with the profile directory path
- name: Set firefox folder name as fact
  set_fact:
    firefox_config_folder_path: "{{ firefox_config_folder_search.files[0].path }}"

# Add in the custom config
- name: Add in Firefox config
  copy:
    src: files/firefox/user.js
    dest: "{{ firefox_config_folder_path }}/user.js"
    owner: "{{ ansible_user_id }}"
    group: "{{ ansible_user_id }}"
    mode: 0644
  when: firefox_config_folder_path != 'false'

The play is idempotent so you can run it as many times as you want and it will continue to give you a nice green response.

I only ever use a single Firefox profile so I don't need to ensure different configs, but it could be extended to take this into account if you needed.

I later found some other software I use has this same config issue so I extracted the process start/kill tasks into a separate file

- name: Starting process {{ item }}
  shell:
    cmd: "{{ item }} &"
  changed_when: false

- name: Waiting for proces {{ item }} to start
  command: sleep 10s
  changed_when: false

- name: Killing process {{ item }}
  shell:
    cmd: kill $(pidof {{ item }})
  changed_when: false

Which can then be called with the following in your playbook

- name: Start/Kill myprogram to generate config
  include_tasks: start-kill-process.yml
  loop:
    - myprogram


If you found this useful, please feel free to donate via bitcoin to 1NT2ErDzLDBPB8CDLk6j1qUdT6FmxkMmNz

© Alasdair Keyes

IT Consultancy Services

I'm now available for IT consultancy and software development services - Cloudee LTD.



Happy user of Digital Ocean (Affiliate link)


Version:master-3093fcc3d4


Validate HTML 5