diff --git a/content/gitlab-tf-state-ansible.rst b/content/gitlab-tf-state-ansible.rst new file mode 100644 index 0000000000000000000000000000000000000000..0b2dd97997dd11db33b60208299e36c5fdb3099c --- /dev/null +++ b/content/gitlab-tf-state-ansible.rst @@ -0,0 +1,48 @@ +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"]