ansible-playbook批量升级centos linux操作系统版本

1. 业务需求

50台centos7.6版本操作系统升级至centos7.8版本

2. ansible-playbook需求拆解

Step1.在/etc/yum.repos.d目录下创建bak目录
Step2.将原OS的yum repo文件备份至bak目录
Step3.创建新的yum repo文件指向centos7.8版本yum源
Step4.执行yum update_cache更新yum源缓存
Step5.执行yum update升级步骤(耗时较长)

3. ansible-playbook编制

---
    - hosts: all
      gather_facts: no
      tasks:
      - name: backup old repo
        file: path=/etc/yum.repos.d/bak state=directory
      - name: mv default repo file to bak dir
        shell: mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak 
      - name: create yum repo
        yum_repository: 
                name: c78yum
                description: centos78 pro
                baseurl: http://10.142.80.12/centos/7.8.2003/x86_64/
                gpgcheck: no
                enabled: 1
      - name: update cache
        yum:
                update_cache: yes
      - name: upgrade all packages
        yum:
                name: '*'
                state: latest

4. 编辑/etc/ansible/hosts文件

[groupname]
10.143.160.138  ansible_ssh_user=root ansible_ssh_pass='password'

5. 执行ansible-playbook升级操作系统版本

#在playbook放置的目录下执行playbook
[root@ansible yml]# ansible-playbook update_os.yml

6. 升级完成后重启目标主机

[root@ansible yml]# ansible all -a 'reboot'

7. 批量检查目标主机是否升级至最新版本

[root@ansible yml]# ansible all -a 'cat /etc/redhat-release'
10.143.160.138 | CHANGED | rc=0 >>
CentOS Linux release 7.8.2003 (Core)

8. 未升级成功的主机登录排查/var/log/yum.log日志文件

猜你喜欢

转载自blog.csdn.net/weixin_43770382/article/details/107384452