gpt4 book ai didi

ansible:将循环(直到)与条件(当)结合起来

转载 作者:行者123 更新时间:2023-12-02 20:24:08 25 4
gpt4 key购买 nike

我正在尝试在本地流浪虚拟机中安装和配置应用程序服务器。使用 Ansible 进行配置。首次启动完成后即可进行配置。此时它正在监听 localhost:8000。之后我可以复制配置文件并重新启动服务。之后端口更改为8081。

问题是,我需要等待第一次启动完成,然后才能覆盖配置文件。为了检查第一次启动是否完成,我在循环中使用了 url 模块,效果很好。因此,虚拟机在第一次 vagrant up --provision 时被配置。

但是,如果有另一个 vagrant 规定,并且 Ansible playbook 正在使用循环执行 url 模块,那么它会因为端口更改而失败。所以我尝试了这个:

- name: configure / check for default server.conf


- command: grep "url.httpport=8081" "/{{ base_dir }}/conf/server.conf"
ignore_errors: yes
register: output

- name: configure / Check that app-server is installed successfully (8000)
block:
- get_url:
url: http://localhost:8000/version.txt
dest: /tmp
register: result
until: result is succeeded
retries: 10
delay: 30
register: started
when: output.rc == 1

- name: configure / Check that app-server is installed successfully (8081)
block:
- get_url:
url: http://localhost:8081/version.txt
dest: /tmp
register: result
until: result is succeeded
retries: 10
delay: 30
register: started
when: output.rc == 0

我的想法是,关于返回代码,选择正确的 block 并跳过另一个 block 。但结果是这样的:

TASK [glue-tomcat8 : configure / check for default server.conf] ***
fatal: [devaws]: FAILED! => {"changed": true, "cmd": ["grep", ".url.httpport=8081", "/opt5/conf/server.conf"], "delta": "0:00:00.003016", "end": "2018-05-22 07:11:51.382776", "msg": "non-zero return code", "rc": 1, "start": "2018-05-22 07:11:51.379760", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
...ignoring

TASK [glue-tomcat8 : Check that app server is installed successfully (8000)] ***
fatal: [devaws]: FAILED! => {"msg": "The conditional check 'result is succeeded' failed. The error was: The failed test expects a dictionary"}

看来,get_url 模块的条件检查存在问题。当单独使用时,没有条件时(当)并且不在 block 内时,它可以工作。我的猜测是,Ansible 在组合直到和何时方面存在问题。我在文档中找不到与此相关的任何内容。因此,我们非常感谢任何帮助,并提前感谢您。

最佳答案

我成功了。这是第 8 层(我)的问题:)启动的变量是剩余的,不需要。看起来 Ansible 被这个变量激怒了。这是我的工作代码:

- command: grep "url.httpport=8081" "/{{ base_dir }}/conf/server.conf"
ignore_errors: yes
register: output

- get_url:
url: http://localhost:8000/version.txt
dest: /tmp
register: result
until: result is succeeded
retries: 10
delay: 30
when: output.rc == 1

- get_url:
url: http://localhost:8081/version.txt
dest: /tmp
register: result
until: result is succeeded
retries: 10
delay: 30
when: output.rc == 0

关于ansible:将循环(直到)与条件(当)结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50462074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com