ZSH

Managing Dotfiles With Ansible

Yesterday 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 rolesand add it to the roles_path in our ansible.cfg. This new directory with our new role will look like the following.

$ tree roles          
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
  homebrew:
    name: zsh
    state: latest

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

- name: Fetch oh-my-zsh
  get_url:
    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

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

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

- name: Render zshrc
  template:
    src: zshrc.j2
    dest: "/Users/{{ ansible_ssh_user }}/.zshrc"
ZSH_THEME="{{ zsh_theme }}"

...

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

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.

  roles:
    - role: geerlingguy.homebrew
    - role: jamescarr.dotfiles
      zsh_theme: bureau
      oh_my_zsh_plugins:
        - 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!

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button