Skip to content
Snippets Groups Projects

Accessing the GitLab remote Terraform state from Ansible

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by nimrod
    playbook.yaml 1.08 KiB
    ---
    - 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: Debug
          debug:
            var: (tf_state.content|from_json)["outputs"]["env"]["value"]
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment