---

- name: Create directories recursively
  ansible.builtin.file:
    path: /tmp/path/to/final/dir
    state: directory

- name: Import tasks
  block:
    - name: By using absolute paths and special variables (preferred)
      ansible.builtin.import_tasks:
        file: "{{ role_path }}/tasks/install/{{ install_method }}.yml"
    - name: By using paths relative to the including file
      ansible.builtin.import_tasks:
        file: pre-flight.yml

- name: Conditionally include tasks
  block:
    - name: by leveraging the 'with_fileglob' loop filter (preferred)
      ansible.builtin.include_tasks:
        file: "{{ item }}"
      with_fileglob: "{{ install_method }}.yml"
    - name: by checking the files' existence
      vars:
        filename: "{{ install_method }}.yml"
      when: lookup('ansible.builtin.fileglob', filename) != []
      ansible.builtin.import_tasks:
        file: "{{ filename }}"

- name: Assertions
  ansible.builtin.assert:
    that:
      - install_method in supported_install_methods
      - external_url is ansible.builtin.url
    fail_msg: What to say if any of the above conditions fail
    success_msg: What to say if all of the above conditions succeed

- name: Pretty print information
  ansible.builtin.debug:
    msg: >-
      {{
        dict([
          [ 'install_method', install_method ],
          [ 'install_method in supported_install_methods', install_method in supported_install_methods ],
        ])
      }}

- name: Generate passwords
  block:
    - name: Randomly
      ansible.builtin.debug:
        msg: "{{ lookup('ansible.builtin.password', '/dev/null') }}"
    - name: Specifying requirements
      ansible.builtin.debug:
        msg: "{{ lookup('ansible.builtin.password', '/dev/null length=32 chars=ascii_letters,digits,punctuation') }}"
    - name: Random but idempotent, so it will not change at every execution
      ansible.builtin.debug:
        msg: "{{ lookup('ansible.builtin.password', '/dev/null', seed=inventory_hostname) }}"

- name: Run containers
  community.docker.docker_container:
    name: gitlab
    image: gitlab/gitlab-ce:16.11.2-ce.0
    hostname: gitlab.lan
    published_ports:
      - "8022:22"
      - "8080:80"
      - "8443:443"
    env:
      GITLAB_OMNIBUS_CONFIG: >-
        external_url 'http://gitlab.lan';
    shm_size: 256m
    volumes:
      - ./config:/etc/gitlab:Z
      - ./logs:/var/log/gitlab:Z
      - ./data:/var/opt/gitlab:Z
    auto_remove: true

- name: Add elements to lists
  set_fact:
    programming_languages: "{{ programming_languages + ['Ruby'] }}"

- name: "Use the users' home directory for something"
  block:
    - name: Executing commands from specified users
      block:
        - name: "Get users' homedir back"
          become: true
          become_user: "{{ item }}"
          become_flags: "-iH"
          check_mode: false
          command: >-
            echo "{{ item }}: $HOME"
          changed_when: false
          with_items:
            - root
            - ec2-user
          register: users_homedir_retrieve
        - name: Compute and register the results
          ansible.builtin.set_fact:
            users_homedir: >-
              {{
                users_homedir_retrieve
                | community.general.json_query('results[].stdout')
                | map('from_yaml')
                | combine
              }}
        - name: Do your thing!
          become: true
          become_user: "{{ item.key }}"
          ansible.builtin.file:
            path: "{{ item.value }}/placeholder"
            state: touch
          with_dict: "{{ users_homedir }}"
    - name: "From the system's entries"
      block:
        - name: "Get raw information from the system's entries"
          ansible.builtin.getent:
            database: passwd
            key: "{{ item }}"
            split: ":"
          with_items:
            - root
            - ec2-user
          register: users_entries
        - name: Compute and register the results
          ansible.builtin.set_fact:
            users_info: >-
              {{
                users_entries
                | community.general.json_query('results[].ansible_facts.getent_passwd[]')
                | combine
              }}
        - name: Do your thing!
          ansible.builtin.file:
            path: "{{ item.value[4] }}/placeholder"
            owner: "{{ item.key }}"
            state: touch
          with_dict: "{{ users_info }}"

- name: Cronjobs
  block:
    - name: At specific times
      become: true
      ansible.builtin.cron:
        name: Prometheus manual data backup
        cron_file: prometheus-manual-data-backup

        # Mind this is based on the hosts' time.
        hour: 4
        minute: 0

        user: root
        job:
          # - Keep '%' characters escaped or they'll be treated as newlines.
          # - Archive creation returns 1 if it detects changes to read files.
          #   Using ';' instead of '&&' to ignore.
          >
            FILENAME="/tmp/prometheus-data-$(date +'\%s-\%F-\%H-\%m-\%S').tar.gz"
            && tar -czf "$FILENAME" '/var/lib/prometheus/data'
            ; tar -tf "$FILENAME" > '/dev/null'
            && aws s3 cp "$FILENAME" 's3://backups/prometheus/'
            && rm "$FILENAME"
