groovy 如何修复“KeyError:'getpwuid()函数:找不到uid:1000英尺“当运行来自Jenkins的sible剧本时

5f0d552i  于 2022-11-01  发布在  Jenkins
关注(0)|答案(1)|浏览(159)

我有一个jenkins管道,用来执行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}
                    """
                }
            }
        }
    }
}

我将这些参数传递给它:

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

the_playbook.yml的内容为:

---
  - 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

当我的管道运行时,我收到以下错误消息:

+ 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

我能做些什么来修复它?

pbgvytdp

pbgvytdp1#

请尝试以下操作。

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}
"""

或者,您可以尝试在基本映像中创建UID为1000的用户。有关详细信息,请参阅this

相关问题