'How to pass token to requirements.yaml used by ansible galaxy for installing pre-requisites

I am trying to use an environment variable inside of requirements.yml but it's not working

    - src: 'http://oauth2:{{ lookup("env", "GITLAB_TOKEN") }}@gitlab.repo/data-migration.git'
  scm: git
  version: "master"

The token is a sensitive data , I can't put it in requirenments.yaml, any idea please ?

ansible-galaxy install -r requirements.yaml --force

ansible --version = 2.4.6.0



Solution 1:[1]

To debug:

run a command -->

export TOKEN=test

playbook -->

---
- name: test
  hosts: localhost
  tasks:
    - name: play
      debug:
        msg: "{{ lookup('env', 'TOKEN') }}"

output -->

ok: [localhost] => {
    "msg": "test"
}

The lookup should work for replacements.

- src: "http://oauth2:{{ lookup('env', 'TOKEN') }}@gitlab.my-repo.git"
  name: role1
  version: master

Solution 2:[2]

The --token parameter can be used in the ansible-galaxy command for installing roles from private repos.

So your requirements.yml could look like: (note that I took the liberty of making this https)

roles:
  - src: 'https://gitlab.repo/data-migration.git'
    scm: git
    version: "master"

and your installation command would become:

ansible-galaxy role install --token $GITLAB_TOKEN -r requirements.yml --force

Now if you want to do this from github actions (my current problem)... brace yourself

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bbaassssiiee
Solution 2 β.εηοιτ.βε