Skip to content

Instantly share code, notes, and snippets.

@jd-apprentice
Last active October 15, 2023 02:14
Show Gist options
  • Save jd-apprentice/74b768294635862e7b238e7072d6011e to your computer and use it in GitHub Desktop.
Save jd-apprentice/74b768294635862e7b238e7072d6011e to your computer and use it in GitHub Desktop.
Ansible reusable tasks in playbooks

To run the playbook we should call the docker.yml with a valid inventory for example ansible-playbook ansible/suite/docker.yml -i ansible/inventory/debian.ini

This command will execute the desired playbook which is going to execute the tasks from debian-update install docker-group

🌳 ansible-playbooks/
┣ πŸ“ ansible/
┃ ┣ πŸ“ inventory/
┃ ┃ β”— πŸ“„ debian.ini
┃ ┣ πŸ“ playbooks/
┃ ┃ β”— πŸ“„ update-and-install.yml
┃ ┣ πŸ“ roles/
┃ ┃ ┣ πŸ“ docker/
┃ ┃ ┃ β”— πŸ“ tasks/
┃ ┃ ┃   ┣ πŸ“„ docker-group.yml
┃ ┃ ┃   β”— πŸ“„ install.yml
┃ ┃ β”— πŸ“ system/
┃ ┃   β”— πŸ“ tasks/
┃ ┃ ┃   β”—πŸ“„ debian-update.yml
┃ β”— πŸ“ suite/
┃   β”— πŸ“„ docker.yml
## Tested with ubuntu focal 20.04: Works
- name: Run apt-get update
become: yes
apt: update_cache=yes
- name: Run autoclean
become: yes
apt: autoclean=yes
- name: Run autoremove
become: yes
apt: autoremove=yes
## Tested with ubuntu focal 20.04: Works
- name: add docker group
group:
name: docker
state: present
- name: add ubuntu to docker group
become: yes
user:
name: "{{ ansible_user }}"
groups: docker
append: yes
## https://chat.openai.com/share/112c9e59-7206-4be6-ac29-3418c4096ea6
## Tested with ubuntu focal 20.04: Works
---
- name: Update system and install docker
hosts: debian
- name: Include update and install playbook
ansible.builtin.import_playbook: ../playbooks/update-and-install.yml
## Tested with ubuntu focal 20.04: Works
- name: Add Docker's official GPG key
shell: |
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
- name: Add the repository to Apt sources
shell: |
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/{{ ansible_distribution | lower }} \
"$(. /etc/os-release && echo "{{ ansible_lsb.codename }}")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
- name: install packages
become: yes
apt:
pkg:
- docker-ce
- docker-ce-cli
- docker-buildx-plugin
- docker-compose-plugin
state: latest
update_cache: yes
- name: service docker
ansible.builtin.service:
name: docker
state: started
## https://docs.ansible.com/ansible/latest/collections/ansible/builtin/include_tasks_module.html#examples
---
- name: Update the system and install Docker, Docker Compose, and add the current user to the Docker group
hosts: debian
tasks:
- name: Include prepare playbook
ansible.builtin.include_tasks:
file: ../roles/system/tasks/debian-update.yml
- name: Include install playbook
ansible.builtin.include_tasks:
file: ../roles/docker/tasks/install.yml
- name: Include add user to docker group playbook
ansible.builtin.include_tasks:
file: ../roles/docker/tasks/docker-group.yml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment