Home > Blockchain >  Jenkins ERROR: script returned exit code 4
Jenkins ERROR: script returned exit code 4

Time:05-04

I got a simple Groovy script to install agents on my servers using Ansible.
After I run the pipeline I get error about

ERROR: script returned exit code 4 Finished: FAILURE

The error happens because I have two instances not running (I don't want them running) and I get connection time out from them.
Is there a way to get Jenkins to ignore such errors?

The pipeline error

CodePudding user response:

A not-so-ideal solution would be to just state ignore_unreachable: yes at the top of you playbook.

This is no ideal because you risk missing on unreachable hosts you do care about.

A possibly better solution would be to gracefully end those unreachable hosts in a meta task based on a list of host(s) you don't need up and running.

For example:

- hosts: localhost, ok-if-down
  gather_facts: no

  pre_tasks:
    - ping:
      ignore_unreachable: yes
      register: ping

    - meta: end_host
      when:
        - inventory_hostname in _possibly_unreachable_hosts
        - ping is unreachable
      vars:
        _possibly_unreachable_hosts:
          - ok-if-down
          ## add more host(s) name in this list, here

  tasks:
    ## here goes your current tasks

When run, the exit code of this playbook would be 0:

$ ansible-playbook play.yml; echo "Return code is $?"

PLAY [localhost, ok-if-down] **************************************************
TASK [ping] *******************************************************************
fatal: [ok-if-down]: UNREACHABLE! => changed=false 
  msg: 'Failed to connect to the host via ssh: ssh: Could not resolve hostname ok-if-down: Name does not resolve'
  skip_reason: Host ok-if-down is unreachable
  unreachable: true
ok: [localhost]

TASK [meta] *******************************************************************
skipping: [localhost]

TASK [meta] *******************************************************************

PLAY RECAP ********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
ok-if-down                 : ok=0    changed=0    unreachable=1    failed=0    skipped=1    rescued=0    ignored=0   

Return code is 0
  • Related