Over a million developers have joined DZone.

Managing Dotfiles With Ansible

DZone's Guide to

Managing Dotfiles With Ansible

How to use Ansible's configuration management tools with zsh configuration.

· DevOps Zone ·
Free Resource

In response to accelerated release cycles, a new set of testing capabilities is now required to deliver quality at speed. This is why there is a shake-up in the testing tools landscape—and a new leader has emerged in the just released Gartner Magic Quadrant for Software Test Automation.

The other day I posted about managing our local configuration with Ansible and today I’m going to continue this path by putting my zsh configuration under configuration management.

Installing ZSH and oh-my-zsh

First up let’s install our preferred shell and customizations. For me this is zsh and oh-my-zsh. Up front I know that this is going to probably be a multi-step process so I’m going to create a local role to bundle up the tasks and templates we’ll be using. I create a new directory named roles and add it to the roles_path in our ansible.cfg. This new directory with our new role will look like the following.

$ tree roles          
└── jamescarr.dotfiles
    └── tasks
        └── main.yaml

For our initial set of tasks we’ll install zsh via homebrew, configure zsh as the current user’s shell and install oh-my-zsh.

- name: Install ZSH
    name: zsh
    state: latest

- name: Change current user shell
    name: "{{ ansible_ssh_user }}"
    shell: /bin/zsh
  become: yes

- name: Fetch oh-my-zsh
    url: https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh
    dest: /tmp/install-oh-my-zsh.sh
    mode: 0755

- name: Install oh-my-zsh
  command: zsh /tmp/install-oh-my-zsh.sh

ZSH_THEME="{{ zsh_theme }}"


plugins=({{ oh_my_zsh_plugins|join(' ') }})

You can see this change in its entirety here. Next up we’ll add a template for our zshrc that we can customize to our liking. To start we’ll grab the zshrc template from the oh-my-zsh git repository and save it to jamescarr.dotfiles/templates/zshrc.j2.

A good first piece of literal text to template out is the zsh theme and the plugins loaded up. We’ll define these with default variables under jamescarr.dotfiles/defaults/main.yaml.

zsh_theme: robbyrussell

  - git

- name: Backup existing zshrc if it exists
    src: "/Users/{{ ansible_ssh_user }}/.zshrc"
    dest: "/Users/{{ ansible_ssh_user }}/.zshrc-backup.{{ ansible_date_time.epoch }}"

- name: Render zshrc
    src: zshrc.j2
    dest: "/Users/{{ ansible_ssh_user }}/.zshrc"

You’ll notice here we’ll also be polite by backing up the existing zshrc file if it is present. A good benefit with this example is that we can now switch up the different pieces of our .zshrc configuration from our playbook by overriding the default variables.

    - role: geerlingguy.homebrew
    - role: jamescarr.dotfiles
      zsh_theme: bureau
        - git
        - osx

You can find everything up to this point at this referenced commit in jamescarr/ansible-mac-demo.

Up Next

Obviously a topic like zsh customizations is a rather large undertaking deserving of its own post so tomorrow I’ll share some of my personal zsh functions and aliases that I find extremely useful to start off with.

Well did you find the useful? Did you run into any problems following along? Please let me know in the comments below.

Gartner: Digital Transformation, DevOps, and the Future of Testing. Download Now! 


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}