# Extra\_vars and env variables empty

**URL:** <https://forum.ansible.com/t/extra-vars-and-env-variables-empty/4308>\
**Category:** Get Help\
**Created:** [March 11, 2024, 9:34am UTC](https://forum.ansible.com/t/extra-vars-and-env-variables-empty/4308 "2024-03-11T09:34:01Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [March 11, 2024, 9:34am UTC](https://forum.ansible.com/t/extra-vars-and-env-variables-empty/4308/1 "2024-03-11T09:34:01Z")

</div>

Hello!  
I switched from my test GitHub and created successfully a project, but i couldnt get the right playbook to copy a file from the new GitLab to a local host.  
Only the auth via username and PAT is aviable.  
I added the user and PAR as a custom Auth and added the env GITLAB\_USER and GITLAB\_TOKEN.

```auto
---
- name: list
  hosts: all
  become: true

  vars:
    gitlab_user: "{{ lookup('env','GITLAB_USER') }}"
    gitlab_token: "{{ lookup('env','GITLAB_TOKEN') }}"
    git_repo: "https://git.gruen.net/awx_gruen/playbooks.git"

  tasks:
    - name: clone from repo to tmp
      ansible.builtin.git:
        repo: "https://{{ gitlab_user }}:{{ gitlab_token }}@{{ git_repo | regex_replace('https://', '') }}"
        dest: "/tmp/repo"
        version: "dev"
        force: yes
        accept_hostkey: yes

```

But i always get the error :

```auto
remote: HTTP Basic: Access denied. The provided password or token is incorrect or your account has 2FA enabled and you must use a personal access token instead of a password.

```

or:

```auto
git@git.gruen.net: Permission denied (publickey,password).\\r\\nfatal: Could not read from remote repository.\\n\\nPlease make sure you have the correct access rights\\nand the repository exists.\\n",

```

2FA is dsabled for this user and i created a user juist for this use.

Thank you again for you really helful input!

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [March 11, 2024, 1:11pm UTC](https://forum.ansible.com/t/extra-vars-and-env-variables-empty/4308/2 "2024-03-11T13:11:22Z")

</div>

it seems that somehow my env vars are empty:  
I created a custom credential type:

 ![grafik](https://us1.discourse-cdn.com/ansible/original/2X/2/23d9911101778a1e80d5f836dc090175b686eaac.png)  
 ![grafik](https://us1.discourse-cdn.com/ansible/original/2X/b/b0373755edd8166bc20bf1044302e4aff9c76a5b.png)  
And used this playbook:

```auto
---
- name: Ausgabe von Beispiel-Umgebungsvariablen
  hosts: localhost
  tasks:
    - name: Ausgabe von EXAMPLE_VAR1
      ansible.builtin.debug:
        msg: "Der Wert von EXAMPLE_VAR1 ist: {{ lookup('env', 'EXAMPLE_VAR1') }}"

    - name: Ausgabe von EXAMPLE_VAR2
      ansible.builtin.debug:
        msg: "Der Wert von EXAMPLE_VAR2 ist: {{ lookup('env', 'EXAMPLE_VAR2') }}"

```

and the ouput seems to be with empty content:

 ![grafik](https://us1.discourse-cdn.com/ansible/original/2X/4/4bb19f2a6c3a2accb5d9e5967e5145bc735add7b.png)  
I have the version 23.9.0 and tried with 23.6.0

---

<div class="post-metadata">

**Author:** ![Dustin](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dustin/32/2573_2.png) [@Dustin](https://forum.ansible.com/u/Dustin)\
**Post date:** [March 11, 2024, 3:57pm UTC](https://forum.ansible.com/t/extra-vars-and-env-variables-empty/4308/3 "2024-03-11T15:57:32Z")

</div>

The easiest way to deal with remote repo authentication with GitLab is to use a [Personal Access Token - PAT](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html).

If you really want to force it to work with username and password, update your credential type to sent your variables as `extra_vars` instead of `env`. Then, you can reference those credentials in the playbook as their variable names.

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [March 12, 2024, 7:19am UTC](https://forum.ansible.com/t/extra-vars-and-env-variables-empty/4308/4 "2024-03-12T07:19:23Z")

</div>

I aleready tried to use the PAT, but i think my problem is that somehow the variables i need for the authentication are empty somehow:  
How do i use the extra vars - do i have to set something in the project at the field extra variables?  
When i change the Variable to:

```auto
{{ EXAMPLE_VAR2 }}"

```

it states the variable is not initialized and if i keep the playbook the variables are still empty or rather undefined.

 ![grafik](https://us1.discourse-cdn.com/ansible/original/2X/6/66573cb0b7c2a24f8a73c6fd950342c4bcca073f.png)  
I followed this guide to use the extra\_vars: [https://www.youtube.com/watch?v=wTJxByGZXsc](https://www.youtube.com/watch?v=wTJxByGZXsc)  
Thank you!

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [March 12, 2024, 8:57am UTC](https://forum.ansible.com/t/extra-vars-and-env-variables-empty/4308/6 "2024-03-12T08:57:48Z")

</div>

I changed the title becaue its more fitting

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [March 12, 2024, 9:32am UTC](https://forum.ansible.com/t/extra-vars-and-env-variables-empty/4308/7 "2024-03-12T09:32:25Z")

</div>

First to make clear that i am right how to use the env variables:  
You make a custom credential and assign the fields to the required env variables in the input field.

then add a new credential with the new created credential type.

and lastly i can use these in my playbooks to get the variable contents:

```auto
{{ lookup('ansible.builtin.env', 'HOME') }}

```

Can i use multiple credentials for one custom credential type and how could i choose the right one?

---

<div class="post-metadata">

**Author:** ![Dustin](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dustin/32/2573_2.png) [@Dustin](https://forum.ansible.com/u/Dustin)\
**Post date:** [March 12, 2024, 2:21pm UTC](https://forum.ansible.com/t/extra-vars-and-env-variables-empty/4308/8 "2024-03-12T14:21:42Z")

</div>

If you really want or need to use environment variables for your playbooks, ansible has a way to se the environment variables for each task.

[Ansible Environment Variables](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_environment.html)

Update your credentials to set extra\_vars then set your environment variables using your credential variables.

Credential Type Input Configuration:

```yaml
fields:
  - id: example_var1
    type: string
    label: Example Variable 1
  - id: example_var1
    type: string
    label: Example Variable 2
    secret: true
required:
  - EXAMPLE_VAR1
  - EXAMPLE_VAR2

```

Credential Type Injector Configuration:

```yaml
extra_vars:
  example_var1: '{{ example_var1}}'
  example_var1: '{{ example_var1}}'

```

Task:

```yaml
- name: Show Off Them Env Vars
  debug:
    msg: "{{ item }} :: {{ lookup('ansible.builtin.env', (item | upper ) ) }}"
  with_items:
    - example_var1
    - example_var2
  environment:
    EXAMPLE_VAR1: "{{ example_var1 }}"
    EXAMPLE_VAR2: "{{ example_var2 }}"

```

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [March 12, 2024, 3:10pm UTC](https://forum.ansible.com/t/extra-vars-and-env-variables-empty/4308/9 "2024-03-12T15:10:44Z")

</div>

okay in the video it seems i only needed to place the

```auto
{{ example_var1 }}

```

just like any other variable where i nedd ith in the playbook- ill try it tomorrow.  
and how do i use the env?  
then it doesnt seem to just could not use :  
`{{ lookup('ansible.builtin.env', 'HOME') }}`  
but it seemed like this in the official guide.  
Thank you!

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [March 13, 2024, 7:58am UTC](https://forum.ansible.com/t/extra-vars-and-env-variables-empty/4308/10 "2024-03-13T07:58:07Z")

</div>

I have editet your playbook to my testvariables i creatzed to test the outpu of the credential type with extra\_vars type:

```auto
---
- name: Show Off Them Env Vars
  hosts: all
  debug:
    msg: "{{ item }} :: {{ lookup('ansible.builtin.env', (item | upper ) ) }}"
  with_items:
    - example_var3
  environment:
    EXAMPLE_VAR3: "{{ test_var_3 }}"

```

but it fails:

 ![grafik](https://us1.discourse-cdn.com/ansible/original/2X/6/6df5a8b2410db0f312ed173e7eaa5d43e6c2b6db.png)

The user and Credential type:

 ![grafik](https://us1.discourse-cdn.com/ansible/original/2X/c/c123ede578a1dcd1b564779a1ffa2e120de4c188.png)

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [March 13, 2024, 8:45am UTC](https://forum.ansible.com/t/extra-vars-and-env-variables-empty/4308/11 "2024-03-13T08:45:05Z")

</div>

I now have the solution:  
I thought i just need to add the credential for the custom credential and its like a global strorage,  
but i have to add the new credentials to the templates and now i can output he content of them.

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [March 14, 2024, 6:44am UTC](https://forum.ansible.com/t/extra-vars-and-env-variables-empty/4308/12 "2024-03-14T06:44:58Z")

</div>

Hi,

This is a formatting error; your play lacks the `tasks` key. Add it, then indent your debug task under this section.

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [March 14, 2024, 8:44am UTC](https://forum.ansible.com/t/extra-vars-and-env-variables-empty/4308/13 "2024-03-14T08:44:38Z")

</div>

Thank you!  
my functional test playbook at the end was this:

```auto
---
- name: testvaroutput
  hosts: all
  tasks:
    - name: testvaroutput
      ansible.builtin.debug:
        msg: "value: {{ test_var_3 }}"

    - name: testvaroutput
      ansible.builtin.debug:
        msg: "value: {{ GITLAB_PUBKEY }}"

    - name: testvaroutput
      ansible.builtin.debug:
        msg: "value: {{ GITLAB_USER }}"

    - name: testvaroutput
      ansible.builtin.debug:
        msg: "value: {{ lookup('ansible.builtin.env', 'EXAMPLE_VAR1') }}"

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 13, 2024, 8:45am UTC](https://forum.ansible.com/t/extra-vars-and-env-variables-empty/4308/14 "2024-04-13T08:45:39Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
