我正在尝试在本地流浪者vm中安装和配置应用程序服务器。对于配置,使用Ansible。 配置可在第一次启动完成后完成。此时它正在侦听localhost:8000。之后,我可以复制配置文件并重新启动服务。之后,端口更改为8081。
问题是,在覆盖配置文件之前,我需要等待第一次启动完成。要检查第一次启动是否已完成,我在循环中使用了url模块,这很有效。所以vm在第一个 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
我的想法是,关于返回代码,选择正确的块,跳过另一个。但结果如下:
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模块的条件检查存在问题。 当单独使用时,没有条件(何时)而不是在块内部,它正在工作。我的猜测是,Ansible在组合之前和之后都存在问题。我在文档中找不到任何相关内容。所以任何帮助都表示赞赏,并提前感谢您。
答案 0 :(得分:1)
首先,我建议不要仅为一(1)个任务使用块。它没有意义。
其次,我建议使用wait_for
模块检查您的端口。因此,如果您想检查端口8000或8001,请执行以下操作:
---
- name: Wait_For Example
hosts: localhost
gather_facts: False
tasks:
- name: Wait for port 443
wait_for:
port: 443
delay: 0
timeout: 3
state: started
register: _port443
ignore_errors: True
- name: Wait for port 8001
wait_for:
port: 8001
delay: 0
timeout: 3
state: started
register: _port8001
ignore_errors: True
- name: Debug
debug:
var: _port443
when:
- _port443.state is defined
- _port443.state == "started"
- _port443.port == 443
- name: Debug
debug:
var: _port8001
when:
- _port8001.state is defined
- _port8001.state == "started"
- _port8001.port == 443
请注意,因为在两种情况下都使用相同的变量started
。
答案 1 :(得分:0)
我得到了它的工作。这是第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