首页 > 解决方案 > Ubuntu 中的 Ansible Playbook 问题

问题描述

我正在尝试创建一个 Ansible 剧本。下面是我创建的剧本的代码。

---
- name: "Install Packages"
  hosts: localhost
  connection: local
  become: yes
  tasks:

  - name: Fix problems with packages
    shell: sudo dpkg --configure -a

  - name: Run apt-get install
    shell: sudo apt-get install -f

  - name: Clean & Update
    shell: sudo apt-get clean && sudo apt-get update

  - name: Upgrade
    shell: sudo apt-get upgrade -y

  - name: Install packages
    apt:
      name:
      - python3-pip
      - apache2
      - libapache2-mod-wsgi-py3
      - mysql-server
      - libmysqlclient-dev
      - apt-transport-https
      - erlang-base
      - erlang-asn1
      - erlang-crypto
      - erlang-eldap
      - erlang-ftp
      - erlang-inets
      - erlang-mnesia
      - erlang-os-mon
      - erlang-parsetools
      - erlang-public-key
      - erlang-runtime-tools
      - erlang-snmp
      - erlang-ssl
      - erlang-syntax-tools
      - erlang-tftp
      - erlang-tools
      - erlang-xmerl
      - rabbitmq-server
      state: latest
      cache_valid_time: 3600 

