我需要来自与特定模式匹配的目录的文件,下面的剧本在命令行中有效,但在Jenkins中执行时失败。
有没有一个解决方案可以使用包含通配符值的Ansible剧本在Jenkins中执行shell命令?
Jenkins 2.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": []}
获取错误消息:
"stderr": "ls: cannot access /opt/app/jenkins/sandbox_project_12345/2212.00*006*: No such file or directory"
我将开始调查的假设是:
- 路径存在吗?我的猜测是,根据评论">。。。工作在命令行">
- 剧本中的用户是否有权在该目录中阅读?在您的Ansible.cfg中定义的Ansible的用户可以具有不同的权限,或者是与">命令行";测验如果是这种情况,您将需要使用become_user(作为最后一个资源变成(,更多信息可在此处获得
- 使用shell查找模式是脆弱的并且容易出错,建议使用Ansible的find模块