ansible 搭建游戏、剧本、动静分离
1、setup模块
获取主机信息
ansible web01 -m setup -a "filter=ansible_all_ipv4_addresses"
常用参数
ansible_all_ipv4_addresses:仅显示ipv4的信息。
ansible_devices:仅显示磁盘设备信息。
ansible_distribution:显示是什么系统,例:centos,suse等。
ansible_distribution_major_version:显示是系统主版本。
ansible_distribution_version:仅显示系统版本。
ansible_machine:显示系统类型,例:32位,还是64位。
ansible_eth0:仅显示eth0的信息。
ansible_hostname:仅显示主机名(不准确)
ansible_fqdn:仅显示主机名。
ansible_kernel:仅显示内核版本。
ansible_lvm:显示lvm相关信息。
ansible_memtotal_mb:显示系统总内存。
ansible_memfree_mb:显示可用系统内存。
ansible_memory_mb:详细显示内存情况。
ansible_swaptotal_mb:显示总的swap内存。
ansible_swapfree_mb:显示swap内存的可用内存。
ansible_mounts:显示系统磁盘挂载情况。
ansible_processor:显示cpu个数(具体显示每个cpu的型号)。
ansible_processor_vcpus:显示cpu个数(只显示总的个数)。
2、利用ansible搭建一个小游戏案例
1、准备好三台web服务器
2、安装Nginx
ansible web -m yum -a "name=nginx state=installed"
3、建立站点
1、创建目录
ansible web -m shell -a "chdir=/opt creates=/opt/game mkdir /opt/game"
2、编写配置文件
[root@master game]# cat game.conf
server {
listen 80;
server_name game.test.com;
location / {
root /opt/game;
index index.html;
}
}
3、上传配置文件
ansible web -m copy -a "src=game.conf dest=/etc/nginx/conf.d/"
4、上传代码包
1、上传代码
ansible web -m unarchive -a "src=mario.zip dest=/opt/ "
ansible web -m shell -a "chdir=/opt/html5-mario mv ./* /opt/game/"
2、修改权限
ansible web -m shell -a "chown -R www.www /opt/game"
3、重启Nginx
ansible web -m systemd -a "name=nginx state=restarted"
5、配置负载均衡,高可用
1、编写配置文件并安装Nginx
ansible lb -m yum -a "name=nginx state=installed"
2、上传配置文件
[root@master game]# cat lb-game.conf
upstream game {
server 172.16.1.7;
server 172.16.1.8;
server 172.16.1.9;
}
server {
listen 80;
server_name game.test.com;
location / {
proxy_pass http://game;
include proxy_params;
}
}
ansible lb -m copy -a "src=lb-game.conf dest=/etc/nginx/conf.d/"
3、重启Nginx
ansible lb -m systemd -a "name=nginx state=restarted"
4、安装keepalived
ansible lb -m yum -a "name=keepalived state=installed"
5、准备keepalived的配置文件
[root@lb01 conf.d]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id hostname
}
vrrp_script check {
script "/etc/keepalived/check_web.sh"
interval 5
}
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.230.100
}
track_script {
check
}
}
ansible lb -m copy -a "src=keepalived.conf dest=/etc/keepalived/"
ansible lb -m copy -a "src=check_web.sh dest=/etc/keepalived/ mode=0755"
ansible lb01 -m shell -a "chdir=/etc/keepalived sed -i 's#hostname#lb01#g' /etc/keepalived/keepalived.conf"
ansible lb02 -m shell -a "chdir=/etc/keepalived sed -i 's#hostname#lb02#g' /etc/keepalived/keepalived.conf"
ansible lb02 -m shell -a "chdir=/etc/keepalived sed -i 's#priority 100#priority 90#g' /etc/keepalived/keepalived.conf"
6、重启keepalived
ansible lb -m systemd -a "name=keepalived state=restarted"