在构建期间如何通过Ansible访问打包机(IP /主机)

时间:2019-01-21 05:36:32

标签: ansible packer

我正在尝试使用打包程序和使用GCP的ansible集成提供程序来构建我的应用程序。但是在构建过程中,我需要执行一些curl命令,而我却找不到执行该操作的方法。

我已经尝试过“ localhost”,然后尝试了一些ansible变量,例如“ {{ansible_host}}”(返回无效的127.0.0.1)和“ {{ansible_default_ipv4.address}}”(未找到变量)

我正在yml.j2文件中使用它们。这可能是问题的原因吗?

错误-> 用“ ansible_host”测试

[0;32m    googlecompute: TASK [deploy-XXXXX : Create the XXXXX collection] ***************************[0m
[0;32m    googlecompute: fatal: [localhost]: FAILED! => {"changed": false, "content": "", "msg": "Status code was -1 and not [200]: Request failed: <urlopen error [Errno 111] Connection refused>", "redirected": false, "status": -1, "url": "http://127.0.0.1:8765/api/v1/collections/XXXXX"}[0m
[0;32m    googlecompute:    to retry, use: --limit @/deploy-XXXXX.retry[0m
[0;32m    googlecompute:[0m
[0;32m    googlecompute: PLAY RECAP *********************************************************************[0m
[0;32m    googlecompute: localhost                  : ok=2    changed=1    unreachable=0    failed=1[0m

使用“ ansible_default_ipv4.address”进行测试

[0;32m    googlecompute: TASK [deploy-XXXXX : copy environment specific variables] *********************[0m
[0;32m    googlecompute: fatal: [localhost]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'ansible_default_ipv4' is undefined"}[0m
[0;32m    googlecompute:    to retry, use: --limit @/deploy-XXXXX.retry[0m
[0;32m    googlecompute:[0m
[0;32m    googlecompute: PLAY RECAP *********************************************************************[0m
[0;32m    googlecompute: localhost                  : ok=0    changed=0    unreachable=0    failed=1[0m
[0;32m    googlecompute:[0m

这是剧本片段

- name: Validate server runing
  uri:
    url: "http://{{ ansible_default_ipv4.address }}:{{ port }}/validate"
    method: PUT
    body_format: json
    body: "{\"test\":\"validate\"}"
    status_code: 200
    headers:
      Content-Type: "application/json"

1 个答案:

答案 0 :(得分:0)

我能够通过将委托_至:默认添加到任务来解决此问题。例如

- name: Validate server runing
  uri:
    url: "http://{{ ansible_default_ipv4.address }}:{{ port }}/validate"
    method: PUT
    body_format: json
    body: "{\"test\":\"validate\"}"
    status_code: 200
    headers:
      Content-Type: "application/json"
  delegate_to: default