1. 程式人生 > >用roles部署nginx

用roles部署nginx

yml service yaml rest conn head sha main host

1、初始化一個role

[root@bogon ~]# ansible-galaxy init /etc/ansible/roles/websrvs

查看已經創建的role
[root@bogon ~]# ls /etc/ansible/roles/
webservs



2、配置role

把初始化後 role裏面沒用的刪除,只留下面四個目錄

[root@bogon ~]# cd /etc/ansible/roles/webservs/
[root@bogon webservs]# ls
handlers  README.md  tasks  templates  vars
[root@bogon webservs]
# ls templates/ index.html.j2 nginx.conf.j2 vars文件內容 [root@bogon webservs]# cat vars/main.yml --- # vars file for /etc/ansible/roles/webservs worker_processes: 4 worker_connections: 768 max_open_files: 65506 tasks 文件內容 [root@bogon webservs]# cat tasks/main.yml --- # tasks file for /etc/ansible/roles/webservs
- name: install nginx command: yum install nginx -y - name: copy nginx config file template: src=/home/lmx/test_ansible/nginx.conf.j2 dest=/etc/nginx/nginx.conf notify: restart nginx - name: copy index.html template: src: /home/lmx/test_ansible/index.html.j2 dest: /usr/share/nginx/www/index.html mode:
0644 notify: restart nginx - name: see file command: ls /root notify: restart nginx handlers 文件內容: [root@bogon webservs]# cat handlers/main.yml --- # handlers file for /etc/ansible/roles/webservs - name: restart nginx service: name=nginx state=restarted 模板文化內容: [root@bogon webservs]# cat templates/nginx.conf.j2 worker_processes {{ worker_processes }}; worker_rlimit_nofile {{ max_open_files }}; events { worker_connections {{ worker_connections }}; } http { server { listen 80; root /usr/share/nginx/www; index index.html index.htm default.html index.php; server_name loclhost; location / { try_files / =404; } } } [root@bogon webservs]# cat templates/index.html.j2 <html> <head> <title>welcome to american</title> </head> <body> <h1>nginx, confitured by ansible</h1> <p>if you can see this, ansible successfully installed nginx.</p> <p>{{ ansible_hostname }}</p> </body> </html> 3、配置playbook,把role添加進來 [root@bogon ~]# cat nginx_role.yaml --- - hosts: webservers become: yes become_method: sudo roles: - role: webservs 4、開始執行Playbook [root@bogon ~]# ansible-playbook nginx_role.yaml

用roles部署nginx