【问题标题】:Phony-Targets for ansible skips $Ansible 跳过的 Phony-Targets $
【发布时间】:2020-08-17 13:31:23
【问题描述】:

我是 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} 我得到了下面的 ansible adhoc

$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 }}

在 Shell 中它应该以这种方式执行。

#docker exec webproxy sh -c 'curl -k -s https://localhost:${nginx_https_port}/healthcheck'

【问题讨论】:

  • nginx_https_port 是否应该是一个 environment 变量?编写的规则将尝试将其解释为 make 变量。您可以通过将$ 加倍以将其转义为make 来解决此问题。或者,您可以在 makefile 或 make 命令行中为其指定一个值(作为 make 变量)。
  • @JohnBollinger 如果它是一个环境变量,它将作为一个 make 变量导入到 GNU make 中,所以这会起作用。
  • Make 不是“跳过”该变量:它正在扩展该变量并且该变量没有值,因此如您所见,它被扩展为空字符串。您需要将该变量设置为某个值。应该设置在哪里?你的makefile中还有其他地方应该设置它吗?是否应该在命令行上设置(make nginx_https_port=8888)?在我们提供帮助之前,您必须提供更多信息,因为 makefile 的上述部分没有告诉我们。
  • 是的,它是环境变量。
  • 是的,它是环境变量。我没有定义变量..它应该从指定的容器中获取它。我试过localhost:$${nginx_https_port}/healthcheck 和localhost:$(${nginx_https_port})/它们都不起作用。

标签: ansible ansible-2.x makefile


【解决方案1】:

您需要转义 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,然后在没有任何引号的情况下执行它并且可以扩展变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 2017-10-04
    • 1970-01-01
    相关资源
    最近更新 更多