Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Automatic operation and maintenance ansble

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

1. Install and configure ansble

Yum install epel-release-y # install epel repository

Yum install ansible-y

two。 Configure host list

Vim / etc/ansible/hosts

[web-proxy]

# grouping

192.168.1.112 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=yxy7714707@

# Port ssh account and password of IP ssh

[web-server]

192.168.1.12 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=yxy7714707@

192.168.1.13 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=yxy7714707@

3. test

Ansible all-m ping

Green means it's successful.

This kind of error report is due to the fact that the fingerprint of the host computer is not on this computer when you connect with ssh for the first time. You can connect with ssh first.

Anisble command syntax: ansible [- I host file] [- f batch] [group name] [- m module name] [- a module parameter]

-I can select the list of hosts. Do not specify the default to / etc/ansible/hosts search followed by the effective range. You can select a single ip (single host) or a group of hosts or all (all hosts in the list). -m module (can be understood as command)-a module parameter

4.ansible-playbook simple small script

Vim web.yml # write automation scripts

Hosts: web-server

# groups for which the application takes effect

Vars:

IP: "{{ansible_ens33 ['ipv4'] [' address']}}"

# assign the ip of the machine to be sent to the variable IP. When different machines are applied, this value will change according to the actual ip of the machine

Port: 233

# assign a value of 233to the variable port, and the variable can also be assigned in the / etc/ansible/hosts file

Remote_user: root

Tasks:name: installing nginx

Yum: name=nginx state=latestname: copy the nginx configuration file. If an optional trigger is performed, the following action restarts nginx

Copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.confname: modify the listening IP of nginx

Lineinfile: dest=/etc/nginx/nginx.conf regexp='listen 80 default_server;' line='listen {{port}} default_server;'

There is a regular match in # regexp. Remember that if the matching character is highlighted and escaped, the character to be replaced is in line, and {{port}} is the variable to be called.

Notify:

# trigger, the following script cq nginx will be called only if the above command is executed

# means that if the above statement is executed, the cq nginx instruction is called, which is in the following name defined in handlers: copy the index home page file

Copy: src=/usr/share/nginx/html/index.html dest=/usr/share/nginx/html/index.htmlname: enter the IP address into the index.html

Lineinfile: dest=/usr/share/nginx/html/index.html regexp= "192.168.0.0" line= "{{IP}}: {{port}}" name: start the nginx service

Service: name=nginx state=started

Handlers:name: cq nginx

Service: name=nginx state=restarted

# PS yml script format is very strict, please write strictly according to the above format

Ansible-playbook web.yml # run the script

All 8 commands are OK, indicating that all of them have been executed successfully

Visit port 233 of one of them and find that there is already content, which is generated by itself according to the variables in the parameters.

5. Reverse script, the use of ansible templates

Cp / etc/nginx/nginx.conf nginx.conf.j2

# copy a nginx configuration file and modify it into a template

Vim nginx.conf.j2

Add backend web group

Change the port to {{port}}, and replace it with a variable when it is passed.

Vim proxy.yml # write reverse script for automatic installation

-hosts: web-proxy # effective range. Web-proxy group vars: port: 666 # assigns the variable port. The variable can also be assigned in / etc/ansible/hosts file remote_user: root tasks:-name: install nginx yum: name=nginx state=latest-name: transfer template file template: src=/root/ansible/nginx.conf.j2 dest=/etc/nginx/nginx.conf # pass the template file to the template file I have changed the port inside to {{prot}}, and the passing process will be directly replaced with the above variable parameter 666-name: start the nginx service service: name=nginx state=started

Ansible-playbook proxy.yml # run the script

This is the second time I have run 4 OK. All the commands have been executed successfully. Ansible is idempotent and is already in the target state. Ansible will not run again.

F5 refresh, can balance the load, the reverse is successful

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report