你好,
我需要将变量打印为数字而不是字符串。
例子:
- 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
我试过
port: {{ port }} <-- not work
port: "{{ port | int }}" <-- not work
任何的想法?谢谢!
最佳答案
实际上似乎不可能将 jinja 模板转换为整数,因为它总是将字符串返回给 Ansible。详细解释在这里:
https://github.com/ansible/ansible/issues/9362#issuecomment-302432118
但是,我找到了一种解决方法,包括在 yaml 中使用折叠字符串块。在您的情况下,Ansible 任务应如下所示:
- 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
它的可读性稍差,但会为端口生成一个未引用的值。发送的正文如下所示:...
"body": {
"configuration": {
"bind_address": "0.0.0.0",
"port": 25565
},
"title": "My title"
},
...
希望有帮助!
关于json - Ansible:如何在 JSON 中打印数字而不是字符串 - 模块 uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53307595/