【问题标题】:Importing/adding a yum .repo file using Ansible使用 Ansible 导入/添加 yum .repo 文件
【发布时间】:2018-12-30 08:22:45
【问题描述】:

我正在尝试使用 Ansible 从自定义存储库安装 MariaDB(或任何软件),但我不确定如何使用 yum/yum_repository 模块导入 .repo 文件。

Ansible

这是我的剧本:

-
    hosts: all
    become: true
    remote_user: root
    tasks:
        -
            name: set system timezone
            timezone:
                name: America/Toronto
        -
            name: add custom repository
            yum_repository:
                name: centos_o
                description: custom repositories
                baseurl: http://example.net/mirror/centos_o.repo
        -
            name: ensure mariadb is installed
            yum:
                name: mariadb-server-5.5.*
                state: installed

我已经尝试了所有includemetalinkbaseurlmirrorlist,但没有成功。我也错过了 GPG 关键步骤,但我什至无法正确添加 repo。

centos_o.repo 文件如下所示:

# JENKINS
[jenkins]
name=CentOS-$releasever - JENKINS
baseurl=http://example.net/mirror/jenkins/
enabled=0
gpgcheck=1

# MariaDB 5.5
[mariadb]
name=CentOS-$releasever - MariaDB
baseurl=http://example.net/mirror/mariadb/yum/5.5/centos$releasever-amd64/
enabled=0
gpgcheck=1

# MariaDB 10.0
[mariadb]
name=CentOS-$releasever - MariaDB
baseurl=http://example.net/mirror/mariadb/yum/10.0/centos$releasever-amd64/
enabled=0
gpgcheck=1

外壳

这是我尝试转换为 Ansible 的 shell 脚本版本:

yum clean all
yum-config-manager --add-repo=http://example.net/mirror/centos_o.repo
yum-config-manager --enable mariadb
rpm --import http://example.net/mirror/mariadb/RPM-GPG-KEY-MariaDB

如果有什么不同,我会在 CentOS 机器上使用 Vagrant 的 Ansible local 配置器运行它。

【问题讨论】:

    标签: ansible yum vagrant-provision


    【解决方案1】:

    使用带有creates 标志的shell 命令。如果 repo 文件存在,这将跳过该步骤。您需要确保知道 repo 文件的名称。

    - name: Add CentOS_o repository
      shell: yum-config-manager --add-repo=http://example.net/mirror/centos_o.repo
      args:
        creates: /etc/yum.repos.d/centos_o.repo 
    

    如果您需要向 url 添加任何架构,请使用类似

    - name: Add CentOS_7_speciality repository
      shell: yum-config-manager --add-repo=http://example.net/{{ ansible_distribution | lower }}/{{ ansible_distribution_major_version }}/{{ ansible_architecture }}/
    centos_o.repo
      args:
        creates: /etc/yum.repos.d/centos_o.repo 
    

    Ansible 会将变量替换为

    {{ ansible_distribution | lower }} == centos
    {{ ansible_distribution_major_version }} == 7
    {{ ansible_architecture }} == x86_64
    

    【讨论】:

      【解决方案2】:

      appears 你是对的,他们不提供你所追求的东西。他们的模型是这样的,您可以调用 yum_repository: 3 次,每次使用您在 .repo 文件中已有的每个 baseurl= 值。

      因此,鉴于您的情况,我建议您只使用command: 来运行yum-config-manager --add-repo,就像您在shell 中一样。唯一的问题是 yum-config-manager --add-repo= 不是幂等的,在这种情况下,您必须手动保护 command: 以防止它在每次运行时一遍又一遍地添加相同的 repo 文件。

      【讨论】:

      • 在这种情况下保护命令可以通过检查centos_o.repo文件是否存在来完成,或者有没有更好的方法?
      • 很遗憾我不知道,但大概 yum-config-manager 具有--list-repo 或等效功能,因此您可以看到发生了什么。我没有 CentOS 系统可以尝试一下,可以肯定地说
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-04
      • 1970-01-01
      相关资源
      最近更新 更多