Ansible 自动化平台 - 通过 yum 安装“azure-cli”无法查找 python-dnf

问题描述 投票:0回答:1

我正在将剧本从 Ansible Tower 迁移到与 Azure DevOps 交互的 Ansible 自动化平台 4.3。因此,我需要安装 azure-cli 来执行后续命令,但它失败了。

- name: Install the application package (RedHat)
  # package:
  yum:
    name: "{{ azure_package_name }}"
    state: present

注意我已经尝试过 package,然后 yum,但总是收到此错误消息:

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Could not import the dnf python module using /usr/bin/python (3.9.13 (main, Nov  9 2022, 13:16:24) [GCC 8.5.0 20210514 (Red Hat 8.5.0-15)]). Please install python3-dnf or python2-dnf package or ensure you have specified the correct ansible_python_interpreter. (attempted ['/usr/libexec/platform-python', '/usr/bin/python3', '/usr/bin/python2', '/usr/bin/python'])", "results": []}

我知道 AAP 现在在执行环境中运行,我正在使用 Automation Hub 默认执行环境并与 RHEL 系统(控制器节点本身)通信。

我认为我不必操纵 EE,因此我认为任何依赖项都可以安装在剧本本身中。我做错了什么?

尝试将“package”切换为“yum”,失败。 尝试通过 yum 直接将 azure-cli 安装到服务器,这成功了,但我仍然从剧本中收到相同的失败消息。我一直在想我应该如何本地运行“yum install azure-cli”,绕过这个 python 模块的需要。

我希望它能够安装 azure-cli 或检测到它已经安装。

azure ansible azure-cli yum ansible-automation-platform
1个回答
0
投票

您可以直接在

Azure DevOps 管道
中使用 Azure CLI task运行 ansible 命令 或在 ansible playbook 本身中添加 azure-cli 安装命令并在 DevOps 管道中运行它,例如下面:-

我的 ansible 剧本存储在 Azure Repos 中:-

install_azure_cli.yml:-

---
- hosts: localhost
  gather_facts: no
  tasks:
    - name: Install Azure CLI
      become: yes
      become_user: root
      apt:
        name: apt-transport-https
        state: present

    - name: Add Microsoft GPG key
      become: yes
      become_user: root
      apt_key:
        url: https://packages.microsoft.com/keys/microsoft.asc
        state: present

    - name: Determine distribution release
      command: lsb_release -cs
      register: distribution_release
      changed_when: false
      check_mode: no

    - name: Add Azure CLI Repository
      become: yes
      become_user: root
      apt_repository:
        repo: "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ {{ distribution_release.stdout }} main"
        state: present
        filename: azure-cli

    - name: Update apt cache
      become: yes
      become_user: root
      apt:
        update_cache: yes

    - name: Install Azure CLI
      become: yes
      become_user: root
      apt:
        name: azure-cli
        state: latest

我的 Azure DevOps 管道:-

在此 yaml 代码中,我使用 Azure CLI 任务,并通过 python 安装 Ansible 来运行我的 ansible-playbook。您可以根据您的要求使用任何一种方法。

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'

steps:

- task: UsePythonVersion@0
  displayName: 'Install Python'
  inputs:
    versionSpec: '3.9'

- task: AzureCLI@2
  displayName: 'Azure CLI'
  inputs:
    azureSubscription: 'ansible'
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
      echo "##vso[task.setvariable variable=ARM_SUBSCRIPTION_ID]$(az account show --query="id" -o tsv)"
      echo "##vso[task.setvariable variable=ARM_CLIENT_ID]${servicePrincipalId}"
      echo "##vso[task.setvariable variable=ARM_CLIENT_SECRET]${servicePrincipalKey}"
      echo "##vso[task.setvariable variable=ARM_TENANT_ID]${tenantId}"
    addSpnToEnvironment: true
- script: pip install ansible
  displayName: 'Install Ansible'

- script: pip install -r https://raw.githubusercontent.com/ansible-collections/azure/dev/requirements-azure.txt
  displayName: 'Install Azure modules needed'

- script: ansible-galaxy collection install azure.azcollection
  displayName: 'Install Ansible Azure Collection'
  
# - script: ansible-playbook -i inv site.yml
#   displayName: 'Run Ansible Playbook'
#   env:
#     AZURE_CLIENT_ID: $(ARM_CLIENT_ID)
#     AZURE_SECRET: $(ARM_CLIENT_SECRET)
#     AZURE_TENANT: $(ARM_TENANT_ID)
#     AZURE_SUBSCRIPTION_ID: $(ARM_SUBSCRIPTION_ID)

- script: ansible-playbook -i inv install_azure_cli.yml
  displayName: 'Run Ansible Playbook'
  env:
    AZURE_CLIENT_ID: $(ARM_CLIENT_ID)
    AZURE_SECRET: $(ARM_CLIENT_SECRET)
    AZURE_TENANT: $(ARM_TENANT_ID)
    AZURE_SUBSCRIPTION_ID: $(ARM_SUBSCRIPTION_ID)

输出:-

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.