[root@ansible roles]# cat web.yml - hosts: web remote_user: root tasks: - name: create file file: name=/tmp/new.file state=touch - name: create new user user: name=test2 system=yes shell=/sbin/nologin - name: install package yum: name=httpd state=installed - name: copy html copy: src=/root/index.html dest=/var/www/html/index.html - name: start service service: name=httpd state=started enabled=yes
添加httpd index文件
[root@ansible ~]# cat index.html
123html
測試訪問mysql
[root@ansible ~]# curl 10.0.0.8
123nginx
ansible web -m ping --limit 10.0.0.7
# 只在web組的 10.0.0.7上執行ping
[root@ansible roles]# ansible-playbook web.yml --list-tasksweb
playbook: web.ymlredis
play #1 (web): web TAGS: []
tasks:
create file TAGS: []
create new user TAGS: []
install package TAGS: []
copy html TAGS: []
start service TAGS: []sql
"web.yml" 20L, 546C 12,7 All - hosts: web remote_user: root tasks: - name: install package yum: name=httpd state=installed - name: copy html copy: src=file/httpd.conf dest=/etc/httpd/conf/ backup=yes notify: restart httpd - name: start httpd service: name=httpd state=started enabled=yes handlers: - name: restart httpd service: name=httpd state=restarted enabled=yes
過濾變量 -setup -a 'ilter =變量'shell
[root@ansible roles]# ansible web -m setup -a 'filter=*addr' 10.0.0.8 | SUCCESS => { "ansible_facts": {}, "changed": false } #支持 通配符 *號 [root@ansible roles]# ansible web -m setup -a 'filter=*ipv4' 10.0.0.8 | SUCCESS => { "ansible_facts": { "ansible_default_ipv4": { "address": "10.0.0.8", "alias": "eth0", "broadcast": "10.0.0.255", "gateway": "10.0.0.2", "interface": "eth0", "macaddress": "00:0c:29:24:58:ed", "mtu": 1500, "netmask": "255.255.255.0", "network": "10.0.0.0", "type": "ether" } }, "changed": false }
獲取到cpu的核心數.根據服務器的核心數設置nginx worker進行的數量 ,經過remplate.j2 模板服務器
ansible web -m setup | grep process "ansible_processor": [ "ansible_processor_cores": 1, "ansible_processor_count": 2, "ansible_processor_threads_per_core": 1, "ansible_processor_vcpus": 2,
[root@ansible roles]# cat templa.yml - hosts: web remote_user: root tasks: - name: install nginx yum: name=nginx state=installed - name: copy conf template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf notify: restart ngx tags: restart ngx - name: start nginx service: name=nginx state=started handlers: - name: restart ngx service: name=nginx state=restarted
[root@ansible roles]# cat templates/nginx.conf.j2 worker_processes {{ ansible_processor_vcpus **2 }}; #**2表示cpu核心的2次方 *2 是2的倍數, 好比服務器核心是2個2次方就是4個,nginx重啓後 worker進程是4個,
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen {{ http_port }};
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}app
[root@ansible roles]# cat line.yml - hosts: web remote_user: root tasks: - name: install package yum: name={{ pkname }} state=installed - name: start httpd service: name={{ pkname }} state=started enabled=yes
命令行中 -e 調用playbook 中的變量 變量優先級 1curl
ansible-playbook -e 'pkname=redis' -C app.yml
- hosts: web remote_user: root vars: - pkg1: httpd - pkg2: mysql tasks: - name: install package yum: name={{ pkg1 }} state=installed - name: install package yum: name={{ pkg2 }} state=installed
寫把變量寫到var.yml文件裏
[root@ansible roles]# cat var.yml var1: httpd var2: vsftpd
寫劇本
[root@ansible roles]# cat testvar.yml - hosts: web remote_user: root var_files: #用個名字引用外面定義的變量 - vars.yml: tast: - name: install pkg yum: name={{ var1 }} #安裝httpd - name: create file file: name=/tmp/{{ var2 }}.log state=touch #在tmp下建立vsftpd日誌文件
執行結果就是安裝 httpd 和mysql
[web]
10.0.0.52 http_port=81 #只對 52主機生效
10.0.0.53 http_port=82 #只對 53生效
模版文件配置 結合模板把nginx配置文件複製到主機,nginx會啓動主機清單裏的端口
cat templates/nginx.conf.j2
worker_processes 1;
events {
worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen {{ http_port }}; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
劇本文件
[root@ansible roles]# cat templa.yml
- hosts: web remote_user: root tasks: - name: install nginx yum: name=nginx state=installed - name: copy conf template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf notify: restart ngx tags: restart ngx - name: start nginx service: name=nginx state=started handlers: - name: restart ngx service: name=nginx state=restarted
好比web組裏有1000個主機,要寫端口爲2020,就得寫1000個,下面給web組設置個變量寫好端口
省的重複在web裏寫http_prot=2020
[web] 10.0.0.51 10.0.0.52 10.0.0.53
[web:vars] http_prot=2020 對web組的全部主機生效
還能夠命令行指定變量 優先級最高
ansible-playbook -e " httpd_port=99"t empla.yml