Ansible playbook 报错 “Found existing installation: requests 2.25.1\n\n:stderr:ERROR: Cannot uninstall requests 2.25.1” 的原因是什么?

82次阅读
没有评论

问题描述

在运行一个安装docker和docker-compose并在ec2 t2.micro上运行compose文件的ansible playbook时,遇到了一个报错。他需要在ec2上安装docker和docker-compose的python模块,以便在ansible中使用docker模块。
以下是他的playbook:

- name: Update yum repo, install docker, pip, docker-compose & pip and python modules
  hosts: all
  become: true
  tasks:
    - name: update yum repo and cache
      yum:
        name: '*'
        state: latest
        update_cache: true
    - name: install docker and pip
      yum:
        name:
          - docker
          - pip
    - name: download and install docker-compose
      get_url:
        url: https://github.com/docker/compose/releases/latest/download/docker-compose-Linux-{{lookup('pipe','uname -m')}}
        dest: /usr/local/bin/docker-compose
        mode: +x
    - name: start docker daemon
      systemd:
        name: docker
        state: started
########################################################### this is the trouble part
    - name: Install docker and docker-compose python module
      pip:
        name:
          - docker
          - docker-compose
############################################################

playbook的其余部分将ec2-user添加到docker组,重置ec2连接,将docker-compose文件复制到服务器并执行它,这些都正常工作。
用户尝试了安装python3并在ansible.cfg文件中选择”interpreter_python= /usr/bin/python3″,以及使用默认的python安装路径/usr/bin/python3.9,但playbook运行时遇到了以下错误:

"Found existing installation: requests 2.25.1\n\n:stderr:ERROR: Cannot uninstall requests 2.25.1 Hint: The package was installed by rpm."

但是,如果用户手动登录到ec2并执行以下命令,模块就会成功安装:

$ pip install docker
$ pip install docker-compose

如果将问题代码注释掉并运行playbook,它将成功执行。用户希望能够一键运行此playbook,并将其集成到terraform文件中,因此无法手动执行这一步骤。

解决方案

请注意以下操作注意版本差异及修改前做好备份。

方案1

报错信息显示已经存在requests 2.25.1的安装包,但无法卸载。这可能是由于使用rpm安装的原因。为了解决这个问题,可以尝试在安装docker和docker-compose之前,先卸载requests 2.25.1。
以下是修改后的playbook代码:

- name: Update yum repo, install docker, pip, docker-compose & pip and python modules
  hosts: all
  become: true
  tasks:
    - name: update yum repo and cache
      yum:
        name: '*'
        state: latest
        update_cache: true
    - name: uninstall requests 2.25.1
      pip:
        name: requests==2.25.1
        state: absent
    - name: install docker and pip
      yum:
        name:
          - docker
          - pip
    - name: download and install docker-compose
      get_url:
        url: https://github.com/docker/compose/releases/latest/download/docker-compose-Linux-{{lookup('pipe','uname -m')}}
        dest: /usr/local/bin/docker-compose
        mode: +x
    - name: start docker daemon
      systemd:
        name: docker
        state: started
    - name: Install docker and docker-compose python module
      pip:
        name:
          - docker
          - docker-compose

在上面的代码中,我们添加了一个新的任务来卸载requests 2.25.1。通过使用pip模块并指定state: absent,我们可以确保在安装docker和docker-compose之前卸载该包。

方案2

如果方案1无法解决问题,可以尝试使用--ignore-installed选项来安装docker和docker-compose的python模块,以忽略已安装的requests 2.25.1。
以下是修改后的playbook代码:

- name: Update yum repo, install docker, pip, docker-compose & pip and python modules
  hosts: all
  become: true
  tasks:
    - name: update yum repo and cache
      yum:
        name: '*'
        state: latest
        update_cache: true
    - name: install docker and pip
      yum:
        name:
          - docker
          - pip
    - name: download and install docker-compose
      get_url:
        url: https://github.com/docker/compose/releases/latest/download/docker-compose-Linux-{{lookup('pipe','uname -m')}}
        dest: /usr/local/bin/docker-compose
        mode: +x
    - name: start docker daemon
      systemd:
        name: docker
        state: started
    - name: Install docker and docker-compose python module
      pip:
        name:
          - docker
          - docker-compose
        extra_args: --ignore-installed

在上面的代码中,我们在安装docker和docker-compose的python模块时,添加了extra_args: --ignore-installed选项。这将忽略已安装的requests 2.25.1,并继续安装docker和docker-compose的模块。
请根据您的实际情况选择适合您的解决方案,并在运行playbook之前做好备份。

正文完