利用ansible-pull配合放在git上的playbook初始化新安装的操作系统。

最近在两个电脑之间互换硬盘,然后就涉及到分别重装了下双系统,第一台在初始设置的时候稍做了些记录整理到ansible-playbook脚本里面,然后第二台只要连网先安装个ansible再执行下脚本即可,直接上放在git上的脚本(git上usernames用户下prj项目的initdeb.yml):

---
#ansible-pull -U https://github.com/usernames/prj  initdeb.yml
- hosts: localhost
  become: root
  #sudo: yes
  vars:
    inst: [chromium,font-manager,fonts-wqy-microhei,fonts-wqy-zenhei,sshpass,python-lockfile-doc,ipython3,python-netaddr-docs,filezilla,kazam,rdesktop,htop,screen,iptraf,expect,vim,vim-gtk,vim-doc,wget,curl,dstat,w3m,im-switch,fcitx,fcitx-bin,fcitx-libs,fcitx-libs-dev,fcitx-table-wbpy,fcitx-config-common,fcitx-config-gtk,fcitx-data,fcitx-frontend-gtk2,fcitx-frontend-gtk3,fcitx-frontend-qt4,fcitx-frontend-qt5,fcitx-frontend-all,fcitx-modules,fcitx-module-lua,fcitx-module-dbus,fcitx-module-x11,fcitx-table,fcitx-ui-classic]
                                                                                                                                   #   fcitx-module-kimpanel    
  tasks: 
  
  - name: Run the equivalent of "apt-get update" as a separate step
    tags: aptupd
    apt:
      update_cache: yes
    
  - name: InstallSoftware
    tags: aptinst
    apt: name={{ inst }} state=latest

  - name: Update all packages to the latest version
    tags: aptupgrd
    apt:
      upgrade: dist

  - name: Remove dependencies that are no longer required
    tags: aptrm
    apt:
      autoremove: yes
    
  - name: Remove useless packages from the cache
    tags: aptcln
    apt:
      autoclean: yes
    
  # Set sshd not use DNS
  #

  - name: Sshd_Not_Use_DNS
    tags: sshd
    lineinfile: 
      backrefs: yes
      path: /etc/ssh/sshd_config
      regexp: "^.*UseDNS.*$"
      line: "UseDNS no"

  - name: Sshd_Not_Check_Key
    tags: sshd
    lineinfile: 
      backrefs: yes
      path: /etc/ssh/ssh_config
      regexp: "^.*StrictHostKeyChecking.*$"
      line: "StrictHostKeyChecking no"

  - name: Sshd_Service_Restart
    tags: sshd
    service:
     name=sshd
     state=restarted
     enabled=yes

  - name: Get_Sshd_cfg
    tags: sshd
    shell: 
      cat /etc/ssh/sshd_config /etc/ssh/ssh_config | grep -E "UseDNS|StrictHostKeyChecking" 
    register: sshdRst

  - name: Get_Sshd_cfg_Rst
    tags: sshd
    debug: 
      msg: "{{sshdRst.stdout_lines}}"
      
  # Set His Info
  #
  - name: Set_History
    tags: history
    blockinfile: 
      marker: "#{mark} Set History Parameter"
      path: /etc/profile
      insertafter: "EOF"
      block: "export HISTTIMEFORMAT='%F %T '"

  - name: Get_History
    tags: history
    shell: 
      "cat /etc/profile | grep -C1 'HISTTIMEFORMAT' "
    register: profHis

  - name: DebugInfo_Get_profileHIS
    tags: history
    debug: 
      msg: "{{profHis.stdout_lines}}"

  # Set Default Multi-user
  #
  - name: Default_MultiUser
    tags: cmdline
    shell: 
      " systemctl set-default multi-user.target "

  - name: Get_Default_MultiUser
    tags: cmdline
    shell: 
      " systemctl get-default "
    register: syRst

  - name: Get_Selinux_Rst
    tags: cmdline
    debug: 
      msg: "{{syRst.stdout_lines}}"

  # Delete VirNet
  #
  - name: Delete_virnet
    tags: virnet
    shell: 
      " virsh net-destroy default "
    ignore_errors: yes

  - name: Destroy_virnet
    tags: virnet
    shell: 
      " virsh net-undefine default "
    register: virRst
    ignore_errors: yes

  - name: Get_virnet
    tags: virnet
    debug: 
      msg: "{{virRst.stdout_lines}}"
    ignore_errors: yes

顺序大概是先更新一下软件包,然后个性习惯设置:包括ssh、设置命令行历史记录格式、最后是从centos移过来的初始安装带kvm时删除自带的网段可忽略,如有其它个性化需求自行增加即可。

这个管理工具不错呦


服务器资源由ZeptoVM赞助

Partners Wiki Discord