Use ansible to manage remote servers in batches.

Source: Internet
Author: User

Use ansible to manage remote servers in batches.
Use ansible to batch manage remote server background

Local management of a batch of remote servers is required, mainly to execute the following tasks:

1) Copy local files to all remote servers;
2) execute commands on the remote server;

The paths of the remote server are not completely consistent. Generally, access is performed through the variable paths defined in the environment variables;
For example, define $ app_path =/opt/app/bin in. bashrc.

Finally, ansible is selected. Using this automated O & M tool can meet my needs;
The following describes the main modules of ansible for my use in this scenario;
For details about what ansible is and how to install it, Baidu;

Copy Module

The copy module allows you to copy local files to a remote server with one click;
-A is followed by the parameter. The parameter specifies the local file and remote path;

ansible myservers -m copy -a "src=/opt/app/bin/transfer.tar dest=~/"

Ansible does not run after logging on to the remote server through ssh. bash_profile is used to set custom environment variables. If the path of the target server to be managed is different, you cannot directly write the absolute path or the path to replace the variables;

For example, the target replication path for server A is/opt/app/user1/bin, and the target replication path for server B is/opt/app/user2/bin; both paths are set to $ bin in their respective servers. However, in the copy module, we cannot directly use dest = $ bin /;
The path settings are generally stored in the. bashrc/. bash_profile file, but the ansible module does not load these two files after logon;

Solution:
In this case, you can set the dest path ~ /, Are all copied to the user directory, and then processed by remote scripts;

Remote batch command

You need to execute commands remotely to manage remote servers;

The command, shell, scripts, and raw modules are used to remotely execute commands;

Command Module

The command module is the default ansible module. If the-m parameter is not specified, the command module is used;
The comand module is relatively simple and Common commands can be used, but its command execution is not through shell, so, like these "<", ">", "| ", and "&" operations are not allowed. Of course, pipelines are not supported;
Example: Show remote path:

ansible myservers  -a 'pwd'10.6.143.38 | success | rc=0 >>/home/rduser10.6.143.53 | success | rc=0 >>/home/rduser10.6.143.37 | success | rc=0 >>/home/rduser

Disadvantage: If MPs queue is not supported, you cannot execute commands in batches;

Shell Module

Use the shell module to execute remote commands through/bin/sh. Therefore, we can use various command Methods entered on the terminal;
But we define it in. bashrc /. the shell module of the environment variable in bash_profile cannot be identified because it is not loaded. To use a custom environment variable, You need to execute the statement to load the custom script at the beginning;

The use of the shell module can be divided into two parts:
1) if there are few statements to be executed, you can write them in one sentence:

ansible myservers  -a ". .bash_profile;ps -fe |grep sa_q" -m shell

2) if there are many remote statements to be executed, you can write them into a script, which is uploaded to the remote end through the copy module, and then executed; but this involves two ansible calls; for this demand, ansible has taken into consideration for us, and the script module is doing this;

Scripts Module

Using the scripts module, you can write a script locally and execute it on a remote server:

ansible myservers  -m script -a "/opt/app/target.sh"

Here is the official document of the command module:
Http://docs.ansible.com/list_of_commands_modules.html

Batch Execute playbooks

Another method for remote batch command execution is playbooks;
This is playbooks official documentation: http://docs.ansible.com/playbooks.html
The playbooks example of ansible: https://github.com/ansible/ansible-examples

Use ansbile API in python

The preceding methods for executing the ansible module are called directly in the command line. If you need to further process the returned results, you can use the ansible module by calling the API in the program:
For example, the preceding method of calling the scripts module in the command line is called in the API:

import ansible.runnerresults = ansible.runner.Runner(pattern='myservers', forks=5,module_name='script', module_args='/opt/app/target.sh',).run()

Here is a detailed example provided by the official team. Run the command once and print all the results to give you an intuitive understanding:

#!/usr/bin/pythonimport ansible.runnerimport sys# construct the ansible runner and execute on all hostsresults = ansible.runner.Runner(pattern='*', forks=10,module_name='command', module_args='/usr/bin/uptime',).run()if results is None:   print "No hosts found"   sys.exit(1)print "UP ***********"for (hostname, result) in results['contacted'].items():if not 'failed' in result:print "%s >>> %s" % (hostname, result['stdout'])print "FAILED *******"for (hostname, result) in results['contacted'].items():if 'failed' in result:print "%s >>> %s" % (hostname, result['msg'])print "DOWN *********"for (hostname, result) in results['dark'].items():print "%s >>> %s" % (hostname, result)

API design details: http://docs.ansible.com/developing_api.html

Posted by: Large CC | 26MAY, 2015
Blog: blog.me115.com [subscription]
Weibo: Sina Weibo

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.