主机 | ip | 系统 |
---|---|---|
node0(ansible) | 192.168.94.142 | rhel8 |
node4 | 192.168.94.131 | centos7 |
node5 | 192.168.94.140 | rhel8 |
[root@node0 yum]# tree .
.
├── ansible.cfg
├── inventory
├── scripts
│ ├── centos7yum.sh
│ └── rhel8yum.sh
└── yum.yml
[root@node0 yum]# vim /etc/hosts
...
192.168.94.131 node4
192.168.94.140 node5
[root@node0 yum]# cp /etc/ansible/ansible.cfg .
[root@node0 yum]# vim ansible.cfg
[defaults]
...
# some basic default values...
inventory = ./inventory #设定清单在当前目录下
[root@node0 yum]# cat inventory
[yum]
node4
node5
[root@node0 yum]# ssh-keygen -t rsa
[root@node0 yum]# ssh-copy-id root@node4
[root@node0 yum]# ssh-copy-id root@node5
[root@node0 yum]# ansible all --list-hosts
hosts (2):
node4
node5
[root@node0 yum]# ansible yum -m ping
node4 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
node5 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
[root@node0 yum]# cat yum.yml
---
- name: yum config
hosts: yum
gather_facts: yes
tasks:
#判断distribution是否为CentOS,主版本是否为7。二者均满足则配置centos7源,否则跳过
- name: get centos7 yum
script: ./scripts/centos7yum.sh
when:
- ansible_facts["distribution"] == "CentOS"
- ansible_facts["distribution_major_version"] == "7"
#判断distribution是否为RedHat,主版本是否为8。二者均满足则配置rhel8源,否则跳过
- name: get rhel8 yum
script: ./scripts/rhel8yum.sh
when:
- ansible_facts["distribution"] == "RedHat"
- ansible_facts["distribution_major_version"] == "8"
[root@node0 yum]# cat scripts/centos7yum.sh
#! /bin/bash
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all && yum makecache
[root@node0 yum]# cat scripts/rhel8yum.sh
#! /bin/bash
#yumconfig
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
#epel config
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
sed -i ‘s|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|‘ /etc/yum.repos.d/epel*
sed -i ‘s|^metalink|#metalink|‘ /etc/yum.repos.d/epel*
#makecache
yum clean all && yum makecache
[root@node0 yum]# ansible-playbook yum.yml
PLAY [yum config] ***************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************
ok: [node4]
ok: [node5]
TASK [get centos7 yum] **********************************************************************************************
skipping: [node5]
changed: [node4]
TASK [get rhel8 yum] ************************************************************************************************
skipping: [node4]
changed: [node5]
PLAY RECAP **********************************************************************************************************
node4 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
node5 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
node4
[root@node4 ~]# yum repolist all
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
repo id repo name status
base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com enabled: 10,072
centosplus/7/x86_64 CentOS-7 - Plus - mirrors.aliyun.com disabled
contrib/7/x86_64 CentOS-7 - Contrib - mirrors.aliyun.com disabled
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 enabled: 13,492
epel-debuginfo/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 - Debug disabled
epel-source Extra Packages for Enterprise Linux 7 - x86_64 - Source disabled
extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com enabled: 448
updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com enabled: 1,155
repolist: 25,167
[root@node5 ~]# yum repolist all
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
repo id repo name status
AppStream CentOS-8 - AppStream - mirrors.aliyun.com enabled
PowerTools CentOS-8 - PowerTools - mirrors.aliyun.com disabled
base CentOS-8 - Base - mirrors.aliyun.com enabled
centosplus CentOS-8 - Plus - mirrors.aliyun.com disabled
extras CentOS-8 - Extras - mirrors.aliyun.com enabled
ansible-playbook配置不同系统yum源(条件语句when)
原文:https://www.cnblogs.com/fangxinxin/p/14274890.html