在詹金斯中使用ant命令运行Ansible剧本

时间:2018-11-08 12:23:53

标签: jenkins ant ansible jenkins-plugins

我正在制作一部剧本来自动执行环境安装。目前,我正在测试一些由同事制作的脚本,我只希望jenkins运行ant命令来检查脚本结构是否正确。该脚本位于目标计算机上,因为我们只想对其进行测试,并且如果脚本不正确,则无需将文件从一台计算机复制到另一台计算机上。

在我的Jenkins中,我建议将jdk和ant版本配置为在创建参数化的fresstyle作业后部署所需的环境。我要运行的任务是:

---
- name: compile all
  shell: ant all
  args:
    chdir: "{{ env_home }}/Project/scripts"

- name: Create project tables
  shell: ant create-project-tables
  args:
    chdir: "{{ env_home }}/Project/scripts"

这是第一个包含至少一个在目标上运行的ant命令的角色。詹金斯总是返回

TASK [build_all : compile all] *****************************************
task path: /opt/testes/env-deployment/roles/build_all/tasks/main.yml:2
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ buildPrepaidAll }}
fatal: [srv1]: FAILED! => {"changed": true, "cmd": "ant all", "delta": "0:00:00.003675", "end": "2018-11-07 15:55:15.917745", "msg": "non-zero return code", "rc": 127, "start": "2018-11-07 15:55:15.914070", "stderr": "/bin/sh: ant: command not found", "stderr_lines": ["/bin/sh: ant: command not found"], "stdout": "", "stdout_lines": []}

Jenkins不会抱怨其他任何东西,只是找不到ant命令,如果我在运行该文件夹的同一文件夹中输入ant all

2 个答案:

答案 0 :(得分:1)

TL; DR

使用 Ant插件或确保ant运行时ansible中的$PATH可执行文件可用。

Ant插件解决方案

在工作配置( Jenkins Ant插件)中添加调用Ant 作为构建步骤

add an **Invoke Ant** build step to your Jenkins job configuration

configure the ***Invoke Ant** build step

build your job

注意:此示例在master上运行。 ant可执行文件是全局工具配置中定义的可执行文件。在詹金斯用户ant中未设置$PATH,詹金斯使用为作业配置的ant可执行文件。

Jenkins **Global Tool Configuration**

如果将Jenkins配置为在远程节点上构建,并且在Jenkins 全局工具配置中定义的相同位置无法进行ant安装,则需要覆盖{{1 }}可执行文件位于节点配置的节点属性> 工具位置中。

node configuration <code>ant</code> executable location override

node build with overridden <code>ant</code> executable location

解决方案

要了解此解决方案,您需要了解错误。参考您的(已编辑的)评论:

  

但是蚂蚁在那里。如果我在目标计算机上运行ant,则ant会给出错误,因为该文件夹上没有build.xml,但仍然可以工作。并设置了ant env变量,该变量在PATH中也可用。但是你给我一个主意。 /如果目标计算机具有包括ant,env var和PATH在内的所有设置,则可能是bash_profile的来源。 ...

Ansible连接是非交互式的,当您将ant与ansible命令一起使用时,您可以在调试跟踪中看到此信息,这是经过编辑的示例:

-vvvv

<172.28.128.13> SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/bmassey/.ansible/cp/881b67b3d5 172.28.128.13 '/bin/sh -c '...'命令在非交互式登录期间不执行。来自.bash_profile

man bash

您需要确保An interactive shell is one started without non-option arguments (unless -s is specified) and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state. ... When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior. 可执行文件ant中有ansible可执行文件,您的操作无关紧要,例如,编辑命令所在的$PATH在非交互式shell登录或使用profilehttps://docs.ansible.com/ansible/devel/user_guide/playbooks_environment.html)设置环境期间执行

ansible

- hosts: localhost environment: PATH: '/opt/ant/apache-ant-1.10.5/bin:{{ ansible_env.PATH }}' 可执行文件ant中没有ansibleno <code>ant</code> in <code>$PATH</code>

$PATH可执行文件ant中,使用ansible的{​​{1}}关键字设置了$PATH

ansible

with <code>ant</code> in <code>$PATH</code>

通过environment的{​​{1}}关键字设置的--- - hosts: localhost environment: - PATH: '/opt/ant/apache-ant-1.10.5/bin:{{ ansible_env.PATH }}' tasks: - name: execute ant build local_action: module: shell args: ant chdir: "{{ ansible_env.HOME }}/workspace/example" register: ant_build - debug: var=ant_build.stdout_lines 可执行文件ansible-playbook中的ant通过Jenkins执行的稍作修改的ansible

$PATH

<code>jenkins</code> -> <code>ansible</code> -> <code>ant</code> execution

答案 1 :(得分:0)

非常感谢masseyb,我找到了问题所在。在目标计算机中,我安装了ant并创建了ANT_HOME并将ANT_HOME / bin添加到PATH。

但是,因为这是在bash_profile上声明的,因此jenkins无法识别该命令,因为它不会成为bash概要文件的来源,因此我在ant命令之前添加了source {{ ansible_env.HOME }}/.bash_profile &&,因此jenkins用户可以获取运行所需的环境变量命令