Ansible is a very powerful automated operation and maintenance deployment tool-recommended

OK, ansible was used in the last project. Here ’s a summary.
Do n’t know if you ’re scared, it ’s really powerful and easy to use!

ansible是什么?

It is an operation and maintenance automation deployment tool, usually used in a cluster environment, and can basically achieve one-click deployment. As a chestnut, there are now 10 microservices running on a machine. When it goes online, you need to deploy 100 to implement a distributed architecture. You cannot go to a machine to deploy and modify the corresponding configuration file. Right. This must not be exhausting, and one of the configuration is wrong, you can not find where the error is.
In this scenario, ansible is born, you only need to deploy a host computer (including written applications, configuration, scripts, etc.), and then use this host computer as a template to batch copy to the corresponding 100 units In the machine. Is it perfect
Insert picture description here

Key concept:

  • The hosts file , 100 machines corresponding to IP, port configuration are written in this file, is the entrance to the operation
  • Script , which yml file to execute
  • yml script , such as main task file main.yml
  • template template , basic j2 file, for example, xml configuration file corresponds to the
    Insert picture description here
    main file entry of the j2 file hosts.
    Insert picture description here
    It depends on the python language, you need to install the python environment in advance, operate on this basis,
    because my virtual machine environment is not used, the following Directly on the command line, you will not show the running environment to everyone.

// All hosts execute app.yml, which is lcpt grouping
ansible-playbook lcpt/app.yml
/// Specify hosts execute app.yml
ansible-playbook --limit=lcpt6 lcpt/app.yml

// Nginx configuration file changes need to modify the following directory configuration files
/home/deploy/ansible/lcpt/roles/nginx/templates/nginx.conf.j2

// Replace the entry of all services nginx configuration file nginx.yml
/home/deploy/ansible/lcpt/roles/nginx/tasks/main.yml ansible-playbook lcpt/nginx.yml

ansible shell module
specifies server start and stop services
ansible lcpt6 -m shell -a "bin/lc.sh stop trans"
ansible lcpt6 -m shell -a "bin/lc.sh start trans"

// The specified host starts and stops node
ansible lcpt4 -m shell -a "chdir=LCPT/WEB_NODE/fms pm2 stop app.js"
ansible lcpt4 -m shell -a "chdir=LCPT/WEB_NODE/fms pm2 -i 4 start app.js"

Execute the script part tags
ansible-playbook app.yml --tags "app,cfg"#run multiple tags
ansible-playbook app.yml --tags app# only run a tag to
skip a task
ansible-playbook app.yml --skip-tags cfg

------------------- All yml and remote module execution needs to be executed in the ansible directory

Commonly used is ansible-playbook --limit to execute deployment commands in groups. Basically, after the script is sorted out, the subsequent batch deployment becomes very simple and trouble-free.

Published 5 original articles · Like1 · Visits 99

Guess you like

Origin blog.csdn.net/iYhuitune/article/details/105459911