我有一个这样的清单:
[all:vars]
env_cidr_prefix='172.25'
antother_var="foo"
[VPN_SERVER]
vpn-server ansible_host="{{ env_cidr_prefix}}.0.1" 在ansible playbook期间,清单仅保存私有ip地址。我不想用公网ip替换"ansible_host="的内容
实战手册示例:
- name: grab the vpn public_ip
set_fact: PUBLIC_IP="{{ instance_eip.public_ip }}"
when: inventory_hostname |search("vpn-server")
- name: update inventory with the vpn public ip
replace:
path: "{{ inventory_file }}"
regexp: "{{ ansible_host }}"
replace: "{{ PUBLIC_IP }}"
when: inventory_hostname |search("vpn-server")如果
ansible_host="172.25.0.1"更换模块将正常工作。
但这失败了
ansible_host="{{ env_cidr_prefix}}.0.1" 调试输出:
ok: [vpn-server] => {
"changed": false,
"invocation": {
"module_args": {
"after": null,
"attributes": null,
"backup": false,
"before": null,
"content": null,
"delimiter": null,
"directory_mode": null,
"encoding": "utf-8",
"follow": false,
"force": null,
"group": null,
"mode": null,
"owner": null,
"path": "/home/toluna/ansible/openvpn/env.properties",
"regexp": "172.25.0.11",
"remote_src": null,
"replace": "1.1.1.1",
"selevel": null,
"serole": null,
"setype": null,
"seuser": null,
"src": null,
"unsafe_writes": null,
"validate": null
}
},
"msg": ""
}注意,我不能使用add_host模块,因为攻略运行在不同的阶段
有没有更好的方法呢?谢谢
发布于 2018-05-01 17:31:06
好的,在测试之后,我想我知道你想要实现什么了。
这里有几个部分:
清单文件如下所示:
vpn-server ansible_host="{{ env_cidr_prefix}}.0.1" 并且您正在尝试替换文件中不存在的172.25.0.1文本。你有"{{ env_cidr_prefix}}.0.1"而不是172.25.0.1。
选项:
主机如果您想要以这种方式进行替换,您可以在您的角色中使用主机文件,像替换Jenkins的/ trying.
托管攻略:
- name : Test
hosts: "{{ variable_vpn_ip | default('vpn-server') }}"并将其称为从您将临时更改的变量中读取,或者:
ansible-playbook play.yml -e "variable_vpn_ip=172.25.0.1"https://stackoverflow.com/questions/50112582
复制相似问题