feat(cinny): add ansible role

This commit is contained in:
2024-09-19 18:04:30 +02:00
parent 6790f4d2d3
commit dedbb72b70
21 changed files with 439 additions and 0 deletions

View File

@ -0,0 +1,37 @@
---
- name: Ensure cinny user '{{ cinny_user }}' is {{ cinny_state }}
ansible.builtin.user:
name: "{{ cinny_user }}"
system: "{{ cinny_user_system | default(true, true) }}"
create_home: "{{ cinny_user_create_home | default(false, true) }}"
state: "{{ cinny_state }}"
groups: "{{ cinny_user_groups | default(omit) }}"
append: "{{ cinny_user_groups_append | default(omit) }}"
register: cinny_user_info
- name: Ensure host path are {{ cinny_state }}
ansible.builtin.file:
name: "{{ path.name }}"
state: "{{ (cinny_state == 'present') | ternary('directory', 'absent') }}"
owner: "{{ path.owner | default(cinny_host_uid) }}"
group: "{{ path.group | default(cinny_host_gid) }}"
mode: "{{ path.mode | default('0750') }}"
loop_control:
loop_var: path
label: "{{ path.name }}"
loop:
- name: "{{ cinny_base_path }}"
mode: '0755'
- name: "{{ cinny_config_path }}"
mode: '0755'
- name: "{{ cinny_source_path }}"
mode: '0755'
- name: Ensure config file is {{ cinny_state }}
ansible.builtin.copy:
content: "{{ cinny_config_complete | to_nice_json }}"
dest: "{{ cinny_config_file }}"
owner: "{{ cinny_host_uid }}"
group: "{{ cinny_host_gid }}"
mode: "{{ cinny_config_file_mode | default('0664') }}"
when: cinny_state == 'present'

View File

@ -0,0 +1,3 @@
---
- fail:
msg: "Not yet implemented"

View File

@ -0,0 +1,3 @@
---
- fail:
msg: "Not yet implemented"

View File

@ -0,0 +1,33 @@
---
- name: Ensure docker client is logged {{ (cinny_state == 'present') | ternary('in', 'out') }}
community.docker.docker_login:
registry_url: "{{ cinny_container_image_registry }}"
username: "{{ cinny_container_image_registry_username }}"
password: "{{ cinny_container_image_registry_password }}"
reauthorize: "{{ cinny_container_image_registry_reauthorize | default(omit, true) }}"
state: "{{ cinny_state }}"
when:
- cinny_container_image_registry_username | default(false, true)
- cinny_container_image_registry_password | default(false, true)
- name: Ensure container image '{{ cinny_container_image }}' is {{ cinny_state }} locally
community.docker.docker_image:
name: "{{ cinny_container_image }}"
state: "{{ cinny_state }}"
source: "{{ cinny_container_source | default('pull') }}"
force_source: "{{ cinny_container_image_tag | default(false, true) }}"
- name: Ensure container '{{ cinny_container_name }}' is {{ cinny_state }}
community.docker.docker_container:
name: "{{ cinny_container_name }}"
image: "{{ cinny_container_image }}"
state: "{{ (cinny_state == 'present') | ternary('started', 'absent') }}"
env: "{{ cinny_container_env | default(omit) }}"
user: "{{ cinny_container_user }}"
ports: "{{ cinny_container_ports | default(omit) }}"
labels: "{{ cinny_container_labels | default(omit) }}"
volumes: "{{ cinny_container_full_volumes }}"
networks: "{{ cinny_container_networks | default(omit) }}"
etc_hosts: "{{ cinny_container_etc_hosts | default(omit) }}"
restart_policy: "{{ cinny_container_restart_policy }}"
purge_networks: "{{ cinny_container_purge_networks | default(omit) }}"

View File

@ -0,0 +1,44 @@
---
- name: Deploy nginx virtual host config file
ansible.builtin.template:
src: nginx.conf.j2
dest: "{{ cinny_nginx_available_sites }}/{{ cinny_nginx_vhost_name }}"
mode: "0640"
when: cinny_state == 'present'
- name: Enable nginx virtual host
ansible.builtin.file:
path: "{{ cinny_nginx_enabled_sites }}/{{ cinny_nginx_vhost_name }}"
src: "{{ cinny_nginx_available_sites }}/{{ cinny_nginx_vhost_name }}"
state: "{{ (cinny_state == 'present') | ternary('link', 'absent') }}"
when: cinny_nginx_vhost_enable
- name: Clean up nginx virtural host config file
ansible.builtin.file:
path: "{{ cinny_nginx_available_sites }}/{{ cinny_nginx_vhost_name }}"
state: absent
when: cinny_state == 'absent'
- name: Ensure nginx configuration is valid
ansible.builtin.command:
cmd: "nginx -t"
when:
- cinny_state == 'present'
- cinny_nginx_vhost_enable
- name: Reload nginx using systemd
ansible.builtin.systemd_service:
name: "nginx.service"
state: reloaded
when:
- cinny_state == 'present'
- cinny_nginx_vhost_enable
- ansible_facts['service_mgr'] == 'systemd'
- name: Inform user about required nginx reload
ansible.builtin.debug:
msg: "Restart nginx service (no systemd found)"
when:
- cinny_state == 'present'
- cinny_nginx_vhost_enable
- ansible_facts['service_mgr'] != 'systemd'

