Score:0

Replace list placeholder with Ansible

th flag

I have a very simple ansible playbook which looks like follows:

- hosts: myhost
  tasks:
  - include_vars:
      file: ./resolved.yml

  - name: resolve placeholders
    tags: resolution
    template:
      src: "./placeholders.yml"
      dest: "/tmp/res.yml"

Given the following YAML with placeholders:

---
test:
  config: {{ prop.resolveme }}

and the following secrets YAML file:

---
prop:
  resolveme:
    - 8.8.8.8
    - 1.1.1.1
    - 192.168.0.1

The resulting resolved YAML file looks as follows:

---
test:
  config: [u'8.8.8.8', u'1.1.1.1', u'192.168.0.1']

(Note: if I simply use a comma-separated list, it works as intended).

Why does this happen? I can't find anything in the YAML spec that explains this behaviour, so I'm thinking it must be something that the Ansible parser does (related to unicode it seems?). Is it possible to get my intended list structure using Ansible parameter interpolation, or should I prefer comma-separated lists of items?

Score:1
br flag

I can't reproduce your problem in Linux and ansible [core 2.11.6] with python version = 3.8.5 and jinja version = 3.0.1. Try explicit conversion to string. The template below

---
test:
  config: {{ prop.resolveme }}
  config: {{ prop.resolveme|map('string') }}

gives

---
test:
  config: ['8.8.8.8', '1.1.1.1', '192.168.0.1']
  config: ['8.8.8.8', '1.1.1.1', '192.168.0.1']

If this does not help you can try brute-force

---
test:
    config: [{% for i in prop.resolveme %}'{{ "%s"|format(i) }}'{% if not loop.last %}, {% endif%}{% endfor %}]
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.