Ansible剧本在通过Jenkins执行时失败,但在CLI上执行时正常

w46czmvw  于 2022-11-01  发布在  Jenkins
关注(0)|答案(1)|浏览(229)

我需要一个目录中与特定模式匹配的文件,下面的剧本在命令行中工作,但在Jenkins中执行时失败。
有没有一个解决方案可以使用包含通配符值的Ansible剧本在Jenkins中执行shell命令?
Jenkins2.319.2
这是我的战术手册:

- name: A Playbook to build artifacts
  hosts: targetservers
  vars:
    path_to_files: /opt/app/jenkins/sandbox_project_12345
  debugger: never
  become: yes
  become_user: root
  gather_facts: no
  remote_user: test_user
  ignore_errors: no
  tasks:
    - name: ping all
      ping:

    - name: List of files to be copied
      shell: bash -c 'ls {{ path_to_files + "/*006*" }}'
      delegate_to: localhost
      register: files_to_be_copied

    - name: Loop through files
      debug:  msg="{{ "Item = " + item  }}"
      with_items: "{{ files_to_be_copied.stdout_lines  }}"

    ```

TASK [List of files to be copied]**********************************************
fatal: [server.company_name.com -> localhost]: FAILED! => {"changed": true, "cmd": "bash -c 'ls /opt/app/jenkins/sandbox_project_12345/2212.00*006*'", "delta": "0:00:00.010146", "end": "2022-10-25 14:34:54.516178", "msg": "non-zero return code", "rc": 2, "start": "2022-10-25 14:34:54.506032", "stderr": "ls: cannot access /opt/app/jenkins/sandbox_project_12345/2212.00*006*: No such file or directory", "stderr_lines": ["ls: cannot access /opt/app/jenkins/sandbox_project_12345/2212.00*006*: No such file or directory"], "stdout": "", "stdout_lines": []}
2w3kk1z5

2w3kk1z51#

获取错误消息:

"stderr": "ls: cannot access /opt/app/jenkins/sandbox_project_12345/2212.00*006*: No such file or directory"

我将要开始研究假设是:

  • 路径存在吗?我的猜测是它会存在,基于注解“*...在命令行工作 *”
  • 行动手册中的用户是否拥有读取该目录的权限?Ansible的用户(在ansible.cfg中定义)可以拥有不同的权限,或者属于与“* 命令行 *”测试中使用的组不同的组;如果是这种情况,您将需要使用 become_user(作为最后一个资源 become),更多信息可用here
  • 使用shell查找模式是脆弱的,而且容易出错,建议使用Ansible'sfindmodule

相关问题