Managing Docker and Oh-My-Zsh with Ansible: A Comprehensive Guide.

Jagrit Thapar

1206 words • 7 min read

Introduction

In this blog post, we will explore how to manage Docker and Oh-My-Zsh using Ansible, a powerful automation tool. We will cover various examples that demonstrate how to install, update, and remove packages, as well as how to manage Docker containers and images.

Section 1: Managing Packages with Ansible on Debian-based Systems

Ansible provides a built-in module called apt that can be used to manage packages on Debian-based systems, such as Ubuntu and Debian. Here are some examples:

  1. Installing Apache HTTPD:
- name: Install apache httpd
  ansible.builtin.apt:
    name: apache2
    state: present

This example installs the Apache HTTPD package using the apt module. The state parameter is optional and can be used to specify whether the package should be installed or removed.

  1. Updating Repositories Cache and Installing a Package:
- name: Update repositories cache and install "foo" package
  ansible.builtin.apt:
    name: foo
    update_cache: yes

This example updates the repositories cache and installs the foo package using the apt module.

  1. Removing a Package:
- name: Remove "foo" package
  ansible.builtin.apt:
    name: foo
    state: absent

This example removes the foo package using the apt module.

  1. Installing a List of Packages:
- name: Install a list of packages
  ansible.builtin.apt:
    pkg:
    - foo
    - foo-tools

This example installs a list of packages using the apt module.

  1. Installing a Specific Version of a Package:
- name: Install the version '1.00' of package "foo"
  ansible.builtin.apt:
    name: foo=1.00

This example installs a specific version of the foo package using the apt module.

  1. Updating a Package to the Latest Version:
- name: Update the repository cache and update package "nginx" to latest version using default release squeeze-backport
  ansible.builtin.apt:
    name: nginx
    state: latest
    default_release: squeeze-backports
    update_cache: yes

This example updates the repository cache and updates the nginx package to the latest version using the apt module.

  1. Installing a Specific Version of a Package and Allowing Downgrades:
- name: Install the version '1.18.0' of package "nginx" and allow potential downgrades
  ansible.builtin.apt:
    name: nginx=1.18.0
    state: present
    allow_downgrade: yes

This example installs a specific version of the nginx package and allows downgrades using the apt module.

  1. Installing a Package and Ensuring Conflicted Packages are Not Removed:
- name: Install zfsutils-linux with ensuring conflicted packages (e.g. zfs-fuse) will not be removed.
  ansible.builtin.apt:
    name: zfsutils-linux
    state: latest
    fail_on_autoremove: yes

This example installs the zfsutils-linux package and ensures that conflicted packages are not removed using the apt module.

  1. Installing a Package Ignoring install-recommends:
- name: Install latest version of "openjdk-6-jdk" ignoring "install-recommends"
  ansible.builtin.apt:
    name: openjdk-6-jdk
    state: latest
    install_recommends: no

This example installs the latest version of the openjdk-6-jdk package and ignores install-recommends using the apt module.

  1. Upgrading All Packages to Their Latest Version:
- name: Update all packages to their latest version
  ansible.builtin.apt:
    name: "*"
    state: latest

This example updates all packages to their latest version using the apt module.

  1. Upgrading the OS (apt-get dist-upgrade):
- name: Upgrade the OS (apt-get dist-upgrade)
  ansible.builtin.apt:
    upgrade: dist

This example upgrades the OS using the apt module.

  1. Running apt-get update as a Separate Step:
- name: Run the equivalent of "apt-get update" as a separate step
  ansible.builtin.apt:
    update_cache: yes

This example updates the repositories cache using the apt module.

  1. Running apt-get update Only if the Last One is More Than 3600 Seconds Ago:
- name: Only run "update_cache=yes" if the last one is more than 3600 seconds ago
  ansible.builtin.apt:
    update_cache: yes
    cache_valid_time: 3600

This example updates the repositories cache only if the last one is more than 3600 seconds ago using the apt module.

  1. Passing Options to dpkg on Run:
