运行 詹金斯管道上的 ansible 剧本

run ansible playbook on jenkins pipeline

我打开这个主题是因为我在 Jenkins 管道中尝试 运行 ansiblePlaybook 时遇到问题。插件ansible已安装并启用。我写了一个 Jenkinsfile,我在其中设置环境并调用我的 ansible 管道,如下所示:

如您所见,我还验证了我的 ansible 在路径中并且它识别了它。但它不能 运行 它。在错误的输出下方:

我也试过 运行 它就像一个 shell 命令:

但是它无法连接到我的服务器,ssh 连接失败:

但是当我在自由式工作中使用 ansible 时它运行良好所以我不明白为什么!

希望有人能帮我解决这个问题,即使是第二个问题也有解决方案,这样我就可以有一个临时解决方案。请原谅我的英语不好,这不是我的母语。

此致,

首先感谢 Matt Schuchard 和 Jimit Raithatha 的回答。我找到了解决问题的方法。我使用 ansible 插件并使用以下代码添加 ansible:

withEnv(["PATH+ANSIBLE"=${tool 'name of the tool in tool configuration'}])

但是 withEnv 仅适用于 shell 命令 (sh '...') 并且不适用于 Java 调用,就像我对 ansiblePlaybook 所做的那样。命令 ansiblePlaybook 有一个参数,允许用户指定你想使用的工具在哪里,我们只需添加使用自定义工具配置中指定名称的参数,对于 ssh 错误我根本没有配置我的 sshagent;这里有一个例子:

    sshagent (credentials: ['name of the credential']) {
                ansiblePlaybook(
                    credentialsId: id of the credential',
                    inventory: 'inventory file',
                    installation: 'name of the tool specified on the configuration tool screen',
                    limit: 'host where run the playbbok',
                    playbook: 'path to the playbook',
                    extras: ' options and var that you want add for instance verbose mode : -vvv'
                )
            }

希望有同样问题的人可以通过这个话题得到帮助!

此致,