Red Cricket
Red Cricket

Reputation: 10470

How do I fix "KeyError: 'getpwuid(): uid not found: 1000'" when running ansible-playbook from jenkins

I have this jenkins pipeline that I use to execute ansible-playbook.

pipeline {
    agent {
        docker {
            image 'gableroux/ansible'
            args '-i --entrypoint='
        }
    }
    stages {
        stage('Setup parameters') {
            steps {
                script {
                    properties([
                        parameters([
                            string( defaultValue: 'hosts', name: 'INVENTORY', trim: true ),
                            string( defaultValue: '\'*\'', name: 'LIMIT', trim: true ),
                            string( defaultValue: 'shell', name: 'PLAYBOOK', trim: true ),
                            string( defaultValue: '--list-hosts', name: 'EXTRA_PARAMS', trim: true )
                        ])
                    ])
                }
            }
        }
        stage('Execute Ansible Playbook.') {
            steps {
                script {
                    env.DEFAULT_LOCAL_TMP = env.WORKSPACE_TMP
                    env.HOME = env.WORKSPACE

                    sh """
                    ansible-playbook -i ${INVENTORY} -l "${LIMIT}" ${PLAYBOOK} ${EXTRA_PARAMS}
                    """
                }
            }
        }
    }
}

I pass it these parameters:

INVENTORY -> ",localhost"
LIMIT -> ' '
PLAYBOOK -> 'the_playbook.yml'
EXTRA_PARAMS -> -vvv --connection=local --user root

The contents of the_playbook.yml are:

---
  - name: "Playing with Ansible and Git"
    hosts: localhost
    connection: local
    tasks:

    - name: "just execute a ls -lrt command"
      shell: "ls -lrt"
      register: "output"

    - debug: var=output.stdout_lines

When my pipeline runs I get this error message:

+ ansible-playbook -i ,localhost -l ' ' the_playbook.yml -vvv '--connection=local' --user root
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the 
controller starting with Ansible 2.12. Current version: 3.7.12 (default, Sep  8
 2021, 01:55:52) [GCC 10.3.1 20210424]. This feature will be removed from 
ansible-core in version 2.12. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.
ansible-playbook [core 2.11.5] 
  config file = /var/jenkins_home/workspace/run_ansibleplaybook/ansible.cfg
  configured module search path = ['/var/jenkins_home/workspace/run_ansibleplaybook/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.7/site-packages/ansible
  ansible collection location = /var/jenkins_home/workspace/run_ansibleplaybook/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.7.12 (default, Sep  8 2021, 01:55:52) [GCC 10.3.1 20210424]
  jinja version = 3.0.1
  libyaml = False
Using /var/jenkins_home/workspace/run_ansibleplaybook/ansible.cfg as config file
Parsed ,localhost inventory source with host_list plugin
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.

PLAYBOOK: the_playbook.yml *****************************************************
1 plays in the_playbook.yml

PLAY [Playing with Ansible and Git] ********************************************

TASK [Gathering Facts] *********************************************************
task path: /var/jenkins_home/workspace/run_ansibleplaybook/the_playbook.yml:2
The full traceback is:
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/ansible/executor/task_executor.py", line 158, in run
    res = self._execute()
  File "/usr/local/lib/python3.7/site-packages/ansible/executor/task_executor.py", line 532, in _execute
    self._connection = self._get_connection(cvars, templar)
  File "/usr/local/lib/python3.7/site-packages/ansible/executor/task_executor.py", line 874, in _get_connection
    ansible_playbook_pid=to_text(os.getppid())
  File "/usr/local/lib/python3.7/site-packages/ansible/plugins/loader.py", line 837, in get_with_context
    obj.__init__(instance, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/ansible/plugins/connection/local.py", line 50, in __init__
    self.default_user = getpass.getuser()
  File "/usr/local/lib/python3.7/getpass.py", line 169, in getuser
    return pwd.getpwuid(os.getuid())[0]
KeyError: 'getpwuid(): uid not found: 1000'
fatal: [localhost]: FAILED! => {
    "msg": "Unexpected failure during module execution.",
    "stdout": ""
}

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

What can I do to fix it?

Upvotes: 2

Views: 3886

Answers (1)

ycr
ycr

Reputation: 14574

Try the following.

sh '''
echo "tempuser:x:$(id -u):$(id -g):,,,:${HOME}:/bin/bash" >> /etc/passwd
echo "tempuser:x:$(id -G | cut -d' ' -f 2)" >> /etc/group
'''
sh """
ansible-playbook -i ${INVENTORY} -l "${LIMIT}" ${PLAYBOOK} ${EXTRA_PARAMS}
"""

Or you can try creating a user with UID 1000 in the base image. Refer this for more information.

Upvotes: 2

Related Questions