# docker-containers role

This is a ansible-role for dealing with the containers from **c3infodesk**.

## Requirements
- To have [ansible](https://docs.ansible.com/ansible/latest/installation_guide/installation_distros.html) installed at your system.


## Configuration
You need to set some configuration for this to work
- ### ansible.cfg
  You have to edit the **ansible.cfg** file to your needs for accessing your target.
  - #### remote_user
    You need to set the **remote_user** configuration for accessing the target host.

    Example: [remote_user](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/ansible.cfg#L227)

- ### HOST_VARS
  In order to access your target host, you need to create/edit your **inventories/host_vars/<YOUR_HOST_NAME>.yml**
  - #### ansible_become_pass
    You need to set the **sudo** password from the user setted on **remote_user** for installing packages and etc. at the target.

    Example: [ansible_become_pass](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/inventories/host_vars/example_server.yml#L5)

- ### HOSTS
  You need to provide a group in which your target belongs to with the same name setted for **inventories/host_vars/<YOUR_HOST_NAME>.yml**.

  There you can add/edit the target address/IP.

  Example: [hosts](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/inventories/hosts)


## Role Variables
Here are the variables to setted from this role:

- ### From: inventories/host_vars/<TARGET_NAME>
  Here are the variables that you should be changing for your own deployment.

  - #### CUPS
    Here you can set the variables if you want to **disable** and/or **mask** the CUPS service at the target.
    - **c3infodesk_printer_host_cups_disable**: DEFAULT=<NOT_SETTED>
    - **c3infodesk_printer_host_cups_mask**: DEFAULT=<NOT_SETTED>
  - #### COMPOSE
    Here you can set the variables if you want to set the **docker compose command** to up/stop/restart/down
    - **c3infodesk_printer_docker_compose_up**: DEFAULT=<NOT_SETTED>
    - **c3infodesk_printer_docker_compose_stop**: DEFAULT=<NOT_SETTED>
    - **c3infodesk_printer_docker_compose_restart**: DEFAULT=<NOT_SETTED>
    - **c3infodesk_printer_docker_compose_down**: DEFAULT=<NOT_SETTED>

- ### From: inventories/group_vars/<TARGET_GROUP>
  Please don't cahnge the default values at this file. If you need to update them, use the **inventories/host_vars/<TARGET_NAME>** file.

  At the moment, there's **NO VARIABLES** setted at the **group_vars** for this role.

- ### From: roles/clone-repositories/defaults/main.yml
  Don't change the values at this file, if you need to change them, change it at the **inventories/host_vars/<TARGET_NAME>** file
  - #### CUPS
    - **c3infodesk_printer_host_cups_disable**: DEFAULT=false
      - If you want to disable the CUPS service at the target system
    - **c3infodesk_printer_host_cups_disable_debug**: DEFAULT=false
      - If you want to debug disabling CUPS at the target system
    - **c3infodesk_printer_host_cups_mask**: DEFAULT=false
      - If you want to mask the CUPS service at the target system
  - #### COMPOSE UP
    - **c3infodesk_printer_docker_compose_up**: DEFAULT=false
      - If you want to run the command: "docker compose up" at the target host
    - **c3infodesk_printer_docker_compose_up_debug**: DEFAULT=false
      - If you want to debug running the command: "docker compose up" at the target host
  - #### COMPOSE STOP
    - **c3infodesk_printer_docker_compose_stop**: DEFAULT=false
      - If you want to run the command: "docker compose stop" at the target host
    - **c3infodesk_printer_docker_compose_stop_debug**: DEFAULT=false
      - If you want to debug running the command: "docker compose stop" at the target host
  - #### COMPOSE RESTART
    - **c3infodesk_printer_docker_compose_restart**: DEFAULT=false
      - If you want to run the command: "docker compose restart" at the target host
    - **c3infodesk_printer_docker_compose_restart_debug**: DEFAULT=false
      - If you want to debug running the command: "docker compose restart" at the target host
  - #### COMPOSE DOWN
    - **c3infodesk_printer_docker_compose_down**: DEFAULT=false
      - If you want to run the command: "docker compose down" at the target host
    - **c3infodesk_printer_docker_compose_down_debug**: DEFAULT=false
      - If you want to debug running the command: "docker compose down" at the target host


## Dependencies
The role dependencies are:
  - [dependencies(role)](roles/dependencies/README.md)
  - [clone_repositories](roles/clone_repositories/README.md)
  - [docker_images](roles/docker_images/README.md)


## Example Playbook

- ### What this example will deploy
  This role will deploy all the containers setted on the [docker-compose.yml](https://git.cccv.de/fejao/c3infodesk-printer/-/blob/main/docker/docker-compose.yml) file.
  **PLEASE CHANGE THE VALEUS** setted at this file on the target to your own.

  The **docker-compose.yml** file can be founded at the location setted from the **c3infodesk_printer_folder** path variable. The default is: **"~/Coding/c3infodesk-printer"** folder

- ### Using the provided example
    For using the example playbook **[playbooks/examples/example_05_docker_containers.yml](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/playbooks/examples/example_05_docker_containers.yml)** file you only need to:
    - Change the user at the **[ansible.cfg](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/ansible.cfg)** file at the field **[remote_user](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/ansible.cfg#L227)** for acessing your target.
    - Update the file **[inventories/host_vars/example_server.yml](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/inventories/host_vars/example_server.yml)** and change the value for **[ansible_become_pass](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/inventories/host_vars/example_server.yml#L5)** from the **[remote_user](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/ansible.cfg#L227)**
    - Update the **[inventories/hosts](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/inventories/hosts)** file and change the **[example_server](https://git.cccv.de/fejao/c3infodesk-deployment/-/blob/main/inventories/hosts#L2)** with the IP address from your target.


  And run the command:
  ```bash
  ansible-playbook -i inventories/hosts playbooks/examples/05_docker_containers.yml
  ```

- ### Setting your own example

  - #### Deploying a new system example
    After setting your variables at **inventories/host_vars/<HOST_NAME>**, you can add this role to your playbook as a regular role.

    Here is an example using the **<HOST_NAME>** as **test_servers**

    You will need to set this variables fro a new deployment:
      - **c3infodesk_printer_host_cups_disable**: true
      - **c3infodesk_printer_host_cups_mask**: true
      - **c3infodesk_printer_docker_compose_up**: true

    ```yaml
    - hosts:
      - example_servers_group
      roles:
        - role: roles/docker_containers
          vars:
            c3infodesk_printer_host_cups_disable: true
            c3infodesk_printer_host_cups_mask: true
            c3infodesk_printer_docker_compose_up: true
    ```

    And call the playbook as:
    ```bash
    ansible-playbook -i <INVENTORY> <PLAYBOOK_NAME>.yml
    ```

  - #### Restarting the containers example

    Here is an example for adding this role for your playbook for **restarting** the **c3infodesk-printer** containers:

    We are using the **<HOST_NAME>** as **test_servers** for example.

    ```yaml
    - hosts:
        - test_servers
      roles:
        - role: roles/docker-containers
          vars:
            printer_docker_compose_restart: true
    ```

    And call the playbook as:

    ```bash
    ansible-playbook -i <INVENTORY> <PLAYBOOK_NAME>.yml
    ```

    Or you can also pass it using the **role variables**. So your playbook file should looks like:

    ```yaml
    - hosts:
        - test_servers
      roles:
        - roles/docker-containers
    ```

      And call it passing the variable, for example, restarting the containers from **c3infodesk-printer**.
    ```bash
    ansible-playbook -i <INVENTORY> <PLAYBOOK_NAME>.yml --extra-vars "printer_docker_compose_restart=true"
    ```


## Testing
You can test this locally using **DIND (docker in docker)** at your own computer using [ansible molecule](https://ansible.readthedocs.io/projects/molecule/installation/)

- ### Testing requirements
  You need docker installed at your system and the [ansible molecule](https://ansible.readthedocs.io/projects/molecule/installation/).

  You might have to set DIND at your system, for linux, you can create the file **/etc/docker/daemon.json**

  And add the content and restart docker.

  ```json
  {
    "exec-opts": ["native.cgroupdriver=systemd"],
    "features": {
      "buildkit": true
    },
    "experimental": true,
    "cgroup-parent": "docker.slice"
  }
  ```

- ### Using molecule
  Just go to the root of the role **roles/dependencies** and run the command:

  ```bash
  molecule test
  ```


## License

MIT / BSD


## Author Information
![fejao logo](../../files/from_authors/fejao_logo_circle.png "fejao") [https://chaos.social/@fejao](https://chaos.social/@fejao)