Managing remote servers in bulk using Ansible
Background local need to manage the remote batch of servers, mainly perform the following tasks: 1) Copy Local files to all remote servers; 2) need to execute commands on remote server; Remote server path is not exactly the same. General access to the variable path defined in the environment variable; For example, define $app_path=/opt/app/bin final selection ansible in. BASHRC, and use this automated OPS tool to meet my needs; Here are the main modules of the ansible that I need to use for this scenario; about ansible and the installation configuration please Baidu; Copy copy module using copy module, you can copy the local file to the remote server with a key, and follow the parameters after -a. Specify local file and remote path;ansible myservers -m copy -a "src=/opt/app/bin/transfer.tar dest= ~/"Ansible does not execute after logging on to the remote server via SSH. Bash_profile to set user-defined environment variables; if we need to manage the path of the target server, we cannot write the absolute path directly or write the path of the variable substitution. For example: The target replication path for Server A is /opt/app/user1/bin , and the destination replication path for Server B is/opt/app/user2/bin; the path variable for both paths in their respective servers is set to $ bin; but in the copy module, we cannot directly use the dest = $bin/; path settings are generally placed in the. bashrc /.bash_profile file, However, the Ansible module does not load these two files after login; workaround: In this case, the dest path can be set to ~/, copied to the user directory, and subsequently through Remote scripting; Remote batch commands need to execute commands remotely to manage remote servers commands, Shell, scripts, and raw modules are available for remote execution, and the Command Module command module is the ansible default module, and the command module is used when the-m parameter is not specified; Comand modules are simple, common commands can be used, but the execution of their commands is not performed through the shell, so, like these "<", ">", &NBSP; "|", and "&" operation is not possible, of course, also does not support pipeline; 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: The pipeline is not supported, you can not batch execution of commands, Shell module using shell module, the remote command through the/bin/sh to execute, so we in the terminal input of the various command methods, are able to use; but we ourselves defined in the. bashrc/. Environment variables in Bash_profile the shell module is not recognized because it is not loaded, and if you need to use a custom environment variable, you need to execute the statement loading the custom script at the very beginning, and the use of the shell module can be divided into two blocks: 1) If there are fewer statements to execute, you can write them directly in a sentence:ansible myservers -a ". .bash_profile;ps -fe |grep sa_q " -m shell2) If there are more statements to be executed remotely, a script can be written to the far end via the copy module and then executed, but this involves two times ansible calls; Ansible already for us, the script module is doing it; the scripts module uses the scripts module to 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 for the command module: http://docs.ansible.com/list_of_commands_ modules.html Bulk Execution playbooks remote batchAnother way to execute the command is with playbooks; here is the official document of Playbooks:http://docs.ansible.com/playbooks.html Here are playbooks examples of ansible: Https://github.com/ansible/ansible-examples using ansbile in Python The way the API executes the Ansible module is called directly on the command line, and if further processing is required for the returned result, the Ansible module can be used in the program through API calls: for example, The above method calls the scripts module in the command line in the API call: 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 given by the official Run directly once, the result is all printed out, there will be an intuitive understanding of:#!/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 ():p rint "%s >>> %s" % ( Hostname, result) API design see: http://docs.ansible.com/developing_api.html
This article from "~" blog, declined reprint!
Ansible managing remote servers in bulk