ansible 自动编译部署 nginx详解
来源:互联网 发布:淘宝图片空间授权管理 编辑:程序博客网 时间:2024/05/29 07:03
一、ansible简介及测试环境
名称hostnameIPansiblenode4192.168.40.131clientnode5192.168.40.132本次测试只是用一台client做测试。但!!!!ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
(1)、连接插件connection plugins:负责和被监控端实现通信;
(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
(3)、各种模块核心模块、command模块、自定义模块;
(4)、借助于插件完成记录日志邮件等功能;
(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。
所以在批量部署时,只需要在hosts文件加入client主机IP即可。
二、安装ansible
因为我的Linux的是CentOS Linux release 7.3.1611 (Core),使用的ali仓库源。
我在yum安装的ansible是
[root@node4 ~]# ansible --versionansible 2.3.2.0 config file = /etc/ansible/ansible.cfg configured module search path = Default w/o overrides python version = 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
由于要使用playbook roles模板自动安装nginx,但该版本存在着BUG,所以只能降到ansible 1.9版本(经过亲测,可以使用)。
所以使用python-pip 安装 ansible1.9版本
[root@node4 ~]# ansible --versionansible 1.9.1 configured module search path = None
具体做法在我的博客,链接如下:
点击打开链接
三、编写nginx的roles模板
先看看所需要的文件
[root@node4 ansible]# pwd/etc/ansible[root@node4 ansible]# tree.├── ansible.cfg #核心配置文件├── hosts #主机清单├── nginx_install.yaml #playbook编译安装nginx文件└── roles └── nginx_install ├── default ├── files │ └── nginx-1.12.0.tar.gz #编译安装nginx的安装包,提前下载至此 ├── handlers │ └── main.yml #触发配置文件 ├── meta #元数据 ├── tasks │ └── main.yml #定义要调用的nginx的主逻辑文件 ├── templates │ ├── nginx.conf #定义nginx的配置文件 │ └── nginx.service #定义nginx的启动脚本(systemctl) └── vars └── main.yml #定义变量配置文件9 directories, 9 files
先创建所需要的目录(必须一样),而且上面配置文件必须以main.yml命名。
必须设置ansible到各主机之间的ssh免秘钥互信。
命令如下
ssh-keygenssh-copy-id node5 #主机名或IP
mkdir -p roles/nginx_instll/{handlers,files,meta,tasks,templates,vars,default}
1、配置主要逻辑文件:
[root@node4 ansible]# cat roles/nginx_install/tasks/main.yml - name: copy nginx package to remote host copy: src=nginx-1.12.0.tar.gz dest=/usr/local/src/nginx-1.12.0.tar.gz#调用files模块(复制nginx-1.12.0.tar.gz至远端) tags: cppkg- name: tar nginx shell: cd /usr/local/src;tar -xf nginx-1.12.0.tar.gz- name: install pakger yum: name={{item}} state=latest#使用变量解决依赖包的问题 with_items: - openssl-devel - pcre-devel - gcc tags: dpdpkg- name: useradd nginx shell: useradd nginx tags: add_nginx- name: install nginx shell: cd /usr/local/src/nginx-1.12.0;./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre;make && make install tags: inngx- name: copy start config template: src=nginx.service dest=//usr/lib/systemd/system/nginx.service#调用templates模块 notify: start nginx- name: mkdir vhosts shell: mkdir /usr/local/nginx/vhosts- name: copy config file nginx.conf template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf notify: reload nginx service
2、将nginx-1.12.0.tar.gz复制到/etc/ansible/roles/nginx_install/files目录下
3、配置templates下的文件。
[root@node4 nginx_install]# ls templates/nginx.conf nginx.service
该目录下的nginx.conf将推送到client主机。
[root@node4 templates]# cat nginx.conf #user nobody;worker_processes {{ ansible_processor_vcpus }};#使用ansible的setup指令自动传入#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen {{ ngxport }} ;#使用vars下自定义的变量 server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root {{ root_dir }};#使用vars下自定义的变量 index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} include vhosts/*.conf;#创建虚拟主机的包含文件,方便下次批量管理}
该目录下的nginx.service将推送到client主机。该启动脚本采用systemctl启动。
[root@node4 nginx_install]# cat templates/nginx.service [Unit]Description=nginxAfter=network.target[Service]Type=forkingExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.confExecStart=/usr/local/nginx/sbin/nginxExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s stopPrivateTmp=true[Install]WantedBy=multi-user.target
4、配置vars下的文件
在nginx.conf的配置文件的变量部分可以选择利用ansible调用系统,例如cup核心数:
[root@node4 nginx_install]# ansible 192.168.40.132 -m setup -a "filter=ansible_processor_vcpus"192.168.40.132 | success >> { "ansible_facts": { "ansible_processor_vcpus": 1 }, "changed": false}所以nginx.conf中定义的
{{ ansible_processor_vcpus }}是可以自动化转入参数。
nginx.conf中其他剩下的变量属于自定义,调用vars的配置文件:
[root@node4 nginx_install]# cat vars/main.yml ngxport: "80"#监听端口server_name: "node5.aizhen.com"#访问域名root_dir: "/web"#web主目录5、配置handlers下的文件
在主逻辑文件中:
- name: copy start config template: src=nginx.service dest=//usr/lib/systemd/system/nginx.service notify: start nginx #触发到handlers的文件
handlers的配置文件
[root@node4 nginx_install]# cat handlers/main.yml - name: start nginx #这里的名字必须与主逻辑文件匹配,否则触发不了 service: name=nginx state=started- name: reload nginx service service: name=nginx state=restarted四、测试
1、检测语法,看看是否正确。
[root@node4 ansible]# ansible-playbook --syntax-chec /etc/ansible/nginx_install.yaml playbook: /etc/ansible/nginx_install.yaml确定没有报错。
2、使用tags,copy nginx的安装包到client主机,防止出错。
[root@node4 ansible]# ansible-playbook -t cppkg /etc/ansible/nginx_install.yaml PLAY [192.168.40.132] ********************************************************* GATHERING FACTS *************************************************************** ok: [192.168.40.132]TASK: [nginx_install | copy nginx package to remote host] ********************* changed: [192.168.40.132]PLAY RECAP ******************************************************************** 192.168.40.132 : ok=2 changed=1 unreachable=0 failed=0查看client
[root@node5 ~]# cd /usr/local/src/[root@node5 src]# lsnginx-1.12.0.tar.gz测试安装nginx
[root@node4 templates]# ansible-playbook /etc/ansible/nginx_install.yaml PLAY [192.168.40.132] ********************************************************* GATHERING FACTS *************************************************************** ok: [192.168.40.132]TASK: [nginx_install | copy nginx package to remote host] ********************* ok: [192.168.40.132]TASK: [nginx_install | tar nginx] ********************************************* changed: [192.168.40.132]TASK: [nginx_install | install pakger] **************************************** ok: [192.168.40.132] => (item=openssl-devel,pcre-devel,gcc)TASK: [nginx_install | useradd nginx] ***************************************** changed: [192.168.40.132]TASK: [nginx_install | install nginx] ***************************************** changed: [192.168.40.132]TASK: [nginx_install | copy start config] ************************************* changed: [192.168.40.132]TASK: [nginx_install | mkdir vhosts] ****************************************** changed: [192.168.40.132]TASK: [nginx_install | copy config file nginx.conf] *************************** changed: [192.168.40.132]NOTIFIED: [nginx_install | start nginx] *************************************** changed: [192.168.40.132]NOTIFIED: [nginx_install | reload nginx service] ****************************** changed: [192.168.40.132]PLAY RECAP ******************************************************************** 192.168.40.132 : ok=11 changed=8 unreachable=0 failed=0
查看client的80端口是否启动
root@node5 ~]# netstat -ntlpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11503/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 917/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1009/master tcp6 0 0 :::111 :::* LISTEN 1/systemd tcp6 0 0 :::22 :::* LISTEN 917/sshd tcp6 0 0 ::1:25 :::* LISTEN 1009/master这就就完成部署啦。
五、测试出现报错
如果使用-C测试的话,会出现报错
[root@node4 templates]# ansible-playbook -C /etc/ansible/nginx_install.yaml PLAY [192.168.40.132] ********************************************************* GATHERING FACTS *************************************************************** ok: [192.168.40.132]TASK: [nginx_install | copy nginx package to remote host] ********************* ok: [192.168.40.132]TASK: [nginx_install | tar nginx] ********************************************* skipping: [192.168.40.132]####这里代表跳过shell的测试,因为第一次使用,无法检测。ok: [192.168.40.132]日志报错如下:
Oct 25 2017 08:35:05 - SKIPPED - ...Oct 25 2017 08:35:05 - OK - {"module_name": "shell", "module_args": "cd /usr/local/src;tar -xf nginx-1.12.0.tar.gz"} => {"msg": "check mode not supported for shell", "skipped": true}
这个问题也不说是报错,只是ansible识别不了而已。
因为主逻辑文件中使用了shell。只要直接推送即可。- ansible 自动编译部署 nginx详解
- 使用Ansible部署Nginx
- ansible自动部署网站
- ansible-playbook批量部署nginx
- jenkins+ansible+maven自动部署
- Ansible一键部署Flask(nginx+Uwsgi)
- Ansible安装部署及常用模块详解
- Ansible安装部署及常用模块详解
- Ansible部署
- 自动运维工具ansible详解
- SVN+Nginx自动部署脚本
- Nginx编译部署环境安装
- nginx 编译参数详解
- Nginx编译参数详解
- Nginx编译参数详解
- Nginx编译参数详解
- nginx 编译参数详解
- nginx 编译参数详解
- dpkg命令
- 阿里巴巴Druid连接池配置
- 算法设计之补零递归法(统计数字问题)
- 11. Container With Most Water
- 【JZOJ5417】方阵
- ansible 自动编译部署 nginx详解
- 基础练习 回文数
- 第8周项目3(1)对称矩阵压缩存储的实现与应用
- AGC010
- 跨域下使用获取iframe的父页面URL
- Angular-JS 与 Angular4 初接触
- Java-Io之文件File
- 【第八周项目5】计数的模式匹配
- 第13章 程序的动态加载和执行(一,引导)