2

I have a task which runs an uri call to get a JSON file containing a YAML fragment (it is obtained from Rancher API). I'm able to extract the YAML fragment using the following task

- name: generate_stack_call_body.yml read docker compose from catalog
  set_fact:
    docker_compose: '{{ template_detail|json_query(dc_query) }}'

When I run the ansible debug strategy, it indicates my docker_compose variable contains

(debug) p vars['docker_compose']
u"consul:\n  labels:\n    io.rancher.container.hostname_override: container_name\n    io.rancher.container.pull_image: always\n    io.rancher.container.hostname_override: container_name\n    io.rancher.scheduler.global: 'true'\n  stdin_open: true\n  image: registry.mycompany.com/my-project/consul-rancher:0.9.0\n  volumes:\n  - /data/consul:/consul/data\nload-balancer:\n  ports:\n  - 8500:8500\n  labels:\n    io.rancher.container.hostname_override: container_name\n    io.rancher.scheduler.global: 'true'\n  stdin_open: true\n  image: rancher/load-balancer-service\n  links:\n
  - consul:consul\n"

Which looks like valid YAML to me.

As a consequence, I guess it should be possible to have that value interpreted as a valid Ansible variable.

But how can I have it interpreted as a variable?

And how can I later put back that variable in a string?

techraf
  • 64,883
  • 27
  • 193
  • 198
Riduidel
  • 22,052
  • 14
  • 85
  • 185

1 Answers1

5

But how can I have it interpreted as a variable?

set_fact:
  docker_compose: '{{ template_detail | json_query(dc_query) | from_yaml }}'

And how can I later put back that variable in a string?

{{ docker_compose | to_yaml }}

Reference

techraf
  • 64,883
  • 27
  • 193
  • 198