Skip to content
This repository has been archived by the owner on Mar 23, 2019. It is now read-only.

jinja env lookup within container.yml does not find environment variables #703

Open
shalomb opened this issue Aug 15, 2017 · 7 comments
Open

Comments

@shalomb
Copy link

shalomb commented Aug 15, 2017

ISSUE TYPE
  • Bug Report
container.yml
---

version: "2"

settings:
  conductor:
    base: debian:latest

services:
  web:
    from: busybox:latest
    ports:
      - "8080:80"
    environment:
      - 'VARFOO={{ lookup("env", "VARFOO") }}'

    command: ['/bin/sh', '-lc', 'while :; do sleep 10; { date; env; } >> /tmp/container.log; done']

registries: {}

OS / ENVIRONMENT
Ansible Container, version 0.9.2rc0
Linux, iris, 4.9.0-3-amd64, #1 SMP Debian 4.9.30-2+deb9u3 (2017-08-06), x86_64
2.7.13 (default, Jan 19 2017, 14:48:08)
[GCC 6.3.0 20170118] /usr/bin/python
{                                                     
  "ContainersPaused": 0,    
  "Labels": null,
  "CgroupDriver": "cgroupfs",
  "ContainersRunning": 1,     
  "ContainerdCommit": {                   
    "Expected": "9048e5e50717ea4497b757314bad98ea3763c145",
    "ID": "9048e5e50717ea4497b757314bad98ea3763c145"
  },
  "InitBinary": "docker-init",
  "NGoroutines": 29,
  "Swarm": {
    "ControlAvailable": false,
    "NodeID": "",
    "Error": "",
    "RemoteManagers": null,
    "LocalNodeState": "inactive",
    "NodeAddr": ""
  },
  "LoggingDriver": "json-file",
  "OSType": "linux",
  "HttpProxy": "",
  "Runtimes": {
    "runc": {
      "path": "docker-runc"
    }
  },
  "DriverStatus": [
    [
      "Pool Name",
      "docker-254:3-33606240-pool"
SUMMARY

Our on-premise GitLab CI jobs set environment variables that I would like to use within container.yml and https://docs.ansible.com/ansible-container/container_yml/template.html#passing-environment-variables notes that

In the case of the build command, for example, template rendering happens before any containers are started, and it occurs outside of the Ansible Build Container.
STEPS TO REPRODUCE
export VARFOO='42'
ansible-container --devel build
ansible-container --devel run --production
EXPECTED RESULTS

I would have expected VARFOO to be looked up by lookup("env", "VARFOO") and for the environment variable to be set in the container like so.

docker exec -it simplebusybox_web_1 grep VARFOO /tmp/container.log | tail -n 1
VARFOO=42
ACTUAL RESULTS

VARFOO happens to be empty in this case

docker exec -it simplebusybox_web_1 grep VARFOO /tmp/container.log | tail -n 1
VARFOO=
@shalomb shalomb changed the title lookup jinja env lookup within container.yml does not find environment variables Aug 15, 2017
@chouseknecht
Copy link
Contributor

@shalomb

Use of Ansible filters, like lookup, in container.yml is not supported. This is because we don't expect, nor require, Ansible to be installed as a prerequisite to Ansible Container.

@shalomb
Copy link
Author

shalomb commented Aug 15, 2017

@chouseknecht - I see. I'm struggling to find a clean way to pass envvars in .. right now, I mangle a env.yml and then do this

ansible-container --vars-files env.yml run --production

Is there a better way to have container.yml take in env-vars?

@chouseknecht
Copy link
Contributor

Within volumes we allow for the existence of ${some_var}. See config.py line 146. The intention of course was to allow for ${pwd}, but it works for any env var.

As a simple fix, I guess, we could apply the same to environment.

@shalomb
Copy link
Author

shalomb commented Aug 15, 2017

@chouseknecht - That would be very practical indeed!! It'd be a big time and complexity saver for us!

I had a go at seeing if I could try and make this change myself.

diff --git a/container/config.py b/container/config.py
index 8a6384e..562bf17 100644
--- a/container/config.py
+++ b/container/config.py
@@ -138,6 +138,12 @@ class BaseAnsibleContainerConfig(Mapping):
                 dev_overrides = service_config.pop('dev_overrides', {})
                 if env == 'dev':
                     service_config.update(dev_overrides)
+            if 'environment' in service_config:
+                # Expand environment variables
+                updated_envvars = []
+                for var in service_config['environment']:
+                    updated_envvars.append(path.expandvars(var))
+                service_config['environment'] = updated_envvars
             if 'volumes' in service_config:
                 # Expand ~, ${HOME}, ${PWD}, etc. found in the volume src path
                 updated_volumes = []

But this doesn't stick and my ability with python is limited. I've given up (for now).

@bmcgair
Copy link

bmcgair commented Feb 3, 2018

Is this solved? I'm having a similar issue, trying to use --with-variables on the ansible-container build command with no luck at all. Only documentation seems to be:

--with-variables WITH_VARIABLES [WITH_VARIABLES ...]

I'm trying to use --with-variables like --extra-vars in ansible-playbook but I'm not sure if I have the syntax correct based on whatever this example is supposed to mean.

Anybody have a working example of --with-variables? I just need to pass an os ENV into the ansible-container build. Seems like it shouldn't be this hard.

@l4r1k4
Copy link

l4r1k4 commented Mar 11, 2019

    environment:  # List or mapping of environment variables
      - DISTRO=bionic
      - TYPE=lamp

This is how I am using ENV vars, hope that helps.

@l4r1k4
Copy link

l4r1k4 commented Mar 11, 2019

the form FOO='{{ bar }}' works too

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants