Skip to content

Instantly share code, notes, and snippets.

@gridhead
Last active May 16, 2021 15:32
Show Gist options
  • Select an option

  • Save gridhead/e7828803c456684ada475d971c5a544b to your computer and use it in GitHub Desktop.

Select an option

Save gridhead/e7828803c456684ada475d971c5a544b to your computer and use it in GitHub Desktop.
Pull Docker Images on an Ubuntu Docker Host Constructed From a Jinja2 Template
[web]
server1 ansible_host=192.168.0.165
server1 ansible_ssh_user=ubuntu
server1 ansible_ssh_pass=<your-ssh-password>
server1 ansible_sudo_pass=<your-sudo-password>

Pull Docker Images on an Ubuntu Docker Host Constructed From a Jinja2 Template

  1. Download the inventory.ini, pull-docker-image-template.yml and pulltemp.j2 to the same directory.
  2. Make changes to the inventory.ini file to change the hostname, username, passwords (for SSH and Sudo).
  3. Make changes to the pull-docker-image-template.yml file to change the following variables.
    1. hostaddr (to reflect the remote machine name from the inventory.ini file)
    2. username (to reflect the username for the remote machine from the inventory.ini file)
    3. imejlist (to reflect the list of images with tags that need to be downloaded)
  4. Once the changes are completed, execute the following command to populate a playbook with the images listed.
    ansible-playbook pull-docker-image-template.yml -vvv
    
  5. Fetch the community.docker collection from Ansible Galaxy by executing the following command.
    ansible-galaxy collection install community.docker
    
  6. Now run the generated playbook on the remote machine by executing the following command.
    ansible-playbook -i inventory.ini pulling_images_for_ubuntu_rpi4_homeserver.yml -vvv
    

The generated playbook is attached here for convenience.

- name: "Establish template for pulling said list of Docker images from Dockerhub"
hosts: "127.0.0.1"
vars:
hostaddr: "server1"
username: "ubuntu"
imejlist: [
"linuxserver/plex:latest",
"linuxserver/code-server:latest",
"zadam/trilium:latest",
"hlince/trilium:latest-stable-arm",
"fedora:latest",
"gitea/gitea:latest",
"linuxserver/heimdall:latest",
"p3terx/ariang:latest",
"t0xic0der/supervisor-frontend-service:v1.2.0b-arm64",
"t0xic0der/supervisor-driver-service:v1.2.1b-arm64",
"linuxserver/rutorrent:latest",
"ubuntu:latest",
"t0xic0der/cdrsrv:v3.9.2-centos8",
"p3terx/aria2-pro:latest",
"portainer/portainer-ce:latest",
"centos:latest"
]
tasks:
- name: "Setting the variables in the example template"
template:
src: "pulltemp.j2"
dest: "pulling_images_for_ubuntu_rpi4_homeserver.yml"
- name: "Pull listed Docker images from DockerHub"
hosts: "server1"
remote_user: "ubuntu"
become: yes
become_method: "sudo"
become_user: "root"
tasks:
- name: "STEP 0 - Install python3-docker on the remote machine first"
apt:
update_cache: "yes"
name: "python3-docker"
state: "latest"
- name: "STEP 1 - Pull linuxserver/plex:latest from DockerHub"
community.docker.docker_image:
name: "linuxserver/plex:latest"
source: "pull"
- name: "STEP 2 - Pull linuxserver/code-server:latest from DockerHub"
community.docker.docker_image:
name: "linuxserver/code-server:latest"
source: "pull"
- name: "STEP 3 - Pull zadam/trilium:latest from DockerHub"
community.docker.docker_image:
name: "zadam/trilium:latest"
source: "pull"
- name: "STEP 4 - Pull hlince/trilium:latest-stable-arm from DockerHub"
community.docker.docker_image:
name: "hlince/trilium:latest-stable-arm"
source: "pull"
- name: "STEP 5 - Pull fedora:latest from DockerHub"
community.docker.docker_image:
name: "fedora:latest"
source: "pull"
- name: "STEP 6 - Pull gitea/gitea:latest from DockerHub"
community.docker.docker_image:
name: "gitea/gitea:latest"
source: "pull"
- name: "STEP 7 - Pull linuxserver/heimdall:latest from DockerHub"
community.docker.docker_image:
name: "linuxserver/heimdall:latest"
source: "pull"
- name: "STEP 8 - Pull p3terx/ariang:latest from DockerHub"
community.docker.docker_image:
name: "p3terx/ariang:latest"
source: "pull"
- name: "STEP 9 - Pull t0xic0der/supervisor-frontend-service:v1.2.0b-arm64 from DockerHub"
community.docker.docker_image:
name: "t0xic0der/supervisor-frontend-service:v1.2.0b-arm64"
source: "pull"
- name: "STEP 10 - Pull t0xic0der/supervisor-driver-service:v1.2.1b-arm64 from DockerHub"
community.docker.docker_image:
name: "t0xic0der/supervisor-driver-service:v1.2.1b-arm64"
source: "pull"
- name: "STEP 11 - Pull linuxserver/rutorrent:latest from DockerHub"
community.docker.docker_image:
name: "linuxserver/rutorrent:latest"
source: "pull"
- name: "STEP 12 - Pull ubuntu:latest from DockerHub"
community.docker.docker_image:
name: "ubuntu:latest"
source: "pull"
- name: "STEP 13 - Pull t0xic0der/cdrsrv:v3.9.2-centos8 from DockerHub"
community.docker.docker_image:
name: "t0xic0der/cdrsrv:v3.9.2-centos8"
source: "pull"
- name: "STEP 14 - Pull p3terx/aria2-pro:latest from DockerHub"
community.docker.docker_image:
name: "p3terx/aria2-pro:latest"
source: "pull"
- name: "STEP 15 - Pull portainer/portainer-ce:latest from DockerHub"
community.docker.docker_image:
name: "portainer/portainer-ce:latest"
source: "pull"
- name: "STEP 16 - Pull centos:latest from DockerHub"
community.docker.docker_image:
name: "centos:latest"
source: "pull"
- name: "Pull listed Docker images from DockerHub"
hosts: "{{ hostaddr }}"
remote_user: "{{ username }}"
become: yes
become_method: "sudo"
become_user: "root"
tasks:
- name: "STEP 0 - Install python3-docker on the remote machine first"
apt:
update_cache: "yes"
name: "python3-docker"
state: "latest"
{% for imejname in imejlist %}
- name: "STEP {{ loop.index }} - Pull {{ imejname }} from DockerHub"
community.docker.docker_image:
name: "{{ imejname }}"
source: "pull"
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment