可以通过使用set_fact模块和Jinja2表达式的组合来实现在“:”符号的左侧使用Ansible变量。在下面的示例中,我们将使用set_fact模块和Jinja2表达式将constr变量定义为“{{ansible_hostname}}:{{some_string}}”。然后,我们将使用someservice变量并将constr放在“:”符号的左侧。
---
- hosts: myhost
vars:
some_string: 'test'
someservice: '{{ "http://" + constr + ":8080" }}'
tasks:
- name: Set the constr variable
set_fact:
constr: "{{ansible_hostname}}:{{some_string}}"
- name: Print the value of someservice
debug:
var: someservice
运行上面的playbook后,someservice变量的值应为“http://myhost:test:8080”。
上一篇:Ansible变量具有不一致的值
下一篇:Ansible变量空值