首页 > 代码库 > ansible实现服务器批量初始化
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 8 directories, 16 files
入口文件的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=http://www.mamicode.com/yum.repo dest=/etc/yum.repos.d/rhel-debuginfo.repo owner=root group=root mode=0644>
执行结果如下:
$ ansible-playbook -i inventory.py site.yml --extra-vars "gateway=‘10.44.245.65‘" PLAY [all] ********************************************************************* TASK [setup] ******************************************************************* ok: [10.44.245.85] TASK [base : sync datatime] **************************************************** changed: [10.44.245.85] TASK [base : sync hwclock] ***************************************************** changed: [10.44.245.85] TASK [base : configure RedHat5 yum repo] *************************************** ok: [10.44.245.85] TASK [base : configure RedHat6.5 yum repo] ************************************* skipping: [10.44.245.85] TASK [base : configure RedHat6.7 yum repo] ************************************* skipping: [10.44.245.85] TASK [base : config static route] ********************************************** ok: [10.44.245.85] TASK [base : install facter] *************************************************** ok: [10.44.245.85] TASK [base : install rubygem-json] ********************************************* ok: [10.44.245.85] TASK [base : hostname] ********************************************************* ok: [10.44.245.85] TASK [base : gather facts again] *********************************************** ok: [10.44.245.85] TASK [base : config hosts] ***************************************************** ok: [10.44.245.85] TASK [base : chkconfig off iptables] ****************************************** changed: [10.44.245.85] TASK [base : stop iptables] **************************************************** ok: [10.44.245.85] TASK [base : chkconfig off sendmail] ****************************************** changed: [10.44.245.85] TASK [base : stop sendmail] **************************************************** ok: [10.44.245.85] TASK [base : install puppet] *************************************************** ok: [10.44.245.85] TASK [base : puppet config file] *********************************************** ok: [10.44.245.85] PLAY RECAP ********************************************************************* 10.44.245.85 : ok=16 changed=4 unreachable=0 failed=0 gather facts again ----------------------------------------------------- 19.88s install puppet ---------------------------------------------------------- 6.99s install rubygem-json ---------------------------------------------------- 5.50s install facter ---------------------------------------------------------- 5.48s stop sendmail ----------------------------------------------------------- 3.51s ------------------------------------------------------------------------ 3.27s configure RedHat5 yum repo ---------------------------------------------- 2.44s sync datatime ----------------------------------------------------------- 2.37s puppet config file ------------------------------------------------------ 2.16s sync hwclock ------------------------------------------------------------ 2.02s Playbook finished: Fri Feb 17 18:11:30 2017, 17 total tasks. 0:01:02 elapsed. 如有不足欢迎大家多多提供宝贵建议本文出自 “Linux之旅” 博客,请务必保留此出处http://openlinuxfly.blog.51cto.com/7120723/1898927
ansible实现服务器批量初始化
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。