我需要來自與特定模式匹配的目錄中的檔案,下面的劇本在命令列中作業,但在 Jenkins 中執行時失敗。
是否有使用包含通配符值的 Ansible 劇本在 Jenkins 中執行 shell 命令的解決方案?
詹金斯 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": []}
uj5u.com熱心網友回復:
獲取錯誤訊息:
"stderr": "ls: cannot access /opt/app/jenkins/sandbox_project_12345/2212.00*006*: No such file or directory"
我將開始調查的假設是:
- 路徑存在嗎?我的猜測是它會存在,基于評論“ ......在命令列中作業”
- 劇本中的用戶是否有權讀取該目錄?Ansible 的用戶,在你的 ansible.cfg 中定義可以有不同的權限,或者是與“命令列”測驗中使用的不同組的一部分;如果是這種情況,您將需要使用become_user(作為最后一個資源become),更多資訊可在此處獲得
- 使用 shell 查找模式很脆弱且容易出錯,建議使用Ansible 的find模塊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/522998.html
標籤:壳詹金斯可靠的
