ansible工具遠程執行批量操作:
1、安裝httpd包 2、複製httpd服務配置文件 3、啓動或重啓httpd服務
- hosts: web
remote_user: root
tasks:
- name: install httpd
yum: name=httpd
- name: copy file
copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/ backup=yes
tags: copyconf
notify: restart httpd
- name: start httpd
service: name=httpd state=started enabled=yes
handlers:
- name: restart httpd
service: name=httpd state=restarted
ansible使用模板
templates功能:根據模塊文件動態生成對應的配置文件
templates文件必須存放於templates目錄下,且命名爲 .j2 結尾
yaml/yml 文件需和templates目錄平級,目錄結構如下:
./
├── temnginx.yml
└── templates
└── nginx.conf.j2
tmpnginx.yml(變量優先級由高到低:命令行-à playbook—) 主機清單)
../templates/nginx.conf配置文件做如下參數修改
按照cpu的個數生成進程數量
如果需要根據變量、facts或此前任務的執行結果來做爲某task執行與否的前提時要用到條件測試,通過when語句實現
groups指定用戶的輔助組 迭代調用
hping3控制ping的速度
迭代嵌套子變量迭代嵌套子變量