一、要做什麼先把思維導圖畫出來
二、接下來我們開始根據自己的思維去寫東西
```
(base) [root@bogon ansible]# cat site.yaml
- hosts: ceshi
roles:- nginx
- nginx
(base) [root@bogon nginx]# cat files/index.html
user ansible install nginx
(base) [root@bogon nginx]# cat handlers/main.yaml
---
- name: restart nginx
service: name=nginx state=restarted
(base) [root@bogon nginx]# cat tasks/main.yaml
---
- name: install nginx packages
yum: name={{ item }} state=latest
with_items:
- epel-release
- nginx
- name: copy nginx.conf template
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx
- name: copy index.html
copy: src=index.html dest=/usr/share/nginx/html/index.html
- name: make sure nginx service running
service: name=nginx state=started
(base) [root@bogon nginx]# cat templates/nginx.conf.j2
在隨便一個地方複製粘貼過來nginx.conf一份即可修改名字爲nginx.conf.j2,修改nginx.conf.j2文件中的worker_connectios定義爲變量 如下
events {
worker_connections {{worker_connections}};
}
(base) [root@bogon nginx]# cat vars/main.yaml
worker_connections: 10240
然後執行:
這裏的主機在hosts文件定義:hosts文件和入口文件平級,然後在瀏覽器訪問
nginx驗證成功
三、問題1:如果安裝的時候碰到這個問題
不要亂百度去了下邊就是解決這個問題的關鍵,上邊提示install nginx packages錯誤那就是執行with_items安裝的時候出錯了,在目標服務器安裝一下源就好了yum install epel-release
到此問題解決接下來在執行一遍就可以安裝成功。
自學ansible跟着我: 拎壺沖沖衝