ansible / ansible-container

DEPRECATED -- Ansible Container was a tool to build Docker images and orchestrate containers using only Ansible playbooks.
GNU Lesser General Public License v3.0
2.19k stars 392 forks source link

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

Open drzraf opened 6 years ago

drzraf commented 6 years ago
ISSUE TYPE
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)
jwarkentin commented 6 years ago

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.

nixar commented 6 years ago

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