/ ansible

Using Ansible to set up Zsh

I am in the midst of setting up my new laptop, with Fedora 28, and other things that make me a happy developer. I am building ansible playlists along the way, so that I can repeat and share these steps and re-build easily on this or another machine. Below is my playlist for setting up zsh ("Z Shell").

For more context, go back and read my last 2 articles:

Just like the last two articles, I'm going to create a new ansible playbook for this task. Here is my ~/ansible-develop/zsh.yml:

---
- name: Set Up zsh
  hosts: localhost
  become_user: "{{ username }}"
  tasks:
    - name: install zsh and friends
      become: True
      become_user: root
      package: name={{ item }}
      with_items:
        - git-core
        - powerline
        - zsh

    - name: ensure the ~/src-hub directory is present
      file: path=/home/{{ username }}/src-hub
            state=directory

    - name: clone oh-my-zsh
      git: repo=https://github.com/robbyrussell/oh-my-zsh.git
           dest=/home/{{ username }}/src-hub/oh-my-zsh
           update=no

    - name: clone dircolors-solarized plugin
      git: repo=https://github.com/joel-porquet/zsh-dircolors-solarized
           dest=/home/{{ username }}/src-hub/oh-my-zsh/custom/plugins

    - name: create symlink to oh-my-zsh
      file: path=/home/{{ username }}/.oh-my-zsh
            src=/home/{{ username }}/src-hub/oh-my-zsh
            state=link

    - name: check if .zshrc exists
      stat: path=/home/{{ username }}/.zshrc
      register: zshrc

    - name: back up existing .zshrc file if it exists
      when: zshrc.stat.exists
      copy: src=/home/{{ username }}/.zshrc
            dest=/home/{{ username }}/.zshrc-orig

    - name: copy zshrc to user home
      copy: src=zshrc
            dest=/home/{{ username }}/.zshrc
            force=yes

    - name: change user shell to zsh
      become: yes
      become_user: root
      command: chsh -s /bin/zsh "{{ username }}"

You can find my ansible-develop project repository at https://github.com/ballPointPenguin/ansible-develop

This configuration makes heavy use of oh-my-zsh so go check out some of their docs and see where you might choose to configure it differently or include themes and plugins.

Before this playlist will work, you need to have a zshrc file template. I suggest you start by copying the template provided by oh-my-zsh:

mkdir ~/ansible-develop/files
wget -O ~/ansible-develop/files/zshrc https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/templates/zshrc.zsh-template

With that in place, this should now work:

ansible-playbook -K zsh.yml

And just like with Docker, you will need to log out and log back in before zsh is automatically used as your default shell. In the meantime you can launch it by typing zsh.

Extra Credit: Combining Playbooks

Now we have 3 playbooks, packages.yml, docker.yml and zsh.yml (with more to come!). Let's take a moment to create a playbook that brings them all together. I'm going to call this localhost.yml.

contents of ~/ansible-develop/localhost.yml:

---
- name: Run all the local playbooks
  hosts: localhost

- import_playbook: packages.yml
- import_playbook: docker.yml
- import_playbook: zsh.yml

Now to re-run all the playbooks, I'll just issue a single command:

ansible-playbook -K localhost.yml

Thanks for reading. Please check out the source code. Next up: vim.

Benjamin Rosas

Benjamin Rosas

I am a constantly curious programmer with a background in science. I enjoy open-source software, programming languages, web development and server administration, along with music, travel and snark.

Read More
Using Ansible to set up Zsh
Share this