在Ansible中摆脱双重大括号 [英] Escaping double curly braces in Ansible
问题描述
例如,如何在以下shell命令中避免双重大括号?
- 名称:Test
shell:docker inspect --format'{{.NetworkSettings.IPAddress}}'instance1
这是一个非常干净可靠的本机解决方法, code> docker --inspect 带花括号。我们假设我们刚刚引用了一个容器,使用可解析码头模块:
- name:query客户端容器的IP
shell:docker exec {{docker_containers [0] .Id}} hostname -I
注册表:_container_query
- 名称:获取查询结果的IP
set_fact:
_container_ip:{{_container_query.stdout | regex_replace('\\s','')}}
您现在可以在变量 _container_ip
中具有Docker容器的IP。我也在我的文章上发布了这个解决方法:与Docker结婚的结婚
[更新2015-11-03]删除容器查询的标准输出的空格。
[更新2015-11-04] BTW,在官方的可靠存储库中有两个提取请求,这将通过恢复Docker模块返回的事实使得这种解决方法不必要。所以你可以通过 docker_containers [0] .NetworkSettings.IPAddress
访问码头接收器的IP。所以请投票支持这些拉票请求:
How to escape double curly braces in Ansible 1.9.2?
For instance, how can I escape double curly braces in the following shell command?
- name: Test
shell: "docker inspect --format '{{ .NetworkSettings.IPAddress }}' instance1"
Here is a mostly clean and Ansible native workaround not depending on docker --inspect
with curly braces. We assume we have just referenced one container with the Ansible docker module before:
- name: query IP of client container
shell: "docker exec {{ docker_containers[0].Id }} hostname -I"
register: _container_query
- name: get IP of query result
set_fact:
_container_ip: "{{ _container_query.stdout | regex_replace('\\s','') }}"
You now have the IP of the Docker container in the Variable _container_ip
. I also published this workaround on my article The Marriage of Ansible with Docker.
[Update 2015-11-03] Removed whitespaces of the stdout of the container query.
[Update 2015-11-04] BTW, there were two pull requests in the official Ansible repository, that would made this workaround needless by recovering the facts returned by the Docker module. So you could acces the IP of a docker container via docker_containers[0].NetworkSettings.IPAddress
. So please vote for those pull requests:
这篇关于在Ansible中摆脱双重大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!