ansible-container icon indicating copy to clipboard operation
ansible-container copied to clipboard

document how to pass variables from vars.yml down to roles inside container.yml

Open drzraf opened this issue 6 years ago • 2 comments

ISSUE TYPE
  • Documentation Report
container.yml
{ role: mysql, db: "{% raw %} {{ site.mysql }} {% endraw %}" }
OS / ENVIRONMENT
Ansible Container, version 0.9.2
Linux, 4.9.0-5-amd64, #1 SMP Debian 4.9.65-3+deb9u2 (2018-01-04), x86_64
2.7.13 (default, Nov 24 2017, 17:33:09) 
[GCC 6.3.0 20170516] /usr/bin/pytho
SUMMARY

As stated in https://groups.google.com/forum/#!topic/ansible-container/PLFQ10cdNII variable interpolation seems to suffer some strange effect when passed to roles inside container.yml. Let's say you want to transmit the mysql property of the site object down to a role (which, for example, makes use of {{ mysql.user }}. Neither:

{ role: mysql, mysql: "{{ site.mysql }}" }

would work (because of interpolation issue) Nor would

{ role: mysql, mysql: "{% raw %} {{ site.mysql }} {% endraw %}" }

because resulting mysql object var is not considered an object by the role, but a string. If the former syntax can't be made to work inside container.yml, at least it should be documented how to correctly pass complex variables.

STEPS TO REPRODUCE

Have an object inside vars/main.yml

site:
  mysql:
    root_password: pwd
    db: wordpress
    user: wordpress
    password: password

Run ansible-container --debug --vars-files vars/main.yml build

EXPECTED RESULTS
2018-03-13T14:44:58.136520 playbook                       [container.core] caller_file=/_ansible/container/core.py caller_func=run_playbook caller_line=552 playbook=[{'hosts': u'wpdb', 'roles': [ordereddict([('role', 'mysql')])], 'vars': {u'mysql': {u'password': u'password', u'db': u'wordpress', u'root_password': pwd', u'user': u'wordpress'}}}]
2018-03-13T14:44:58.157126 Running Ansible Playbook       [container.core] caller_file=/_ansible/container/core.py caller_func=run_playbook caller_line=621 command=ansible-playbook -vvvv  -i /tmp/tmpOKoddc/hosts -c docker  /tmp/tmpOKoddc/playbook.yml  cwd=/src
ACTUAL RESULTS
ruamel.yaml.scanner.ScannerError: mapping values are not allowed here
  in "<unicode string>", line 3, column 22:
      mysql: '{'password': 'password', 'db': 'wordpress', ...
                         ^ (line: 3)

drzraf avatar Mar 13 '18 14:03 drzraf

Running into this same issue. I can't seem to find any way to pass complex configuration to a role with ansible-container. This seems like a significant oversight and it's put me in a bit of a bind. I'm guessing I'm going to have to find a way to make sure the config file is present in the container and then pass a path to it and have my role read it in separately.

jwarkentin avatar Apr 10 '18 19:04 jwarkentin

It's wore than that, I just found out that passing a YAML within a string (for piping into from_yaml in the role) does not work either, as ansible-container magically unwraps it as soon as it's defined either in settings/defaults or within a var_file, and then again when the conductor is running if you've forcibly stringified it:

defaults:
  myvar: |
    a: 
      a: 1
      b: 2
...
services:
  myservice:
    roles:
    - role: myrole
      vars: 
        a: "{{ a | to_yaml }}"

nixar avatar May 24 '18 13:05 nixar