playbooks 是一种简单的配置管理系统与多机器部署系统的基础。与现有的其他系统有不同之处,且非常适合于复杂应用部署
playbook 可以定制配置,可以按指定的步骤有序执行,支持同步以及异步方式。
官网例子:https://github.com/ansible/ansible-examples
playbooks 可以用于声明配置,更强大的地方在于,在playbooks中可以编排有序的执行过程,甚至于做到多组机器间,来回有序的执行特别指定的步骤,并且可以同步或异步发起任务。
ansible-playbook命令参数:
-u REMOTE_USER, --user=REMOTE_USER # ssh 连接的用户名
-k, --ask-pass #ssh登录认证密码 -s, --sudo #sudo 到root用户,相当于Linux系统下的sudo命令
-U SUDO_USER, --sudo-user=SUDO_USER #sudo 到对应的用户
-K, --ask-sudo-pass #用户的密码(—sudo时使用)
-T TIMEOUT, --timeout=TIMEOUT # ssh 连接超时,默认 10 秒
-C, --check # 指定该参数后,执行 playbook 文件不会真正去执行,而是模拟执行一遍,然后输出本次执行会对远程主机造成的修改
-e EXTRA_VARS, --extra-vars=EXTRA_VARS # 设置额外的变量如:key=value 形式 或者 YAML or JSON,以空格分隔变量,或用多个-e
-f FORKS, --forks=FORKS # 进程并发处理,默认 5
-i INVENTORY, --inventory-file=INVENTORY # 指定 hosts 文件路径,默认 default=/etc/ansible/hosts
-l SUBSET, --limit=SUBSET # 指定一个 pattern,对- hosts:匹配到的主机再过滤一次
--list-hosts # 只打印有哪些主机会执行这个 playbook 文件,不是实际执行该 playbook
--list-tasks # 列出该 playbook 中会被执行的 task
--private-key=PRIVATE_KEY_FILE # 私钥路径
--step # 同一时间只执行一个 task,每个 task 执行前都会提示确认一遍
--syntax-check # 只检测 playbook 文件语法是否有问题,不会执行该 playbook
-t TAGS, --tags=TAGS #当 play 和 task 的 tag 为该参数指定的值时才执行,多个 tag 以逗号分隔
--skip-tags=SKIP_TAGS # 当 play 和 task 的 tag 不匹配该参数指定的值时,才执行
-v, --verbose #输出更详细的执行过程信息,-vvv可得到所有执行过程信息。
实例:
1[root@localhost ~]# tree /etc/ansible/ 2/etc/ansible/ 3├── ansible.cfg 4├── group_vars 5│ ├── all 6│ └── t3 7├── hosts 8├── roles 9│ └── nginx 10│ ├── handlers 11│ │ └── main.yml 12│ ├── tasks 13│ │ └── main.yml 14│ └── templates 15│ ├── default_proxy_params.conf 16│ ├── new.conf 17│ ├── nginx.conf 18│ ├── static_proxy_params.conf 19│ ├── upstream.conf 20│ ├── vhost.conf 21│ ├── vhost_ssl.conf 22│ └── websocket_proxy_params.conf 23├── site.retry 24└── site.yml 25 26[root@localhost ~]# cat /etc/ansible/hosts 27[all:vars] 28ansible_ssh_private_key_file=/root/.ssh/id_rsa 29ansible_ssh_port=22 30ansible_ssh_user=root 31 32[t3:vars] 33ansible_python_interpreter=/usr/bin/python2 34 35[t3] 36192.168.11.162 37 38[root@localhost ~]# cat /etc/ansible/site.yml 39- hosts: t3 # 组名 40 user: root 41 roles: 42 - nginx # 角色 43 44[root@localhost ~]# cat /etc/ansible/group_vars/t3 # t3为组名 45worker_processes: 4 46num_cpus: 4 47max_open_file: 65506 48worker_connections: 10240 49log_format_format: 'json' #日志类型,默认为main 50log_format_main: '$remote_addr - $remote_user [$time_local] $request "$status" $body_bytes_sent 51"$http_referer" "$request_body" "$http_user_agent" "$http_x_forwarded_for" 52cache_status:$upstream_cache_status upstream:$upstream_addr response_time: $request_time 53response_time: $request_time host: $host' 54 55log_format_json: '{"client_ip":"$remote_addr","ident":"-","auth":"$remote_user", 56"timestamp":"$time_local","request":"$request","response":"$status", 57"bytes":"$body_bytes_sent","referer":"$http_referer","request_body":"$request_body", 58"user_agent":"$http_user_agent","forwarded":"$http_x_forwarded_for", 59"cache_status":"$upstream_cache_status","upstream":"$upstream_addr", 60"upstream_status":"$upstream_status","http_host":"$host","ssl_protocol":"$ssl_protocol", 61"ssl_cipher":"$ssl_cipher","request_time":"$request_time", 62"upstream_response_time":"$upstream_response_time"}' 63 64vhost_domain: ["t1.bet","t2.com","t3.tv"] # 域名列表 65 66 67upstream_list: [ # upstream 列表 68 { 69 "name" : "mobile", # 名称 70 "server_list": [ # 服务列表 71 {"ip":"10.0.0.1","port" : 3000,"max_fails":2,"fail_timeout":"30s","weight":5}, 72 {"ip":"10.0.0.2","port" : 3000,"max_fails":2,"fail_timeout":"30s","weight":15}, 73 {"ip":"10.0.0.3","port" : 3000,"max_fails":2,"fail_timeout":"30s","weight":10}, 74 {"ip":"10.0.0.4","port" : 3000,"max_fails":2,"fail_timeout":"30s","weight":5} 75 ] 76 }, 77 { 78 "name":"desktop", 79 "server_list":[ 80 {"ip":"10.0.0.4","port" : 3001,"max_fails":2,"fail_timeout":"30s","weight":1}, 81 {"ip":"10.0.0.3","port" : 3001,"max_fails":2,"fail_timeout":"30s","weight":1}, 82 ] 83 } 84] 85 86[root@localhost ~]# cat /etc/ansible/roles/nginx/tasks/main.yml 87- name: nginx is at then latest version # 安装nginx 88 yum: pkg=nginx state=latest 89 90- name: write the nginx.conf config file # nginx.conf 模板文件 91 template: src=nginx.conf dest=/etc/nginx/nginx.conf 92 notify: 93 - restart nginx 94- name: write the default_proxy_params.conf config file 95 template: src=default_proxy_params.conf dest=/etc/nginx/conf.d/default_proxy_params.conf 96 notify: 97 - restart nginx 98 99- name: write the default_proxy_params.conf config file 100 template: src=new.conf dest=/etc/nginx/conf.d/new.conf 101 notify: 102 - restart nginx 103 104- name: write the static_proxy_params.conf config file 105 template: src=static_proxy_params.conf dest=/etc/nginx/conf.d/static_proxy_params.conf 106 notify: 107 - restart nginx 108 109- name: write the websocket_proxy_params.conf config file 110 template: src=websocket_proxy_params.conf dest=/etc/nginx/conf.d/websocket_proxy_params.conf 111 notify: 112 - restart nginx 113 114- name: write the upstream.conf config file 115 template: src=upstream.conf dest=/etc/nginx/conf.d/upstream.conf 116 notify: 117 - restart nginx 118 119- name: write the vhost.conf config file 120 template: src=vhost.conf dest=/etc/nginx/conf.d/vhost.conf 121 notify: 122 - restart nginx 123 124- name: write the vhost_ssl.conf config file 125 template: src=vhost_ssl.conf dest=/etc/nginx/conf.d/vhost_ssl.conf 126 notify: 127 - restart nginx 128 129- name: ensure nginx is running 130 service: name=nginx state=started 131 132[root@localhost ~]# cat /etc/ansible/roles/nginx/handlers/main.yml 133- name: restart nginx 134 service: name=nginx state=started 135 136[root@localhost ~]# cat /etc/ansible/roles/nginx/templates/nginx.conf 137worker_processes {{ worker_processes }}; 138pid /var/run/nginx.pid; 139{% if num_cpus == 2 %} 140worker_cpu_affinity 01 10; 141{% elif num_cpus == 4 %} 142worker_cpu_affinity 1000 0100 0010 0001; 143{% elif num_cpus >=8 %} 144worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; 145{% else %} 146worker_cpu_affinity 1000 0100 0010 0001; 147{% endif %} 148 149worker_rlimit_nofile {{ max_open_file }} 150 151events { 152 use epoll; 153 worker_connections {{ worker_connections }}; 154 multi_accept on; 155} 156... 157# 日志格式配置 158{% if log_format_format == 'json' %} 159 log_format json {{ log_format_json }}; 160 {% else %} 161 log_format main {{ log_format_main }}; 162{% endif %} 163 164[root@localhost ~]# cat /etc/ansible/roles/nginx/templates/vhost.conf 165{% for domain in vhost_domain %} 166server { 167 listen 80 ; 168 server_name {{ domain }}; 169 rewrite ^(.*) https://www{{ domain }} permanent; 170 {% if log_format_format == 'json' %} 171 access_log logs/{{ domain }}.access.log json; 172 {% else %} 173 access_log logs/{{ domain }}.access.log main; 174 {% endif %} 175} 176{% endfor %} 177 178[root@localhost ~]# cat /etc/ansible/roles/nginx/templates/vhost_ssl.conf 179{% for domain in vhost_domain %} 180server { 181 listen 443;#HTTP Port 182 server_name www.{{ domain }} {{ domain }}; 183 include /usr/local/nginx/conf.d/new.conf; 184 index index.jsp index.html index.htm; 185 {% if log_format_format == 'json' %} 186 access_log logs/{{ domain }}.access.log json; 187 {% else %} 188 access_log logs/{{ domain }}.access.log main; 189 {% endif %} 190 191 if ($http_host = {{ domain }} ) { 192 rewrite ^(.*)$ https://www.{{ domain }}$1 permanent; } 193 ssl on; 194 ssl_certificate /usr/local/nginx/conf.d/ssl/www.{{ domain }}/www.{{ domain }}.crt; 195 ssl_certificate_key /usr/local/nginx/conf.d/ssl/www.{{ domain }}/www.{{ domain }}.key; 196} 197{% endfor %} 198 199[root@localhost ~]# cat /etc/ansible/roles/nginx/templates/upstream.conf 200{% for upstream_name in upstream_list %} 201upstream {{ upstream_name.name }} { 202 {% for server_name in upstream_name.server_list%} 203 server {{ server_name.ip }}:{{ server_name.port }} max_fails={{ server_name.max_fails }} fail_timeout={{ server_name.fail_timeout }} weight={{ server_name.weight}}; 204 {% endfor %} 205} 206{% endfor %} 207... 208 209[root@localhost ~]# ansible-playbook /etc/ansible/site.yml 210PLAY [t3] *********************************************************** 211 212TASK [Gathering Facts] ********************************************** 213ok: [192.168.11.162] 214 215TASK [nginx : nginx is at then latest version] ********************** 216ok: [192.168.11.162] 217...