Ad-hoc refers to a command that is executed temporarily under ansible and does not require a saved command, and playbook is used for complex commands. Ad-hoc's execution relies on modules, and Ansible provides a large number of modules. such as: command, raw, Shell, file, cron, etc., can be viewed through ansible-doc-l. You can use the Ansible-doc-s module to view the parameters of a module, or you can use the Ansible-doc Help module to view more detailed information about the module.
I. AD-HOC 1, command description
The execution of a AD-HOC command needs to be performed in the following format:
Ansible host or group-M module name-a ' module parameter ' ansible parameter
The host and the group are the specified part in the/etc/ansible/hosts, and of course the dynamic inventory uses the host that the script obtains from the external application;
Module name, you can view the currently installed module through ANSIBLE-DOC-L, when not specified by default, using the command module, you can view the/etc/ansible/ansible.cfg "#module_name = command" section, The default module can be modified in this configuration file;
Module parameters, you can see the specific usage and the following parameters through the "ansible-doc-s module name";
Ansible parameter, can be found through the help information of the ansible command, there are many parameters to choose from, such as whether to enter a password, whether sudo, etc.
2. Backstage execution
When the command execution time is longer, it can also be executed in the background, using the-B,-p parameters, as follows:
Ansible all-b 3600-a "/usr/bin/long_running_operation--do-stuff" #后台执行命令3600s,-B indicates the time of the background execution
Ansible all-m async_status-a "jid=123456789" #检查任务的状态
Ansible all-b 1800-p 60-a "/usr/bin/long_running_operation--do-stuff" #后台执行命令最大时间是1800s即30分钟,-p per 60s check status, default 15s
Second, the command execution module
The command execution module consists of the following four modules:
Command module: The module can be executed directly with the commands to be executed with-a followed by a command that does not succeed "<", ">", "|", "&" If there is a part with the following characters.
Shell module: Use basic and command, but it is executed through/bin/sh, so the shell module can execute any command, as in native execution;
Raw modules: As with shell modules, they can also execute arbitrary commands, just as they do in native execution;
Script module: It is the management side of the shell on the managed host execution, the principle is to first copy the shell to the remote host, and then execute on the remote host, similar to the principle of raw modules.
Note: Raw module and comand, Shell module is not the ChDir, creates, removes parameters, the function of the ChDir parameter is to first cut to chdir the specified directory, then execute the following command, which in many later modules will have this parameter.
The command module contains the following options:
Creates: A filename when the file exists, the command does not execute
Free_form: Linux instructions to execute
ChDir: Before executing the instruction, switch to the specified directory
Removes: A filename when the file does not exist, this option does not execute
Executable: Switch the shell to execute the instruction, the execution path must be an absolute path
Examples of using chdir:
Ansible 192.168.1.1-m command-a ' chdir=/tmp/test.txt touch test.file '
Ansible 192.168.1.1-m shell-a ' chdir=/tmp/test.txt touch test2.file '
Ansible 192.168.1.1-m raw-a ' chdir=/tmp/text.txt touch test3.file '
Three commands will return the status of successful execution. However, only the first two files are actually created successfully. The result file that uses the raw module's execution is in fact also created normally, but not in the directory specified by ChDir, but in the current execution of the user's home directory.
Creates and removes examples:
Ansible 192.168.1.1-a ' creates=/tmp/server.txt uptime ' #当/tmp/server.txt file exists, the uptime directive is not executed
Ansible 192.168.1.1-a ' removes=/tmp/server.txt uptime ' #当/tmp/server.txt file does not exist, the uptime instruction is not executed
Example Script module:
The script file to be executed script.sh content as follows:
#/bin/bashifconfigdf-ht
Execute ansible directive: ansible 10.212.52.252-m script-a ' script.sh ' |egrep ' >>|stdout '
This article is from the "Nobody" blog, please be sure to keep this source http://breezey.blog.51cto.com/2400275/1757588
Ansible4:ad-hoc and command execution module "Go"