Saturday, October 29, 2022

[SOLVED] Ansible Playbook fails when executed through Jenkins, but works when executed on CLI

Issue

I need the files from a directory that match a particular pattern and the playbook below works at the command line but fails when executed within Jenkins.

Is there a solution for executing shell commands within Jenkins using an Ansible playbook that contains wildcard values?

Jenkins 2.319.2

This my playbook:

- 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": []}

Solution

Taking the error message:

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

the hypothesis that I would start investigating are:

  • Does the path exist? My guess is that it would exist, based on the comment "...works at the command line"
  • Will the user in the playbook have permissions to read in that directory? Ansible's user, defined in your ansible.cfg can have different permissions, or be part of different groups than the ones used in the "command line" test; if this is the case, you will need to use become_user (as a last resource become), more information is available here
  • Using shell to find patterns is flimsy and prone to errors, the suggestion is to use Ansible's find module.


Answered By - Carlos Monroy Nieblas
Answer Checked By - Marilyn (WPSolving Volunteer)