使用Ansible导入/添加yum .repo文件

15

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

Ansible

这是我的playbook:

-
    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密钥步骤,但是我甚至无法正确添加仓库。

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

Shell

这是我正在尝试转换为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 provisioner运行此操作。

2个回答

25

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

- 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

10

看起来你是正确的,他们没有提供你需要的东西。他们的模型是这样的,你需要三次调用yum_repository:,每次使用你已经在.repo文件中有的baseurl=值之一。

因此,在你的情况下,我建议只需使用command:运行yum-config-manager --add-repo,就像在shell中一样。唯一需要注意的是,如果yum-config-manager --add-repo=不是幂等的,那么你必须手动保护该command:,以防止它在每次运行时重复添加相同的repo文件。


在这种情况下,保护命令可以通过检查centos_o.repo文件的存在来完成,还有更好的方法吗? - rink.attendant.6
不幸的是我不知道,但是可以假设 yum-config-manager 有一个 --list-repo 或类似的功能,这样你就可以用这种方式查看情况了。我没有 CentOS 系统来尝试,因此不能确定。 - mdaniel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接