ansible實現服務器批量初始化


通過ansible的playbook實現服務器批量初始化工作,會節省大量時間,提高工作效率

ansible模塊目錄結構

$ tree roles/ roles/ └── base ├── defaults ├── files │ ├── puppet.conf │ ├── yum65.repo │ ├── yum67.repo │ └── yum.repo ├── handlers │ └── main.yml ├── meta ├── tasks │ ├── chkconfig.yml │ ├── hostname.yml │ ├── main.yml │ ├── ntpd.yml │ ├── puppet.yml │ ├── repo.yml │ └── route.yml ├── templates │ ├── hosts.j2 │ └── static-routes.j2 └── vars └── main.yml 

入口文件的site.yml

$ more site.yml 
---
- hosts: all
  remote_user: test
  become: yes
  become_method: sudo
  roles:
        - base

模版文件template

修改主機名
$ more base/templates/hosts.j2 
127.0.0.1   {{ ansible_fqdn }}   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         {{ ansible_fqdn }}   localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.1 puppet.server
 
添加靜態路由,需要重啟網絡
$ more base/templates/static-routes.j2 
any net 10.0.0.0/8 gw {{ gateway }}
any net 172.0.0.0/8 gw {{ gateway }}
any net 192.168.1.0/24 gw {{ gateway }}

可以在base/vars/main.yml中定義變量,由於環境特殊,我在命令行中使用變量。

yml中定義使用變量的格式如下
 
name:value

task中的入口文件

$ more base/tasks/main.yml 
---
- include: ntpd.yml
- include: repo.yml
- include: route.yml
- include: hostname.yml
- include: chkconfig.yml
- include: puppet.yml

時間同步

$ more base/tasks/ntpd.yml 
---
- name: sync datatime
  command: /usr/sbin/ntpdate 202.120.2.101
   
- name: sync hwclock
  command: /sbin/hwclock -w

更具不同系統版本配置yum源

$ more base/tasks/repo.yml 
---
- name: configure RedHat5 yum repo
  copy: force=yes src=yum.repo  dest=/etc/yum.repos.d/rhel-debuginfo.repo owner=root group=root mode=0644
  when: ansible_distribution_major_version == '5'
 
- name: configure RedHat6.5 yum repo
  copy: force=yes src=yum65.repo  dest=/etc/yum.repos.d/rhel-debuginfo.repo owner=root group=root mode=0644
  when: ansible_distribution_version == '6.5'
 
- name: configure RedHat6.7 yum repo
  copy: force=yes src=yum67.repo  dest=/etc/yum.repos.d/rhel-debuginfo.repo owner=root group=root mode=0644
  when: ansible_distribution_version == '6.7'

配置路由

$ more base/tasks/route.yml 
- name: config static route
  template:  force=yes src=static-routes.j2 dest=/etc/sysconfig/static-routes owner=root group=root mode=0644
  notify: restart network

批量配置服務器的hostname(動態inventory腳本實現)

$ more base/tasks/hostname.yml 
---
- name: install facter
  yum: name=facter state=latest
 
- name: install rubygem-json
  yum: name=rubygem-json state=latest
 
- hostname: name={{ hostname }}
 
- name : gather facts again
  setup :
 
- name: config hosts
  template:  force=yes src=hosts.j2 dest=/etc/hosts owner=root group=root mode=0644

關閉iptables,sendmail和selinux

$ more base/tasks/chkconfig.yml 
- name: chkconfig off  iptables
  shell: /sbin/chkconfig iptables off
 
- name: stop iptables
  service: name=iptables state=stopped
 
- name: chkconfig off  sendmail
  shell: /sbin/chkconfig sendmail off
 
- name: stop sendmail
  service:  name=sendmail state=stopped 
   
- name: stop selinux 
  command:  /sbin/setenforce  0

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM