Can I use variable replacement with indispensable galaxy and .yml requirements?

We have private git repos for a number of Ansible roles. Repo hosts vary from site to site, for example:

  • Site1 uses https://gitforsite1.ourdomain.com

  • Site2 uses https://gitforsite2.ourdomain.com

I want to have a single file requirements.yml

and substitute the correct git repo. One way to do this is to set the bash script to an environment variable:

#!/bin/bash
...
if [ "$1" = "site1" ]; then
    export REPO_ROOT="https://gitforsite1.ourdomain.com"
fi
if [ "$1" = "site2" ]; then
    export REPO_ROOT="https://gitforsite2.ourdomain.com"
fi
... error checking if the value is not site1 or site2 ...
# Then install the roles
ansible-galaxy install -f -r config/requirements.yml -p roles

      

and then replace that value in requirements.yml

:

---

- src: {{ lookup('env', 'REPO_ROOT') }}/role1.git
  name: role1

- src: {{ lookup('env', 'REPO_ROOT') }}/role.git
  name: role2

      

This approach gives: a ERROR! Unable to load data from the requirements file

suggestion about the wrong file structure. (It is possible that the approach works, and my syntax is wrong.)

Any approach that allows me to set a variable (environment, command line, whatever) is fine. Otherwise, if this is not supported, is it necessary to overwrite the file requirements.yml

at runtime, perhaps using sed

?

EDIT 1: Added a line ansible-galaxy

in the bash script excerpt above to show how the file is being used requirements.yml

. I think this is the problem: ansible-galaxy

Doesn't expand variable substitutions included in group_vars/all

or environment. Using Ansible version 2.3.1.0 with Python 2.7.10.

EDIT 2: Discovered in the documentation is there server

to point to another instance of Galaxy, in ansible.cfg

for example like this:

[galaxy]  
server=https://gitforsite1.ourdomain.com

      

Galaxy does use this setting, but it must be a full Galaxy web app because it calls https://gitforsite1.ourdomain.com/api

. So that doesn't help either.

+3


source to share


2 answers


When they start with {

, you must specify values ​​in the mappings associated with the source. If not a parser yaml will try to parse this value as a stream style display instead of a scalar:

- src: "{{ lookup('env', 'REPO_ROOT') }}/role1.git"
  name: role1

      



Since you have single quotes in your scalar, not double quotes, nor any backslashes ( \

), I've used double quotes around the scalar. It's better to use single quotes if you don't have single quotes in your scalar, or if you have a backslash. If you have both types of quotes, use single quotes and double quotes inside . The following will load in the same way as above:

- src: '{{ lookup(''env'', ''REPO_ROOT'') }}/role1.git'
  name: role1

      

+1


source


What if you did it like this:

#!/bin/bash
...
if [ "$1" = "site1" ]; then
    export REPO_ROOT="https://gitforsite1.ourdomain.com"
fi
if [ "$1" = "site2" ]; then
    export REPO_ROOT="https://gitforsite2.ourdomain.com"
fi
... error checking if the value is not site1 or site2 ...
# Then install the roles
ansible-galaxy install -f -r config/requirements.yml -p roles -s ${REPO_ROOT}

      



and then replace this value in require.yml:

---

- src: role1.git
  name: role1

- src: role.git
  name: role2

      

0


source







All Articles