provision.ymlΒΆ

---
- name: "πŸ” List the workspaces the crossposter may publish into"
  uri_retry:
    url: "{{ OPENPOST_LOCAL_URL }}/api/v1/workspaces"
    headers: "{{ openpost_breakglass_headers }}"
    status_code: 200
  register: openpost_crossposter_workspaces
  changed_when: false
  no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"

- name: "🏒 Pick the organisation workspace for the crossposter"
  ansible.builtin.set_fact:
    openpost_crossposter_target_workspace: >-
      {{ (openpost_crossposter_workspaces.json
          | selectattr('name', 'equalto', OPENPOST_ORGANISATION_WORKSPACE) | first).id }}

- name: "πŸ”Ž Check the stored crossposter API token"
  uri_retry:
    url: "{{ OPENPOST_LOCAL_URL }}/api/v1/workspaces"
    headers: "{{ OPENPOST_API_HEADERS | combine({'Authorization': 'Bearer ' ~ openpost_crossposter_token}) }}"
    status_code: [200, 401, 403]
  register: openpost_crossposter_token_check
  changed_when: false
  when: openpost_crossposter_token | length > 0
  no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"

- name: "πŸ”‘ Issue a workspace API token for the crossposter"
  uri_retry:
    url: "{{ OPENPOST_LOCAL_URL }}/api/v1/api-tokens"
    method: POST
    headers: "{{ openpost_breakglass_headers }}"
    body_format: json
    body:
      name: "{{ OPENPOST_CROSSPOSTER_TOKEN_NAME }}"
      scope: "api:write"
      workspace_id: "{{ openpost_crossposter_target_workspace }}"
    status_code: [200, 201]
  register: openpost_crossposter_issued
  changed_when: true
  when: >-
    openpost_crossposter_token | length == 0
    or openpost_crossposter_token_check.status != 200
    or openpost_crossposter_workspace != openpost_crossposter_target_workspace
  no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"

- name: "🧭 Adopt the newly issued crossposter API token"
  ansible.builtin.set_fact:
    openpost_crossposter_workspace: "{{ openpost_crossposter_target_workspace }}"
    openpost_crossposter_token: "{{ openpost_crossposter_issued.json.token }}"
  when: openpost_crossposter_issued is changed
  no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"

- name: "🌱 Seed the CI source tokens and social sets"
  ansible.builtin.include_tasks: crossposter/seed.yml
  when: OPENPOST_SIMULATOR_ENABLED | bool

- name: "πŸ’Ύ Persist the crossposter workspace, API token and CI source tokens"
  ansible.builtin.copy:
    content: >-
      {{ {'workspace': openpost_crossposter_workspace, 'token': openpost_crossposter_token,
          'sources': openpost_crossposter_sources} | to_json }}
    dest: "{{ OPENPOST_CROSSPOSTER_STATE_FILE }}"
    mode: "0600"
  no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"