我设置了一系列变量。称他们为Hosts和Inthosts。每个都有一个适当的值集,如调试输出中所示。当我尝试将inthosts的值分配给主机时,它实际上并不会使它们相同,它会将双引号更改为单引号并放置一个" u"在每个"项目"前面。有没有办法强制Ansible在这种情况下实际上做一个文字等于不解析文本?该文本应该被视为一个字符串。在这种情况下,"修改"值正在输出到文件,而更改会破坏事物。 计划是使用默认主机,如果相关服务器应使用不同的服务器集,则使用inthosts覆盖它。
默认变量设置
filebeat_kafka_hosts: '["x.compute-1.amazonaws.com:9093", "y.compute-1.amazonaws.com:9093"]'
filebeat_kafka_inthosts: '["x.compute-1.amazonaws.com:9093", "y.compute-1.amazonaws.com:9093", "z.compute-1.amazonaws.com:9093"]'
Ansible Code
- debug:
msg: "Hosts {{ filebeat_kafka_hosts }} "
- debug:
msg: "IntHosts {{ filebeat_kafka_inthosts }} "
- set_fact:
filebeat_kafka_hosts="{{ filebeat_kafka_inthosts }}"
- debug:
msg: "Inthosts -> hosts {{ filebeat_kafka_hosts }} "
输出(已修改)
"msg": "Hosts [\"x.compute-1.amazonaws.com:9093\", \"y.compute-1.amazonaws.com:9093\"] " |
"msg": "IntHosts [\"x.compute-1.amazonaws.com:9093\", \"y.compute-1.amazonaws.com:9093\", \"z.compute-1.amazonaws.com:9093\"] "
set {"ansible_facts": {"filebeat_kafka_hosts": ["x.compute-1.amazonaws.com:9093", "y.compute-1.amazonaws.com:9093", "z.compute-1.amazonaws.com:9093"]}, "changed": false}
"msg": "Inthosts -> hosts [u'x.compute-1.amazonaws.com:9093', u'y.compute-1.amazonaws.com:9093', u'z.compute-1.amazonaws.com:9093'] "
答案 0 :(得分:0)
Ansible将filebeat_kafka_inthosts
和filebeat_kafka_hosts
解释为列表。这给了你' u'调试中每个项目之前的字符。以下任务
- debug:
msg: "{{ item }}"
with_items: "{{ filebeat_kafka_hosts }}"
- debug:
msg: "{{ item }}"
with_items: "{{ filebeat_kafka_inthosts }}"
会给你
TASK [debug] *******************************************************************
ok: [127.0.0.1] => (item=y.compute-1.amazonaws.com:9093) => {
"item": "y.compute-1.amazonaws.com:9093",
"msg": "y.compute-1.amazonaws.com:9093"
}
ok: [127.0.0.1] => (item=x.compute-1.amazonaws.com:9093) => {
"item": "x.compute-1.amazonaws.com:9093",
"msg": "x.compute-1.amazonaws.com:9093"
}
TASK [debug] *******************************************************************
ok: [127.0.0.1] => (item=x.compute-1.amazonaws.com:9093) => {
"item": "x.compute-1.amazonaws.com:9093",
"msg": "x.compute-1.amazonaws.com:9093"
}
ok: [127.0.0.1] => (item=y.compute-1.amazonaws.com:9093) => {
"item": "y.compute-1.amazonaws.com:9093",
"msg": "y.compute-1.amazonaws.com:9093"
}
ok: [127.0.0.1] => (item=z.compute-1.amazonaws.com:9093) => {
"item": "z.compute-1.amazonaws.com:9093",
"msg": "z.compute-1.amazonaws.com:9093"
}
由于您将此行写入文件,因此您不必过于担心。 ' u' character是调试模块的副作用。将变量写入文件会产生相同的结果(尽管使用单引号而不是双引号)。
- lineinfile:
path: some_file
line: "{{ filebeat_kafka_hosts }}"
<强> SOME_FILE 强>
['x.compute-1.amazonaws.com:9093', 'y.compute-1.amazonaws.com:9093', 'z.compute-1.amazonaws.com:9093']
如果您确实需要双引号,可以使用to_json
过滤器
- lineinfile:
path: some_file
line: "{{ filebeat_kafka_hosts | to_json }}"
<强> SOME_FILE 强>
["x.compute-1.amazonaws.com:9093", "y.compute-1.amazonaws.com:9093", "z.compute-1.amazonaws.com:9093"]
答案 1 :(得分:0)
似乎将变量转换为字符串也有效。谢谢你的建议!