Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Jenkins中使用ant命令运行Ansible playbook_Jenkins_Ant_Ansible_Jenkins Plugins - Fatal编程技术网

在Jenkins中使用ant命令运行Ansible playbook

在Jenkins中使用ant命令运行Ansible playbook,jenkins,ant,ansible,jenkins-plugins,Jenkins,Ant,Ansible,Jenkins Plugins,我正在制作一个剧本来自动化环境安装。目前我正在测试一位同事编写的一些脚本,我只想让jenkins运行ant命令来检查脚本结构是否正确。这些脚本位于目标机器中,因为我们只想测试它们,如果脚本不正确,则无需将文件从一台机器复制到另一台机器 在我的Jenkins中,我配置了推荐的jdk和ant版本,以便在创建参数化的Fressyle作业后部署请求的环境。我要运行的任务是: --- - name: compile all shell: ant all args: chdir: "{{ e

我正在制作一个剧本来自动化环境安装。目前我正在测试一位同事编写的一些脚本,我只想让jenkins运行ant命令来检查脚本结构是否正确。这些脚本位于目标机器中,因为我们只想测试它们,如果脚本不正确,则无需将文件从一台机器复制到另一台机器

在我的Jenkins中,我配置了推荐的jdk和ant版本,以便在创建参数化的Fressyle作业后部署请求的环境。我要运行的任务是:

---
- 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
,命令就会运行

,感谢masseyb,我发现了问题。在我的目标机器中,我安装并创建了ant_HOME,并将ant_HOME/bin添加到路径中

但是,由于这是在bash_profile上声明的,jenkins无法识别该命令,因为它不构成bash profile的源代码,所以我在ant命令之前添加了
source{{{ansible_env.HOME}/.bash_profile&
,以便jenkins用户可以为运行命令所需的环境变量提供源代码;博士 使用Ant插件或确保
Ant
可执行文件在
ansible
运行时
$PATH
中可用

Ant插件解决方案 添加调用Ant作为作业配置中的构建步骤(Jenkins Ant插件):

注意:此示例在master上运行。
ant
可执行文件是在全局工具配置中定义的<没有在jenkins用户
$PATH
中设置code>ant,jenkins使用为作业配置的
ant
可执行文件

如果Jenkins配置为在远程节点上构建,并且如果在Jenkins全局工具配置中定义的同一位置上没有可用的
ant
安装,则需要覆盖节点的节点属性中的
ant
可执行位置配置

易变解 要理解此解决方案,您需要理解错误。参考您的(修订)意见:

但是蚂蚁在那里。如果我在我的目标机器上运行ant,ant会给出一个错误,因为文件夹上没有build.xml,但在没有文件夹的情况下工作。ant env变量已设置,并且在PATH中也可用。但是你给了我一个主意/如果目标机器已经设置了包括ant、env var和PATH在内的所有组件,那么可能就是bash_概要文件的源

Ansible连接是非交互式的,在使用Ansible命令时,您可以在调试跟踪中看到这一点,这是一个修订的示例:

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\u profile
在非交互式登录期间不执行命令。从
manbash

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登录期间从中执行命令的
profile
,或将环境设置为
ansible
()

ansible
可执行文件
$PATH
中没有
ant

ansible
可执行文件
$PATH
中使用
ansible
环境
关键字设置
ant

---

- 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
---

- 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: "{{ lookup('env', 'WORKSPACE') }}"
    register: ant_build
    ...

通过Jenkins在
ansible
可执行文件
$PATH
中使用
ansible
环境
关键字设置中使用
ant
执行稍微修改的
ansible剧本

---

- 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
---

- 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: "{{ lookup('env', 'WORKSPACE') }}"
    register: ant_build
    ...

如果我正确理解了这个问题,您的Jenkins作业将被配置为执行一个ansible playbook,它将抛出ant调用?有没有理由不使用Jenkins ant插件(例如,将调用ant作为构建步骤)?错误是明确的,ant在ansible的路径中不可用。但是ant在那里。如果我在目标机器上运行
ant
,ant会给出一个错误,因为该文件夹上没有build.xml,但在没有帮助的情况下工作。ant env变量已设置,并且在PATH中也可用。但是你给了我一个主意/如果目标机器具有所有设置,包括ant、env var和PATH,则可能是bash_概要文件的源。要测试它并给出反馈不是詹金斯不识别
ant
命令,而是
ansible
不识别
ant
命令。执行顺序为jenkins->ansible->ant。如果在全局工具配置中配置了工具的位置,那么jenkins在查找
ant
ansible
时不会有问题,但是这并不意味着jenkins设置的
$PATH
与jenkins生成的进程可用的
$PATH
相同。这种情况下的问题是由
ansible
(非交互式shell)建立的连接类型不执行
.bash\u profile
命令。您可以从等式中删除Jenkins,错误将持续存在,因为它与
ansible
和操作系统配置相关。话虽如此,如果您打算使用Jenkins,那么我建议您使用Ant插件,而不是使用Ansible插件来shell out
Ant