问题描述
我是Makefile的新手。这是我在Makefile中使用ansible的PHONY目标
.PHONY: healthcheck-webproxy
healthcheck-webproxy:
ansible-playbook -i inventory.py -e "env=dev" -e "group=webproxy" -e "command='curl -k
-s https://localhost:${nginx_https_port}/healthcheck'" site.yaml --
limit=vm4node.lite.com -bv
当我运行make healthcheck-webproxy
时,它会跳过$ {nginx_https_port}
我得到下面的滑稽主语
$make healthcheck-webproxy
ansible-playbook -i inventory.py -e "env=dev" -e "group=webproxy" -e "command='curl -k -s https://localhost:/healthcheck'" limit=vm4node.lite.com -bv
有人可以帮我这个忙吗?我不希望makefile跳过$ {nginx_https_port} 任何帮助将不胜感激。
我想在这里添加代码。
- hosts: "{{ env }}"
gather_facts: false
become: true
become_method: sudo
tasks:
- name: Check HealthService
shell: docker exec {{ container_name }} sh -c {{ command | quote }}
在命令行管理程序中,它应该以此方式执行。
#docker exec webproxy sh -c'curl -k -s https:// localhost:$ {nginx_https_port} / healthcheck'
解决方法
您需要在makefile中转义$
。将${nginx_https_port}
变成\${nginx_https_port}
。
makefile中的字符串为:
"command='curl -k -s https://localhost:${nginx_https_port}/healthcheck'"
因为它是一个用双引号引起来的字符串,所以发生了变量替换/扩展。因此${nginx_https_port}
在makefile中展开,在makefile运行所在的主机上。为防止这种情况,您需要对$进行转义,以便将该字符串原样加载到ansible中。
在单引号字符串中(例如'echo $ hello'),不会发生变量替换/扩展,这也是为什么docker exec(在shell上执行)可以为您工作的原因:
docker exec webproxy sh -c 'curl -k -s https://localhost:${nginx_https_port}/healthcheck'
在这里,字符串curl -k -s https://localhost:${nginx_https_port}/healthcheck'
是按原样分配给docker容器内的sh的,然后在其中不加引号的情况下执行该字符串,并且可以扩展该变量。