Commit a588aff7 authored by nimrod's avatar nimrod
Browse files

Entry on getting an output from the GitLab Terrafrom remote state with

Ansible.
parent f93a1d8b
Loading
Loading
Loading
Loading
Loading
+48 −0
Original line number Diff line number Diff line
Accessing the GitLab remote Terraform state from Ansible
========================================================

:date: 2021-05-08
:summary: Accessing the GitLab remote Terraform state from Ansible.

Note to self: How to get the outputs from a GitLab remote Terrafrom state from
Ansible.

.. code:: yaml

    ---
    - name: Get Terraform outputs from the GitLab remote state
      hosts:
        - localhost
      connection: local
      become: false
      gather_facts: false
      vars:
        project_name: amilive
        tf_workspace: "{{ lookup('env', 'TF_WORKSPACE')|default('default', true) }}"
        gitlab_token: "{{ lookup('env', 'GITLAB_TOKEN') }}"
        gitlab_base_url: "{{ lookup('env', 'GITLAB_BASE_URL') }}"

      tasks:
        - name: Projects
          ansible.builtin.uri:
            headers:
              PRIVATE-TOKEN: "{{ gitlab_token }}"
            method: GET
            url: "{{ gitlab_base_url }}/projects"
          register: projects

        - name: Request
          ansible.builtin.uri:
            headers:
              PRIVATE-TOKEN: "{{ gitlab_token }}"
            method: GET
            return_content: true
            status_code: [200]
            url: "{{ gitlab_base_url }}/projects/{{ project_id }}/terraform/state/{{ tf_workspace }}"
          vars:
            project_id: "{{ (projects.json|selectattr('path', 'equalto', project_name))[0].id }}"
          register: tf_state

        - name: Env output
          debug:
            var: (tf_state.content|from_json)["outputs"]["env"]["value"]