簡單部署小遊戲和交做業頁面
環境
主機 |
外網ip |
內網ip |
web01 |
10.0.0.7 |
172.16.1.7 |
web02 |
10.0.0.8 |
172.16.1.8 |
虛擬主機配置
server {
listen {{gm_port}};
server_name {{gm_com}};
location / {
root {{gm_site_dir}};
index index.html;
}
}
建立角色目錄
[root@m01 roles]# ansible-galaxy init games
編輯tasks目錄
1.編輯虛擬主機配置
[root@m01 games]# vim tasks/copy.yml
- name: copy games.conf.j2
template:
src: "games.conf.j2"
dest: "/etc/nginx/conf.d/games.conf"
notify:
- "reload nginx"
2.建立站點目錄
[root@m01 games]# vim tasks/dir.yml
- name: create {{gm_site_dir}}
file:
path: "{{gm_site_dir}}"
state: directory
owner: "{{ww_w}}"
group: "{{ww_w}}"
recurse: yes
3.解壓軟件包
[root@m01 games]# vim tasks/unarchive.yml
- name: jieya "{{gm_packages}}"
unarchive:
src: "{{gm_packages}}"
dest: "{{gm_site_dir}}"
owner: "{{ww_w}}"
group: "{{ww_w}}"
4.include
[root@m01 games]# vim tasks/main.yml
- include: copy.yml
- include: dir.yml
- include: unarchive.yml
5.上傳軟件包
[root@m01 files]# ll
total 18324
-rw-r--r-- 1 root root 18704885 Jun 23 14:25 games.tar.gz
-rw-r--r-- 1 root root 53845 Jun 23 14:34 zuoye.tar.gz
6.觸發器
[root@m01 games]# vim handlers/main.yml
- name: reload nginx
systemd:
name: nginx
state: reloaded
7.編輯虛擬主機
[root@m01 games]# vim templates/games.conf.j2
server {
listen {{gm_port}};
server_name {{gm_com}};
location / {
root {{gm_site_dir}};
index index.html;
}
}
8.編輯變量
#--------------------------------------------小遊戲 做業頁面等
##小遊戲
#gm_port: "80"
#gm_com: "games.com"
#gm_site_dir: "/code/games/"
##包名
#gm_packages: "games.tar.gz"
#做業
gm_port: "80"
gm_com: "zy.com"
gm_site_dir: "/code/zy/"
#包名
gm_packages: "zuoye.tar.gz"
編輯入口文件
[root@m01 roles]# vim site.yml
#- { role: rsync_client,when: ansible_fqdn is match 'web*' }
#- { role: rsync_client,when: ansible_fqdn is match 'nfs*' }
#- { role: rsync_server,when: ansible_fqdn is match 'backup*' }
#- { role: nfs_server,when: ansible_fqdn is match 'nfs*' }
#- { role: nfs_client,when: ansible_fqdn is match 'web*' }
#- { role: mount_server,when: ansible_fqdn is match 'nfs*' }
#- { role: mount_client,when: ansible_fqdn is match 'web*' }
#- { role: sersync,when: ansible_fqdn is match 'nfs' }
- { role: nginx_web,when: ansible_fqdn is match 'web*' }
#- { role: nginx_lb,when: ansible_fqdn is match 'lb*' }
#- { role: keepalived_lb,when: ansible_fqdn is match 'lb*' }
#- { role: lb_ssl,when: ansible_fqdn is match 'lb*' }
#- { role: php,when: ansible_fqdn is match 'web*' }
#- { role: mariadb,when: ansible_fqdn is match 'nfs*' }
#- { role: wordpress,when: ansible_fqdn is match 'web*' }
#- { role: wecenter,when: ansible_fqdn is match 'web*' }
#- { role: mysql_master,when: ansible_fqdn is match 'db01' }
#- { role: mysql_slave,when: ansible_fqdn is match 'db02' }
#- { role: zabbix_server,when: ansible_fqdn is match 'nfs' }
#- { role: zabbix_client,when: ansible_fqdn is match 'web02' }
#- { role: jumpserver,when: ansible_fqdn is match 'jumpserver' }
- { role: games,when: ansible_fqdn is match 'web*' }
執行
[root@m01 roles]# ansible-playbook site.yml
#解析,瀏覽器訪問
注意
1.這套代碼只適合經過nginx簡單的部署一些服務
上傳軟件包後,經過修改變量運行ansible便可實現項目的部署