首页 > 其他 > 详细

ansible-playbook配置不同系统yum源(条件语句when)

时间:2021-01-13 23:55:01      阅读:0      评论:0      收藏:0      [点我收藏+]

1.准备工作

  • 操作环境
主机 ip 系统
node0(ansible) 192.168.94.142 rhel8
node4 192.168.94.131 centos7
node5 192.168.94.140 rhel8
  • yum任务目录结构
[[email protected] yum]# tree .
.
├── ansible.cfg
├── inventory
├── scripts
│   ├── centos7yum.sh
│   └── rhel8yum.sh
└── yum.yml

1.1配置清单

  • 映射ip
[[email protected] yum]# vim /etc/hosts 

...
192.168.94.131 node4
192.168.94.140 node5

  • 复制ansible配置文件到yum目录下
[[email protected] yum]# cp /etc/ansible/ansible.cfg .
[[email protected] yum]# vim ansible.cfg 


[defaults]
...
# some basic default values...

inventory      = ./inventory  #设定清单在当前目录下

  • 创建清单文件
[[email protected] yum]# cat inventory 

[yum]
node4
node5

  • 创建公钥
[[email protected] yum]# ssh-keygen -t rsa

  • 复制公钥到受控机
[[email protected] yum]# ssh-copy-id [email protected]
[[email protected] yum]# ssh-copy-id [email protected]

  • 查看清单
[[email protected] yum]# ansible all --list-hosts
  hosts (2):
    node4
    node5

  • 测试连接
[[email protected] 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"
}

2.yum剧本配置

2.1编写yum剧本

[[email protected] 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"

2.2编写配置yum脚本

  • centos7yum配置
[[email protected] 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

  • rhel8yum配置
[[email protected] 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

3.运行验证

  • 运行剧本
[[email protected] 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

[[email protected] ~]# 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

  • node5
[[email protected] ~]# 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

(0)
(0)
   
举报
评论 一句话评论(0
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号