在运行这个剧本时,我收到下面提到的错误 -

    [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
    
    PLAY [Install Packages] *********************************************************************************************************************************************************************
    
    TASK [Gathering Facts] **********************************************************************************************************************************************************************
    ok: [localhost]
    
    TASK [Fix problems with packages] ***********************************************************************************************************************************************************
    [WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo
    changed: [localhost]
    
    TASK [Run apt-get install] ******************************************************************************************************************************************************************
    changed: [localhost]
    
    TASK [Clean & Update] ***********************************************************************************************************************************************************************
    changed: [localhost]
    
    TASK [Upgrade] ******************************************************************************************************************************************************************************
    changed: [localhost]
    
    TASK [Install packages] *********************************************************************************************************************************************************************
    fatal: [localhost]: FAILED! => {"cache_update_time": 1597472721, "cache_updated": false, "changed": false, "msg": "'/usr/bin/apt-get -y -o \"Dpkg::Options::=--force-confdef\" -o \"Dpkg::Options::=--force-confold\"      install 'python3-pip' 'apache2' 'libapache2-mod-wsgi-py3' 'mysql-server' 'libmysqlclient-dev' 'apt-transport-https' 'erlang-base' 'erlang-asn1' 'erlang-crypto' 'erlang-eldap' 'erlang-ftp' 'erlang-inets' 'erlang-mnesia' 'erlang-os-mon' 'erlang-parsetools' 'erlang-public-key' 'erlang-runtime-tools' 'erlang-snmp' 'erlang-ssl' 'erlang-syntax-tools' 'erlang-tftp' 'erlang-tools' 'erlang-xmerl' 'rabbitmq-server'' failed: E: Unable to correct problems, you have held broken packages.\n", "rc": 100, "stderr": "E: Unable to correct problems, you have held broken packages.\n", "stderr_lines": ["E: Unable to correct problems, you have held broken packages."], "stdout": "Reading package lists...\nBuilding dependency tree...\nReading state information...\nSome packages could not be installed. This may mean that you have\nrequested an impossible situation or if you are using the unstable\ndistribution that some required packages have not yet been created\nor been moved out of Incoming.\nThe following information may help to resolve the situation:\n\nThe following packages have unmet dependencies:\n erlang-ftp : Depends: erlang-base (= 1:22.0.7+dfsg-1build1~ubuntu18.04.1) but 1:20.2.2+dfsg-1ubuntu2 is to be installed or\n                       erlang-base-hipe (= 1:22.0.7+dfsg-1build1~ubuntu18.04.1) but it is not going to be installed\n              Depends: erlang-runtime-tools (= 1:22.0.7+dfsg-1build1~ubuntu18.04.1) but 1:20.2.2+dfsg-1ubuntu2 is to be installed\n              Depends: erlang-ssl (= 1:22.0.7+dfsg-1build1~ubuntu18.04.1) but 1:20.2.2+dfsg-1ubuntu2 is to be installed\n erlang-tftp : Depends: erlang-base (= 1:22.0.7+dfsg-1build1~ubuntu18.04.1) but 1:20.2.2+dfsg-1ubuntu2 is to be installed or\n                        erlang-base-hipe (= 1:22.0.7+dfsg-1build1~ubuntu18.04.1) but it is not going to be installed\n", "stdout_lines": ["Reading package lists...", "Building dependency tree...", "Reading state information...", "Some packages could not be installed. This may mean that you have", "requested an impossible situation or if you are using the unstable", "distribution that some required packages have not yet been created", "or been moved out of Incoming.", "The following information may help to resolve the situation:", "", "The following packages have unmet dependencies:", " erlang-ftp : Depends: erlang-base (= 1:22.0.7+dfsg-1build1~ubuntu18.04.1) but 1:20.2.2+dfsg-1ubuntu2 is to be installed or", "                       erlang-base-hipe (= 1:22.0.7+dfsg-1build1~ubuntu18.04.1) but it is not going to be installed", "              Depends: erlang-runtime-tools (= 1:22.0.7+dfsg-1build1~ubuntu18.04.1) but 1:20.2.2+dfsg-1ubuntu2 is to be installed", "              Depends: erlang-ssl (= 1:22.0.7+dfsg-1build1~ubuntu18.04.1) but 1:20.2.2+dfsg-1ubuntu2 is to be installed", " erlang-tftp : Depends: erlang-base (= 1:22.0.7+dfsg-1build1~ubuntu18.04.1) but 1:20.2.2+dfsg-1ubuntu2 is to be installed or", "                        erlang-base-hipe (= 1:22.0.7+dfsg-1build1~ubuntu18.04.1) but it is not going to be installed"]}
    
    PLAY RECAP **********************************************************************************************************************************************************************************
    localhost                  : ok=5    changed=4    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

> 致命:[本地主机]:失败!=> {"cache_update_time": 1597472721, cache_updated": false, "changed": false, "msg": "'/usr/bin/apt-get -y -o \"Dpkg::Options::=--force -confdef\" -o \"Dpkg::Options::=--force-confold\" install 'python3-pip' 'apache2' 'libapache2-mod-wsgi-py3' 'mysql-server' 'libmysqlclient-dev' 'apt-transport-https' 'erlang-base' 'erlang-asn1' 'erlang-crypto' 'erlang-eldap' 'erlang-ftp' 'erlang-inets' 'erlang-mnesia' 'erlang-os-mon' ' erlang-parsetools''erlang-public-key''erlang-runtime-tools''erlang-snmp''erlang-ssl''erlang-syntax-tools''erlang-tftp''erlang-tools'' erlang-xmerl' 'rabbitmq-server'' 失败:E: 无法纠正问题,你持有损坏的包。\n", "rc": 100, "stderr": "E: 无法纠正问题,你持有损坏的包。\n", "stderr_lines": ["E: 无法纠正问题,您持有损坏的包。"], "stdout": "正在读取包列表...\n正在构建依赖关系树...\n正在读取状态信息...\n无法安装某些软件包。这可能意味着您\n请求了一个不可能的情况,或者如果您使用的是不稳定的\n分发,一些必需的包尚未创建\n也没有被移出 Incoming。\n以下信息可能有助于解决这种情况:\n \n以下软件包具有未满足的依赖关系:\n erlang-ftp : Depends: erlang-base (= 1:22.0. 某些软件包无法安装。这可能意味着您已经”、“请求了一个不可能的情况或者如果您使用的是不稳定的”、“尚未创建某些必需包的分发”、“或已移出 Incoming。”、“以下信息可能帮助解决这种情况:"、""、"以下软件包具有未满足的依赖关系:"、" erlang-ftp : 依赖:erlang-base (= 1:22.0.7+dfsg-1build1~ubuntu18.04.1) 但 1: 20.2.2+dfsg-1ubuntu2 要安装或", " erlang-base-hipe (= 1:22.0.7+dfsg-1build1~ubuntu18.04.1) 但不会安装", " 取决于: erlang -runtime-tools (= 1:22.0.7+dfsg-1build1~ubuntu18.04.1) 但要安装 1:20.2.2+dfsg-1ubuntu2", "

无法理解如何解决此问题。我将 Ubuntu 作为 Hyper-V 来宾运行。我是否需要将“elang”部分与 apt 部分分开并尝试使用 apt-get 运行?

我的操作系统版本 - Ubuntu 18.04.3 LTS


更新:

经过几次谷歌搜索,我发现我试图以错误的方式为 Ubuntu 18.04.3 LTS 安装 RabbitMQ Server & erlang。因此,我修改了我的 Ansible Playbook,如下所示。在此处发布更新的剧本代码,以防将来有人需要。

---
- name: "Install Packages"
  hosts: localhost
  connection: local
  become: yes
  tasks:

  - name: Install basic packages
    apt:
      name:
      - vim
      - curl
      - python3-pip
      - apache2
      - libapache2-mod-wsgi-py3
      - mysql-server
      - libmysqlclient-dev
      state: latest
      cache_valid_time: 3600

  - name: Curl for RabbitMQ
    shell: curl -fsSL https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc | sudo apt-key add -

  - name: Add an apt key by id from a keyserver
    apt_key:
      keyserver: hkps://keys.openpgp.org
      id: 0A9AF2115F4687BD29803A206B73A36E6026DFCA

  - name: Install transport http
    apt:
      name:
      - apt-transport-https
      state: latest
      cache_valid_time: 3600

  - name: Insert/Update /etc/apt/sources.list.d/bintray.erlang.list file
    blockinfile:
      path: /etc/apt/sources.list.d/bintray.erlang.list
      block: |
        # >>> Bionic version is for ubuntu 18.04
        # This repository provides Erlang packages produced by the RabbitMQ team
        # See below for supported distribution and component values
        deb https://dl.bintray.com/rabbitmq-erlang/debian bionic erlang
      create: yes

  - name: Update apt-get repo and cache
    apt: update_cache=yes force_apt_get=yes cache_valid_time=3600

  - name: Install erlang packages
    apt:
      name:
      - erlang-base
      - erlang-asn1
      - erlang-crypto
      - erlang-eldap
      - erlang-ftp
      - erlang-inets
      - erlang-mnesia
      - erlang-os-mon
      - erlang-parsetools
      - erlang-public-key
      - erlang-runtime-tools
      - erlang-snmp
      - erlang-ssl
      - erlang-syntax-tools
      - erlang-tftp
      - erlang-tools
      - erlang-xmerl
      state: latest
      cache_valid_time: 3600

  - shell: echo "deb https://dl.bintray.com/rabbitmq/debian bionic main" | sudo tee /etc/apt/sources.list.d/bintray.rabbitmq.list
    register: echoout
  - debug: msg="the echo was {{ echoout.stdout }}"

  - name: Update apt-get repo and cache
    apt: update_cache=yes force_apt_get=yes cache_valid_time=3600

  - name: Install RabbitMQ Server
    apt:
      name:
      - rabbitmq-server
      state: latest
      cache_valid_time: 3600

  - name: start & enable RabbitMQ Server
    service:
      name=rabbitmq-server
      state=started
      enabled=yes

  - name: checking RabbitMQ Server status
    command: systemctl status "{{ item }}"
    with_items:
    - rabbitmq-server
    register: result
    ignore_errors: yes

  - name: showing RabbitMQ Server status
    debug:
     var: result

感谢您的帮助和支持。将状态标记为已解决。

标签: ubuntuansible

解决方案


failed: E: 无法纠正问题,你持有损坏的包。\n", "rc": 100

A: 看来您已经标记为存放破损的包裹。列出“搁置”的包裹

shell> sudo apt-mark showhold

并取消对破损包裹的设置保留。如果您将任何包裹“搁置”,请确保您知道自己在做什么

shell> sudo apt-mark unhold {package}

现在,手动尝试使用 playbook 中的 shell 命令来修复、解决、更新和升级。当打包从命令行再次运行时,请尝试 Ansible。


可以通过 Ansible 模块dpkg_selections选项管理选择:.install, hold, deinstall, purge


推荐阅读