首页 > 其他 > 详细

ansible 配置yum源

时间:2021-01-14 10:03:34      阅读:304      评论:0      收藏:0      [点我收藏+]

准备工作

[root@wang yum]# tree .
.
├── ansible.cfg
├── hosts
├── inventory
├── scripts
│   ├── centos7yum.sh
│   └── rhel8yum.sh
└── yum.yml

1 directory, 6 files

[root@wang yum]# ssh-copy-id root@192.168.170.137 #设置免密登入

[root@wang yum]# cat /etc/hosts  #配置清单文件
192.168.170.134 apache
192.168.170.137 cos7

将ansible配置文件f复制到yum目录下

 [root@wang yum]# cp /etc/lamp/inventory

[root@wang yum]# cp /etc/ansible/ansible.cfg .

尝试能否ping通

[root@wang yum]# ansible cos7 -m ping
cos7 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"

查看清单

[root@wang yum]# ansible all --list-hosts
hosts (2):
apache
cos7

  编写剧本

剧本内容

[root@wang yum]# cat yum.yml 
---
- hosts: all
tasks:
- name: get centos7 yum
script: ./scripts/centos7yum.sh
when:
- ansible_facts[distribution] == "CentOS"
- ansible_facts[distribution_major_version] == "7"
- name: get rhel8 yum 
script: ./scripts/rhel8yum.sh
when:
- ansible_facts["distribution"] == "RedHat"
- ansible_facts["distribution_major_version"] == "8"

脚本内容

[root@wang 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@wang yum]# cat scripts/rhel8yum.sh 
#! /bin/bash
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
# 安装yum源包
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
#修改yum文件配置
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@wang yum]# ansible-playbook yum.yml

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [apache]
ok: [cos7]

TASK [get centos7 yum] *********************************************************
skipping: [apache]
changed: [cos7]

TASK [get rhel8 yum] ***********************************************************
skipping: [cos7]
changed: [apache]

PLAY RECAP *********************************************************************
apache : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 
cos7 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0

 

跳过案例、

剧本内容

[root@wang yum]# cat yum.yml 
---
- hosts: all
tasks:
- name: get centos7 yum
script: ./scripts/centos7yum.sh
when:
- ansible_facts[distribution] == "CentOS"
- ansible_facts[distribution_major_version] == "8"  #更改7为8,测试是否跳过
- name: get rhel8 yum 
script: ./scripts/rhel8yum.sh
when:
- ansible_facts["distribution"] == "RedHat"
- ansible_facts["distribution_major_version"] == "7"  #更改8为7,测试是否跳过

成功跳过

root@wang yum]# ansible-playbook  yum.yml

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [apache]
ok: [cos7]

TASK [get centos7 yum] *********************************************************
skipping: [apache]
skipping: [cos7]

TASK [get rhel8 yum] ***********************************************************
skipping: [apache]
skipping: [cos7]

PLAY RECAP *********************************************************************
apache : ok=1 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0 
cos7 : ok=1 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0

 

ansible 配置yum源

原文:https://www.cnblogs.com/wangming/p/14275120.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!