Extra_vars and env variables empty

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.

---
- 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 :

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:

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!

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



And used this playbook:

---
- 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:


I have the version 23.9.0 and tried with 23.6.0

The easiest way to deal with remote repo authentication with GitLab is to use a Personal Access Token - PAT.

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.

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:

{{ EXAMPLE_VAR2 }}"

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


I followed this guide to use the extra_vars: https://www.youtube.com/watch?v=wTJxByGZXsc
Thank you!

I changed the title becaue its more fitting

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:

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

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

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

Update your credentials to set extra_vars then set your environment variables using your credential variables.

Credential Type Input Configuration:

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:

extra_vars:
  example_var1: '{{ example_var1}}'
  example_var1: '{{ example_var1}}'

Task:

- 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 }}"

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

{{ 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!

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

---
- 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:

The user and Credential type:

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.

Hi,

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

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

---
- 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') }}"

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