Jenkins 版本:2.7.4
Ansible/Playbook 版本:2.1.1.0
Ansible 剧本文件:[ vagrant@/tmp ] $ ls -l pb.yml
-rw-rw-r--. 1 vagrant vagrant 189 Nov 2 05:39 pb.yml
[ 流浪者@/tmp ] $ 猫 $_
---
#- hosts: "{{ host }}"
- hosts: all
gather_facts: False
# vars:
# - host: "all"
tasks:
- name: Hello
shell: "echo hello"
tags:
- hello
- name: Bye
shell: "echo bye"
tags:
- bye
当我在命令行的ansible/host 机器上运行以下命令时,如果我只传递一个或多个主机,在主机值之间的“,”字符之后有或没有空格,它就可以工作:
$ /usr/bin/ansible-playbook tasks/pb.yml -i "app-server1" -f 5 --private-key /tmp/ssh738232337886876255.key -u ubuntu
如果我通过多个主机(cmd 行),它仍然有效(PS:即使我有,作为清单主机列表的后缀):
$ /usr/bin/ansible-playbook tasks/pb.yml -i "app-server1,web-server2" -f 5 --private-key /tmp/ssh738232337886876255.key -u ubuntu
$ /usr/bin/ansible-playbook tasks/pb.yml -i "app-server1,web-server2," -f 5 --private-key /tmp/ssh738232337886876255.key -u ubuntu
但是,在-i选项的值中传递了一个空格字符,我看到它适用于没有空间的服务器条目,并且对于带有空格后缀/前缀的同一主机失败(至少根据标准输出控制台上显示的内容) )。
例如,使用 ansible 命令运行“-m setup”(收集事实),请注意以下输出中的空格字符,位于proxy-server3 | 遥不可及!线。
如果我运行 -m setup(ansible 命令) - 或者 - 即使我运行相同的 playbook(ansible-playbook 命令),我也会看到类似的错误 UNREACHABLE ,如下所示:
$ /usr/bin/ansible all -m setup -i "app-server1, proxy-server3," -f 5 --private-key /tmp/ssh738232337886876255.key -u ubuntu
proxy-server3 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh.",
"unreachable": true
}
app-server1 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"10.124.12.101"
],
...more...json...stuff..here
...
现在,我在 Jenkins 的构建步骤中调用 Ansible Playbook 。在 Jenkins 中,我有一个名为hosts的参数化作业参数,我在“ Invoke Ansible Playbook ”插件的构建步骤中将其用作其库存部分的“内联内容”单选按钮。
当我只传递“app-server1”并启动作业时,Jenkins 正在成功运行作业并按预期运行 ansible-playbook .yml 文件。
但是,当我给hosts变量提供多个逗号分隔值时,它会出错。
如果Jenkins 中的hosts参数设置为多个主机,即:
app-server1,web-server2
OR
app-server1,app-server1(“,”字符旁边没有任何空格)
或
app-server1,web-server2, ( PS:这里 , 是值的后缀)。
我收到以下错误:
23:48:00 PLAY [all] *********************************************************************
23:48:00
23:48:00 TASK [hello] ******************************************************
23:48:01 fatal: [app-server1,web-server2]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true}
23:48:01
23:48:01 PLAY RECAP *********************************************************************
23:48:01 app-server1,web-server2 : ok=0 changed=0 unreachable=1 failed=0
23:48:01
23:48:01 FATAL: command execution failed
23:48:01 hudson.AbortException: Ansible playbook execution failed
23:48:01 at org.jenkinsci.plugins.ansible.AnsiblePlaybookBuilder.perform(AnsiblePlaybookBuilder.java:227)
23:48:01 at org.jenkinsci.plugins.ansible.AnsiblePlaybookBuilder.perform(AnsiblePlaybookBuilder.java:200)
23:48:01 at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:78)
23:48:01 at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
23:48:01 at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779)
23:48:01 at hudson.model.Build$BuildExecution.build(Build.java:205)
23:48:01 at hudson.model.Build$BuildExecution.doRun(Build.java:162)
23:48:01 at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:534)
23:48:01 at hudson.model.Run.execute(Run.java:1741)
23:48:01 at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
23:48:01 at hudson.model.ResourceController.execute(ResourceController.java:98)
23:48:01 at hudson.model.Executor.run(Executor.java:410)
23:48:01 ERROR: Ansible playbook execution failed
23:48:01 Finished: FAILURE
或
(当,用作hosts参数值的后缀)
23:48:01 fatal: [app-server1,web-server2,]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh.", "unreachable": true}
23:48:01
23:48:01 PLAY RECAP *********************************************************************
23:48:01 app-server1,web-server2, : ok=0 changed=0 unreachable=1 failed=0
23:48:01
23:48:01 FATAL: command execution failed
或者
如果我在两个主机值之间给出一个空格(不管末尾的后缀 , 字符如何),我会收到不同的错误消息。
示例:我将以下值传递给参数hosts:
app-server1、web-server2
或
app-server1、web-server2、proxy-server3、
错误示例:
23:49:53 ERROR! /tmp/inventory5444675088910579622.ini:1: Expected key=value host variable assignment, got: app-server1,
23:49:53 FATAL: command execution failed
23:49:53 hudson.AbortException: Ansible playbook execution failed
23:49:53 at org.jenkinsci.plugins.ansible.AnsiblePlaybookBuilder.perform(AnsiblePlaybookBuilder.java:227)
23:49:53 at org.jenkinsci.plugins.ansible.AnsiblePlaybookBuilder.perform(AnsiblePlaybookBuilder.java:200)
23:49:53 at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:78)
23:49:53 at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
23:49:53 at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779)
23:49:53 at hudson.model.Build$BuildExecution.build(Build.java:205)
23:49:53 at hudson.model.Build$BuildExecution.doRun(Build.java:162)
23:49:53 at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:534)
23:49:53 at hudson.model.Run.execute(Run.java:1741)
23:49:53 at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
23:49:53 at hudson.model.ResourceController.execute(ResourceController.java:98)
23:49:53 at hudson.model.Executor.run(Executor.java:410)
23:49:53 ERROR: Ansible playbook execution failed
问题:
当我在 ansible 命令或 ansible-playbook 命令中为-i选项的值提供空间时,为什么没有自动删除空间?如果主机在-i选项的双引号内用逗号“ , ”字符周围的空格字符传递,我该如何摆脱这种情况。我知道我可以事先使用sed但-i选项默认情况下应忽略 " , " 字符旁边的任何空格(空格是否在逗号字符旁边加前缀/后缀)。
为什么在我的 Jenkins 工作中(当我没有在参数值中留出任何空间时),当值具有“ , ”时它会出错,而它在命令行中成功?
给-i "app-server1"(在命令行)不起作用。但是,当我给出-i "app-server1,"时,它会起作用 (请参阅引号中的末尾)。
在 Jenkins 中使用参数(字符串类型)和使用 Invoke Ansible Playbook 构建步骤的行为完全相反;在那里,如果我给主机参数“app-server1”的值它可以工作,但给“app-server1”(后缀逗号字符)则不行。