Could a value refer another key’s value as variable

48 views
Skip to first unread message

David Wong

unread,
Jul 15, 2016, 7:59:05 PM7/15/16
to Ansible Project
Hi,

I would like to do something in playbook like

myapp:
  version: 1.2
  path: xxxx_{{version}

I want to refer another key’s value to devise new value. It does not work. 

Is it possible at all?


Regards,

David

jpea...@sparkpost.com

unread,
Jul 20, 2016, 3:43:16 PM7/20/16
to Ansible Project

On Friday, July 15, 2016 at 7:59:05 PM UTC-4, David Wong wrote:
myapp:
  version: 1.2
  path: xxxx_{{version}
 
I do this all the time, but I always quote the RHS of each assignment.  NOTE also that you are missing a closing brace in your example code.

John

David Wong

unread,
Jul 20, 2016, 11:50:00 PM7/20/16
to Ansible Project
Hi John

Thank for your reply, do you mean quoting like this:

---
- hosts: localhost
  vars:
    record:
      field1: 1
      field2: "{{field1}}"
  tasks:
   - name: This one does not work
     debug: msg="{{record.field2}}"
...

I still get error:

fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: {u'field2': u'{{field1}}', u'field1': 1}: 'field1' is undefined\n\nThe error appears to have been in 'xxxxx.yml': line 8, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n   - name: This one does not work\n     ^ here\n"}

Joanna Delaporte

unread,
Jul 21, 2016, 3:29:17 PM7/21/16
to Ansible Project
Does field2's value display correctly if you use the full path to the other variable?

---
- hosts: localhost
  vars:
    record:
      field1: 1
      field2: "{{record.field1}}"
  tasks:
   - name: This one does not work
     debug: msg="{{record.field2}}"

David Wong

unread,
Jul 22, 2016, 5:11:39 AM7/22/16
to Ansible Project
Hi Joanna,

I tried that as well, it still did not work, it will fail with:


.....recursive loop detected in template string: {{record.field1}}"}



David


Reply all
Reply to author
Forward
0 new messages