View File

@ -0,0 +1,22 @@
---
- name: Ensure container image '{{ cinny_container_image }}' is {{ cinny_state }} locally
containers.podman.podman_image:
name: "{{ cinny_container_image }}"
state: "{{ cinny_state }}"
pull: "{{ cinny_container_source == 'pull' }}"
force: "{{ cinny_container_image_tag | default(false, true) }}"
- name: Ensure container '{{ cinny_container_name }}' is {{ cinny_state }}
containers.podman.podman_container:
name: "{{ cinny_container_name }}"
image: "{{ cinny_container_image }}"
state: "{{ (cinny_state == 'present') | ternary('started', 'absent') }}"
env: "{{ cinny_container_env | default(omit) }}"
user: "{{ cinny_container_user }}"
ports: "{{ cinny_container_ports | default(omit) }}"
labels: "{{ cinny_container_labels | default(omit) }}"
volumes: "{{ cinny_container_full_volumes }}"
network: "{{ cinny_container_networks | default(omit) }}"
hostname: "{{ cinny_container_hostname | default(omit) }}"
etc_hosts: "{{ cinny_container_etc_hosts | default(omit) }}"
restart_policy: "{{ cinny_container_restart_policy }}"

View File

@ -0,0 +1,46 @@
---
- name: Check if running cinny version is saved on host
ansible.builtin.stat:
path: "{{ cinny_running_version_file }}"
register: cinny_running_version_st
- name: Retrieve running cinny version
ansible.builtin.slurp:
path: "{{ cinny_running_version_file }}"
register: cinny_running_version_info
when: cinny_running_version_st.stat.exists
- name: Extract running cinny version
set_fact:
cinny_is_update: >-
{{ not cinny_running_version_st.stat.exists or
(cinny_version is version(cinny_running_version, 'gt', version_type='semver')) }}
vars:
cinny_running_version: >-
{{ (cinny_running_version_info is defined)
| ternary(cinny_running_version_info['content'] | b64decode, false) }}
- name: Download tarball from GitHub release page
ansible.builtin.get_url:
url: "{{ cinny_tarball_url }}"
dest: "{{ cinny_tarball_path }}"
url_username: "{{ cinny_tarball_url_username | default(omit, true) }}"
url_password: "{{ cinny_tarball_url_password | default(omit, true) }}"
mode: "0664"
when: cinny_is_update
- name: Ensure old application files are gone
ansible.builtin.file:
path: "{{ cinny_dist_path }}"
state: absent
when: cinny_is_update
- name: Extract tarball to {{ cinny_source_path }}
ansible.builtin.unarchive:
src: "{{ cinny_tarball_path }}"
dest: "{{ cinny_source_path }}"
remote_src: true
owner: "{{ cinny_host_uid }}"
group: "{{ cinny_host_gid }}"
mode: "u+rwX,g+rwX,o+rX"
when: cinny_is_update

View File

@ -0,0 +1,23 @@
---
- name: Check if state is valid
ansible.builtin.fail:
msg: "Unknown state '{{ cinny_state }}'. Valid states are {{ cinny_states | join(', ') }}"
when: cinny_state not in cinny_states
- name: Check if deployment method is supported
ansible.builtin.fail:
msg: "Deployment method '{{ cinny_deployment_method }}' is not supported! (supported are: {{ cinny_deployment_methods | join(', ') }})"
when: cinny_deployment_method not in cinny_deployment_methods
- name: Include base configuration
ansible.builtin.include_tasks:
file: configure.yml
- name: Deploy tarball if required
ansible.builtin.include_tasks:
file: deploy-tarball.yml
when: cinny_deployment_method in cinny_needs_tarball
- name: Deploy using {{ cinny_deployment_method }}
ansible.builtin.include_tasks:
file: "deploy-{{ cinny_deployment_method }}.yml"