Ansible角色:更换CentOS源

Ansible角色:更换CentOS源

CentOS7和CentOS批量换源角色
系统版本自动检测,自动备份
内含恢复剧本
被控主机需要联网


一、版本要求

被控节点:CentOS7 or CentOS8
控制节点:Ansible2.9


二、如何使用

1、进入Ansible默认角色目录

cd /etc/ansible/roles

如果在ansible.cfg中更改了默认的角色目录,根据你自己角色目录进行更改


2、创建一个角色

ansible-galaxy init change_source

3、下载

change_source.tar.gz


4、解压

tar zxvf change_source.tar.gz

5、主机清单

vim /etc/ansible/hosts

ansible_ssh_host 填写被控主机地址
ansible_ssh_user 填写被控主机用户
ansible_ssh_pass 填写被控主机登录密码

[CentOS]
host_A ansible_ssh_host= 

[CentOS:vars]
ansible_ssh_user= 
ansible_ssh_pass= 
ansible_ssh_port= 22

三、实例剧本

1、主剧本

---
- hosts: CentOS
  remote_user: root
  roles:
    - change_source

2、恢复源剧本

recovery.yml

---
  #备份恢复剧本,在角色中不自动执行,如需恢复备份则需要手动执行此剧本
  - hosts: CentOS
    remote_user: root
    tasks:
    #引入找到源文件路径任务
    - include_tasks: find_source_file
    #备份文件恢复任务
    - name: recovery source 
      shell: mv {{sflie.bk}} {{sfile_url}} 
    #清除缓存&创建缓存
    - name: update_cache
      yum:
        update_cache:
    #升级软件包
    - name: upgrade
      yum:
        name: "*"
        state: latest
    #备份恢复
    - name: recovery state
      debug:
        msg: " recovery successful "

四、角色详解

1、角色目录

defaults
  └─main.yml  
files
handlers
meta
tasks
  ├─backup.yml
  ├─change.yml
  ├─find_sfile.yml
  └─main.yml
templates
tests
  ├─Recovery.yml
  └─do.yml
vars


2、任务

主任务
main.yml

# tasks file for change_source
#剧本执行顺序
- include: find_sfile.yml
- include: backup.yml
- include: change.yml

找到源文件任务
find_sfile.yml

---
  #寻找源文件路径任务
  - name: find the source file path
    find:
      paths: "{{ sfile_path }}"
      patterns: "*Base*.repo"
    register: resource_path
    
  #寻找源文件路径任务状态输出  
  - name: file state
    debug:
      msg:
      - " source file was found "
      - " source file is local at #{{sfile}}# "

备份源文件任务
backup.yml

---
  #备份源文件任务
  - name: Backup the source file
    shell: cp {{sfile}} {{sfile_bk}}
    
  #备份源文件任务完成情况输出
  - name: Backup state
    debug:
      msg: "File is backed up to #{{sfile_bk}}# "

更换源文件任务
change.yml

---
  #创建临时文件夹任务
  - name: Create temp folder
    file:
      path: /etc/tmp
      state: directory

  #下载源文件任务
  - name: Download sourcefile
    get_url: 
      url: "{{ sfile_url }}"
      dest: "{{ new_sfile }}"

  #替换源文件任务
  - name: Set sourcefile
    shell: cp {{new_sfile}} {{sfile}}

  #清除缓存&创建缓存任务
  - name: update_cache
    yum:
      update_cache: 

  #升级安装包任务
  - name: upgrade
    yum:
      name: "*"
      state: latest
      
  #替换源情况输出任务
  - name: Report of change the source
    debug:
      msg: 
      - "The source changed"
      - "New source from #{{sfile_url}}#"

3、变量

---
#剧本定义变量

#关联文件find_sfile.yml 源文件所在路径
sfile: "{{resource_path.files[0].path}}" 
#备份源文件路径
sfile_bk: /etc/yum.repos.d/sourcefile.repo.backup 
#源文件所在目录
sfile_path: /etc/yum.repos.d
#源文件下载地址
sfile_url : https://mirrors.aliyun.com/repo/Centos-{{ansible_distribution_major_version}}.repo 
#源文件下载后存放路径
new_sfile: /etc/temp/Source_file.repo

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值