Ansible使用playbook自动化编译安装Nginx(3)

10、在nginx_config目录加入我们经常要增加nginx站点,直接写好模板推送到vhos目录:
[root@cml1roles]# cd nginx_config/
[root@cml1nginx_config]# ls
default  files handlers  meta  tasks templates  vars
[root@cml1nginx_config]# cd templates/
[root@cml1templates]# ls
temp_server.conf
[root@cml1templates]# cat temp_server.conf
server
{
listen 80;
server_name {{server_name }};
index index.phpindex.html;
root {{root_dir }};
}

###在var定义变量:
[root@cml1templates]# cd ../vars/
[root@cml1vars]# cat main.yml
server_name: "www.xiaoluo.com"
root_dir:"/web"

11、写配置nginx的tasks步骤:
[root@cml1nginx_config]# cd tasks/
[root@cml1tasks]# ls
main.yml
[root@cml1tasks]# cat main.yml
- name: createvhosts
  shell: mkdir -p /usr/local/nginx/conf/vhosts/
  tags: create_dir
- name: copyconf file nginx.conf          # 调用templates模块
  template: src=https://www.linuxidc.com/temp_server.confdest=/usr/local/nginx/conf/vhosts/{{ server_name }}.conf
  tags: ngxconf
  notify: reload nginx service
###定义重启触发器:
[root@cml1tasks]# cd ../handlers/
You have newmail in /var/spool/mail/root
[root@cml1handlers]# cat main.yml
- name: reloadnginx service
  shell: /usr/local/nginx/sbin/nginx-t;/usr/local/nginx/sbin/nginx -s reload


测试:
[root@cml1ansible]# ansible-playbook -C nginx.yaml
 
PLAY[192.168.5.104] **********************************************************
 
GATHERING FACTS***************************************************************
ok:[192.168.5.104]
 
TASK:[nginx_install | copy nginx package to remote host] *********************
changed:[192.168.5.104]
 
TASK:[nginx_install | tar nginx] *********************************************
skipping:[192.168.5.104]
ok:[192.168.5.104]
 
TASK:[nginx_install | install pakger] ****************************************
changed: [192.168.5.104]=> (item=openssl-devel,pcre-devel,gcc)
 
TASK:[nginx_install | install nginx] *****************************************
skipping:[192.168.5.104]
ok:[192.168.5.104]
 
TASK:[nginx_install | copy conf file nginx.conf] *****************************
changed:[192.168.5.104]
 
TASK:[nginx_install | copy shell] ********************************************
changed:[192.168.5.104]
 
TASK:[nginx_install | create user nginx] *************************************
skipping:[192.168.5.104]
ok: [192.168.5.104]
 
TASK:[nginx_config | create vhosts] ******************************************
skipping:[192.168.5.104]
ok:[192.168.5.104]
 
TASK:[nginx_config | copy conf file nginx.conf] ******************************
changed:[192.168.5.104]
 
NOTIFIED:[nginx_config | reload nginx service] *******************************
skipping:[192.168.5.104]
ok:[192.168.5.104]
 
PLAY RECAP********************************************************************
192.168.5.104              : ok=6    changed=5  unreachable=0    failed=0[root@cml1 ansible]# ansible-playbook  nginx.yaml
PLAY [192.168.5.104] **********************************************************
GATHERING FACTS ***************************************************************
ok: [192.168.5.104]
TASK: [nginx_install | copy nginx package to remote host] *********************
ok: [192.168.5.104]
TASK: [nginx_install | tar nginx] *********************************************
changed: [192.168.5.104]
TASK: [nginx_install | install pakger] ****************************************
ok: [192.168.5.104] => (item=openssl-devel,pcre-devel,gcc)
TASK: [nginx_install | install nginx] *****************************************
changed: [192.168.5.104]
TASK: [nginx_install | copy conf file nginx.conf] *****************************
ok: [192.168.5.104]
TASK: [nginx_install | copy shell] ********************************************
ok: [192.168.5.104]
TASK: [nginx_install | create user nginx] *************************************
changed: [192.168.5.104]
TASK: [nginx_config | create vhosts] ******************************************
changed: [192.168.5.104]
TASK: [nginx_config | copy conf file nginx.conf] ******************************
ok: [192.168.5.104]
NOTIFIED: [nginx_install | start nginx service] *******************************
changed: [192.168.5.104]
PLAY RECAP ********************************************************************
192.168.5.104              : ok=11  changed=5    unreachable=0    failed=0[root@cml3 ~]# ifconfig
ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.5.104  netmask 255.255.255.0  broadcast 192.168.5.255

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/3a6a64fd3dece1aa67af101697dbb15b.html