- name: Pass options to dpkg on run
  ansible.builtin.apt:
    upgrade: dist
    update_cache: yes
    dpkg_options: 'force-confold,force-confdef'

This example passes options to dpkg on run using the apt module.

  1. Installing a .deb Package:
- name: Install a .deb package
  ansible.builtin.apt:
    deb: /tmp/mypackage.deb

This example installs a .deb package using the apt module.

  1. Installing the Build Dependencies for a Package:
- name: Install the build dependencies for package "foo"
  ansible.builtin.apt:
    pkg: foo
    state: build-dep

This example installs the build dependencies for the foo package using the apt module.

  1. Installing a .deb Package from the Internet:
- name: Install a .deb package from the internet
  ansible.builtin.apt:
    deb: https://example.com/python-ppq_0.1-1_all.deb

This example installs a .deb package from the internet using the apt module.

  1. Removing Useless Packages from the Cache:
- name: Remove useless packages from the cache
  ansible.builtin.apt:
    autoclean: yes

This example removes useless packages from the cache using the apt module.

  1. Removing Dependencies that are No Longer Required:
- name: Remove dependencies that are no longer required
  ansible.builtin.apt:
    autoremove: yes

This example removes dependencies that are no longer required using the apt module.

  1. Removing Dependencies that are No Longer Required and Purging Their Configuration Files:
- name: Remove dependencies that are no longer required and purge their configuration files
  ansible.builtin.apt:
    autoremove: yes
    purge: true

This example removes dependencies that are no longer required and purges their configuration files using the apt module.

Section 2: Managing Docker Containers and Images with Ansible

Ansible provides a built-in module called docker that can be used to manage Docker containers and images. Here are some examples:

  1. Starting a Docker Container:
- name: Start a Docker container
  community.docker.docker_container:
    name: my-container
    image: my-image
    state: started

This example starts a Docker container using the docker_container module.

  1. Stopping a Docker Container:
- name: Stop a Docker container
  community.docker.docker_container:
    name: my-container
    state: stopped

This example stops a Docker container using the docker_container module.

  1. Removing a Docker Container:
- name: Remove a Docker container
  community.docker.docker_container:
    name: my-container
    state: absent

This example removes a Docker container using the docker_container module.

  1. Creating a Docker Image:
- name: Create a Docker image
  community.docker.docker_image:
    name: my-image
    build: /path/to/Dockerfile
    state: present

This example creates a Docker image using the docker_image module.

  1. Removing a Docker Image:
- name: Remove a Docker image
  community.docker.docker_image:
    name: my-image
    state: absent

This example removes a Docker image using the docker_image module.

Section 3: Managing Oh-My-Zsh with Ansible

Ansible can be used to manage Oh-My-Zsh configurations and plugins. Here are some examples:

  1. Installing Oh-My-Zsh:
- name: Install Oh-My-Zsh
  ansible.builtin.git:
    repo: https://github.com/ohmyzsh/ohmyzsh.git
    dest: /home/{{ ansible_user }}/.oh-my-zsh
    version: 'master'
    clone: yes
    update: yes
  1. Installing Oh-My-Zsh Plugins:
- name: Install Oh-My-Zsh plugins
  ansible.builtin.copy:
    src: /path/to/plugins/
    dest: /home/{{ ansible_user }}/.oh-my-zsh/custom/plugins/
    owner: {{ ansible_user }}
    group: {{ ansible_user }}
    mode: '0755'

This example installs Oh-My-Zsh plugins using the copy module.

  1. Configuring Oh-My-Zsh:
- name: Configure Oh-My-Zsh
  ansible.builtin.blockinfile:
    path: /home/{{ ansible_user }}/.zshrc
    block: |
      ZSH_THEME="robbyrussell"
      plugins=(git docker)

This example configures Oh-My-Zsh using the blockinfile module.

Conclusion

In this blog post, we have explored how to manage Docker and Oh-My-Zsh using Ansible. We have covered various examples that demonstrate how to install, update, and remove packages, as well as how to manage Docker containers and images. With Ansible, you can automate complex tasks and manage your infrastructure with ease.