Automated operation and maintenance ANSBLE

1. Installation and configuration ansble

yum install epel-release -y #Install epel warehouse
yum install ansible -y

2. Configure host list

vim /etc/ansible/hosts
[web-proxy]
#Group
192.168.1.112 ansible_ssh_port=22 ansible_ssh_user=root [emailprotected]
#IP ssh port ssh account and password
[web-server]
192.168.1.12 ansible_ssh_port=22 ansible_ssh_user=root [email protected]
192.168.1.13 ansible_ssh_port=22 ansible_ssh_user=root [email protected]

3. Test

ansible all -m ping
Automated operation and maintenance ansble
Green instructions It succeeded.
Automated operation and maintenance ansble
This kind of error report should be the first After ssh connection, the fingerprint of the other host is not on this computer, just use ssh to connect first
anisble command syntax: ansible [-i host file] [-f batch] [group name] [-m module name ] [-a module parameter]
-i can select the host list, do not specify the default to /etc/ansible/hosts to find the effective range followed by the effective range, you can select a single ip (single host) or a group of hosts or all (the All hosts in the list). -m module (can be understood as a command) -a module parameter

4.ansible-playbook simple script

vim web.yml #write automation script

  • hosts: web-server
    #Apply effective groups
    vars:
    IP: “{{ ansible_ens33[‘ipv4’][‘address’]}}”
    #The need to download Assign the ip of the machine to the variable IP. When different machines are used, this value will change according to the actual ip of the machine.
    port: 233
    #Assignment 233 to the variable port, the variable can also be in /etc/ansible/ Assign values ​​in the hosts file
    remote_user: root
    tasks:
    • name: install nginx
      yum: name=nginx state=latest
    • name: copy the nginx configuration file, If the optional trigger is executed, the following actions restart nginx
      copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
    • name: modify nginx’s monitoring IP
      lineinfile: dest=/etc/nginx/nginx.conf regexp=’listen 80 default_server;’ line=’listen {{port}} default_server;’
      #regexp is a regular match, remember if it matches The characters in the line are highlighted and need to be escaped. The characters in the line are the characters to be replaced, {{port}} is the variable to be called,
      notify:
      #trigger, the following will be called only when the above command is executed Script
    • cq nginx
      # means that if the statement above is executed, call the cq nginx command, which is defined in the handlers below.
    • name: copy the index homepage file
      copy: src=/usr/share/nginx/html/index.html dest=/usr/share/nginx/html/index.html
    • name: Enter the IP address into index.html
      lineinfile: dest=/usr/ share/nginx/html/index.html regexp=”192.168.0.0″ line=”{{IP}}:{{port}}”
    • name: start nginx service
      service: name= nginx state=started
      handlers:
    • name: cq nginx
      service: name=nginx state=restarted
      #PS yml script format and strict, please write strictly in accordance with the above format< /li>

ansible-playbook web.yml #Run script
Automated operation and maintenance ansble
All 8 commands are OK, indicating that all of them are executed successfully
Automated operation and maintenance ansble
Visit the port 233 of one of them, and found that there is already content, and the content is generated by itself according to the variables in the parameters.

5. Use of anti-generation scripts and ansible templates

cp /etc/nginx/nginx.conf nginx.conf.j2
#Copy a nginx configuration file and modify it to Template
vim nginx.conf.j2
Automated operation and maintenance ansble
Add Backend web group
Automated operation and maintenance ansble
Change the port to {{port }}, directly replace with variables when it will be delivered
vim proxy.yml #Write an automated installation anti-generation script

- hosts: web-proxy
#effective range, web-proxy Group
vars:
port: 666
#Assign 233 to the variable port, the variable can also be assigned in the /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, and the port inside has been replaced with {{prot}}, and the process of passing will be directly replaced with the above variable parameter 666
-name: start nginx service
service: name=nginx state=started

ansible-playbook proxy.yml #Run script
Automated operation and maintenance ansble
This is my second run. 4 OK, all the commands are executed successfully. Ansible is idempotent and is already in the target state. Ansible will not run again.
Automated operation and maintenance ansble
Automated operation and maintenance ansbleF5 refresh, load can be balanced, reverse generation is successful

Leave a Comment

Your email address will not be published.