4

Hi, I need to print a variable as a number instead a string. Example:

- name: Create input
  uri:
    url: "https://{{ url_graylog }}/api/system/inputs"
    ...
    body_format: json
    body:
      title: "{{ name }}"
      configuration:
      bind_address: "0.0.0.0"
      port: "{{ port }}"  <-- its print as string, I need number
    global: true

I tried

port: {{ port }}          <-- not work
port: "{{ port | int }}"  <-- not work

Any idea? Thanks!

5
  • 1
    I found this note. I think the same problem Values passed in using the key=value syntax are interpreted as strings. Use the JSON format if you need to pass in anything that shouldn’t be a string (Booleans, integers, floats, lists etc). [docs.ansible.com/ansible/latest/user_guide/… Nov 14, 2018 at 20:07
  • Is it considered a string because of you have the quotes? Remove the quotes and do port: {{ port | int }} Nov 15, 2018 at 4:08
  • 1
    Be aware that you cannot do what William D Irons is saying because when YAML sees port: { it expects that to be an object literal, just like JSON, versus a jinja2 template which is port: {{, so you must use some kind of YAML quoting; either '{{ port | int }}' or port: >-\n {{ port | int }} etc. We will need more details about why "{{ port | int }}" <-- not work
    – mdaniel
    Nov 15, 2018 at 6:18
  • @MatthewLDaniel "{{ port | int }}" not work as a number, only as a string. My problem is because to value is string. I need that {{ port | int }} was number. But reading the documentation, I look this: ``` Values passed in using the key=value syntax are interpreted as strings. Use the JSON format if you need to pass in anything that shouldn’t be a string (Booleans, integers, floats, lists etc).``` :-( Nov 26, 2018 at 12:53
  • I tried some string manipulation with JSON strings. It is ugly, but maybe help. gist.github.com/leonardonsantos/…
    – Leonardo
    Dec 3, 2018 at 22:28

3 Answers 3

3

Actually it seems not possible to convert jinja template into integer since it always return string to Ansible. Detailed explanation here : https://github.com/ansible/ansible/issues/9362#issuecomment-302432118

However, I found a workaround consisting of using folded string bloc in yaml. In your case the Ansible task should look like this :

- name: Create inputenter code here
  uri:
    url: "https://{{ url_graylog }}/api/system/inputs"
    ...
    body_format: json
    body: >
      {
        "title": "{{ name }}",
        "configuration": {
          "bind_address": "0.0.0.0",
          "port": {{ port | int }}
        }
      }
    global: true

It is a little bit less readable but will produce a non-quoted value for port. The body sent looks like this :

...
"body": {
    "configuration": {
        "bind_address": "0.0.0.0",
        "port": 25565
    },
    "title": "My title"
},
...

Hope it helped !

1
  • This worked, thank you!!
    – ossys
    Oct 29, 2021 at 21:36
1

You can set in ansible.cfg: jinja2_native = True

2
  • Bear in mind this is a global setting implying a global impact.
    – Yuri
    Mar 26, 2021 at 14:12
  • with jinja2_native=True encrypted values ($ANSIBLE_VAULT;1.1;AES256...) will no longer be decrypted (tested on ansible v2.10.5)
    – canochordo
    Oct 31, 2021 at 21:40
1

Create your variable as such:

    variableName:
      name: "demo"
      capacityUnits: 1

Then body like this:

body: "{{ variableName | to_json }}"

Then the int will persist:

"body": "{\"name\": \"demo\", \"capacityUnits\": 1}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.