You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ansible-homelab/roles/setup_portainer/tasks/main.yml

73 lines
2.3 KiB
YAML

# @meta author: Cian Hatton
# @meta description: >
# Role to configure Portainer. This role copies the docker-compose.yml
# onto the host and starts portainer with docker compose.
# If there is a backup of the portainer volume stored in s3, it will
# be restored before portainer is started.
# @end
---
- name: Portainer | Create directory if it does not exist
ansible.builtin.file:
path: "{{directories.docker_compose_directory}}"
state: directory
mode: '0755'
- name: Portainer | Copy docker compose file
copy:
src: docker-compose.yml
dest: "{{directories.docker_compose_directory}}/docker-compose.yml"
- name: Portainer | Check if volume exists
shell: docker volume ls -f name=portainer_portainer_data --format '{{ '{{' }} .Name {{ '}}' }}'
register: portainer_volume
changed_when: False
- name: Portainer | Pull images
docker_image:
name: "{{item}}"
source: pull
with_items:
- ubuntu
- busybox
- name: Docker Volume Backup | Restore Portainer volume from S3
when: (portainer_volume.stdout_lines | length) == 0
docker_container:
command: "restore-volume --s3 --volume portainer_portainer_data"
image: "ghcr.io/chatton/docker-volume-backup:v0.3.0"
name: "s3-restore-portainer"
cleanup: true # delete container after it's done.
state: started # container should execute.
detach: no # task fails if container exits.
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /tmp:/tmp # temp s3 archive goes here
env:
AWS_ACCESS_KEY_ID: "{{aws_s3.aws_access_key}}"
AWS_SECRET_ACCESS_KEY: "{{aws_s3.aws_secret_key}}"
AWS_DEFAULT_REGION: "{{aws_s3.region}}"
AWS_BUCKET: "{{aws_s3.bucket}}"
AWS_ENDPOINT: "{{aws_s3.s3_url}}"
- name: Portainer | Docker compose up
community.docker.docker_compose:
project_src: "{{directories.docker_compose_directory}}/portainer"
# Don't really need this as long as there is an S3 backup.
#- name: Portainer | Register Admin User
# when: (portainer_volume.stdout_lines | length) == 0
# uri:
# url: http://localhost:9000/api/users/admin/init
# method: POST
# body:
# Username: admin
# Password: "{{portainer.password}}"
# status_code: 200
# body_format: json
# register: result
# until: result.status == 200
# retries: 60
# delay: 1