In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Install Nginx with playbook
Idea: first compile and install nginx on a machine, package it, and then use ansible to distribute it
# cd / etc/ansible enter the ansible configuration file directory # mkdir nginx_install to create a nginx_install directory to facilitate the management of # cd nginx_install# mkdir-p roles/ {common,install} / {handlers,files,meta,tasks,templates,vars}
Description: there are two roles in the roles directory, common for some preparation operations and install for installing nginx operations. There are several directories under each role, and under handlers are the actions to be performed when a change occurs, which is usually used to change the configuration file and restart the service. Files for the installation of some files (files copied to the other machine), meta for the description of information such as role dependence (can be left blank), tasks is the core configuration files, templates usually save some configuration files, startup scripts and other template files, vars for the definition of variables.
You need to prepare the files used for the installation in advance, as follows:
1. Compile and install nginx in advance on a machine, configure the startup script and configure the configuration file.
2. After installation, we need to package the nginx directory and put it under / etc/ansible/nginx_install/roles/install/files/. The name is nginx.tar.gz.
3. Startup scripts and configuration files should be placed under / etc/ansible/nginx_install/roles/install/templates
Nginx, startup script, nginx configuration file.
# ls / usr/local/nginx/client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_ temp [root @ fuxi01 nginx_install] # ls / etc/init.d/nginx / etc/init.d/nginx [root@fuxi01 nginx_install] # ls / usr/local/nginx/conf/nginx.conf/usr/local/nginx/conf/nginx.conf compiled nginx, startup script, configuration file. Then package: # cd / usr/local/ [root@fuxi01 local] # tar czvf nginx.tar.gz-- exclude "nginx.conf"-- exclude "vhost" nginx/ filter out the nginx.conf and vhost directories. [root@fuxi01 local] # mv nginx.tar.gz / etc/ansible/nginx_install/roles/install/files/ move to this directory [root@fuxi01 local] # cp nginx/conf/nginx.conf / etc/ansible/nginx_install/roles/install/templates/ [root @ fuxi01 local] # cp / etc/init.d/nginx / etc/ansible/nginx_install/roles/install/templates/
Defining the tasks,nginx of common requires some dependency packages, using the yum module, installing the zlib and pcre packages.
# cd / etc/ansible/nginx_install/roles# vim. / common/tasks/main.yml / / is as follows-name: Install initializtion require software yum: name= {{item}} state=installed with_items:-zlib-devel-pcre-devel
Define variable
Why put it in the template?
Because variables are supported in the template.
At deployment time, different values can be defined depending on the machine environment. For example, the first machine defines www, and the second machine defines Apache. On the left is the variable name, and on the right is the variable value (www).
# vim / etc/ansible/nginx_install/roles/install/vars/main.yml / / contains the following nginx_user: wwwnginx_port: 80nginx_basedir: / usr/local/nginx
First, copy all the documents used to the target machine.
# vim / etc/ansible/nginx_install/roles/install/tasks/copy.yml / / is as follows-name: Copy Nginx Software copy: src=nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root- name: Uncompression Nginx Software shell: tar zxf / tmp/nginx.tar.gz-C / usr/local/- name: Copy Nginx Start Script template: src=nginx dest=/etc/init.d/nginx owner=root group=root mode=0755- name: Copy Nginx Config template: src=nginx.conf dest= {{nginx_basedir}} / conf/ owner=root group=root mode=0644
The nginx.tar.gz here does not write the absolute path, because the copy module will look in the install/files directory by default, and the template module will look under templates.
Next, the user is created, the service is started, and the package is deleted. This is a total yml. )
# vim / etc/ansible/nginx_install/roles/install/tasks/install.yml / / is as follows-name: Create Nginx User user: name= {{nginx_user}} state=present createhome=no shell=/sbin/nologin- name: Start Nginx Service shell: / etc/init.d/nginx start- name: Add Boot Start Nginx Service shell: chkconfig-- level 345 nginx on- name: Delete Nginx compression files shell: rm-rf / tmp/nginx.tar.gz
Description:
Module user, variable nginx_user, defined in install/vars/main.yml, present already exists, whether createhome creates the user's home directory.
Create a user, start the nginx service, boot up the nginx service, and delete the nginx compression package.
Then create main.yml and call copy and install
# vim / etc/ansible/nginx_install/roles/install/tasks/main.yml / / is as follows-include: copy.yml- include: install.yml
At this point, the two roles:common and install are defined, and then you need to define an entry profile.
# vim / etc/ansible/nginx_install/install.yml / / is as follows-hosts: testhost remote_user: root gather_facts: True roles:-common-install
Execute: ansible-playbook / etc/ansible/nginx_install/install.yml
Before performing this step, make sure that nginx is not installed on the other party's machine. After the execution is successful, it is correct to check the nginx process on the other party's machine:
[root@yw03 ~] # ps aux | grep nginxroot 1127 0.0 45912 1284? Ss November 21 0:00 nginx: master process / usr/local/nginx/sbin/nginx-c / usr/local/nginx/conf/nginx.confnobody 1128 0.00.2 48404 4168? S November 21 0:43 nginx: worker processnobody 1129 0.00.2 48404 3912? S November 21 0:00 nginx: worker processroot 8061 0.0 112732 972 pts/0 S + 22:02 0:00 grep-color=auto nginx
Idea figure 1:
Idea figure 2:
II. Playbook management configuration file
Most of the time in a production environment, configuration files need to be managed, and installation packages are only used when initializing the environment. Below is a playbook that manages the nginx configuration file.
# mkdir-p / etc/ansible/nginx_config/roles/ {new,old} / {files,handlers,vars,tasks}
New is used for updating, old is used for rollback, nginx.conf and vhosts directories are under files, and handlers is the command to restart nginx service.
With regard to rollback, it is wrong that the configuration file needs to be rolled back to the original configuration file, and then the corresponding service is loaded. It is necessary to back up the old configuration before performing playbook, so the management of the old configuration file must be strict, never modify the configuration of the online machine casually, and ensure that the configuration under the new/files is consistent with the online configuration.
First put the nginx.conf and vhosts directories under the files directory
# cd / usr/local/nginx/conf/# cp-r nginx.conf vhost / etc/ansible/nginx_config/roles/new/files/# vim / etc/ansible/nginx_config/roles/new/vars/main.yml / / define the variable nginx_basedir: / usr/local/nginx# vim / etc/ansible/nginx_config/roles/new/handlers/main.yml / / define to reload the nginx service-name: restart nginx shell: / etc/init.d/ Nginx reload# vim / etc/ansible/nginx_config/roles/new/tasks/main.yml / / this is the core task-name: copy conf file copy: src= {{item.src}} dest= {{nginx_basedir}} / {{item.dest}} backup=yes owner=root group=root mode=0644 with_items:-{src: nginx.conf Dest: conf/nginx.conf}-{src: vhost, dest: conf/} notify: restart nginx description: the loop object here has two children. One is src, and the other is dest,item.src 's loop-only src,item.dest in items and dest in loop-only items. Copy nginx.conf to / usr/local/nginx/conf/nginx.conf. Notify, here is handlers, using restart nginx in this file: / etc/ansible/nginx_config/roles/new/handlers/main.yml# vim / etc/ansible/nginx_config/update.yml / / finally defines the total entry configuration-hosts: testhost user: root roles:-new
Execute: ansible-playbook / etc/ansible/nginx_config/update.yml
When you change the contents of a file, execute this command again, and only that changed file will be updated.
The roles corresponding to the rollback backup.yml is old.
# rsync-av / etc/ansible/nginx_config/roles/new/ / etc/ansible/nginx_config/roles/old/
The rollback operation is to overwrite the old configuration, then reload the nginx service, and back up the nginx configuration file to old before each change, corresponding to / etc/ansible/nginx_config/roles/old/files.
# vim / etc/ansible/nginx_config/rollback.yml / / finally, define the configuration of the master portal-hosts: testhost user: root roles:-old
The order is as follows: before changing the contents of the configuration file, rsync is backed up to old, and then the configuration file under / new/files is changed, and then ansible-playbook update.yml is executed. If there is no problem, then OK. If there is a problem, perform ansible-playbook rollback.yml to roll back to the previous configuration.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.