One of the most important functions in ansible is the ability to execute AD-HOC commands, and some people may not understand the meaning of the word ad-hoc, which means instant meaning, or arbitrary meaning.
In contrast to the Ansible playbook function, playbook is suitable for batch deployment environments and is generally not frequently changed. The Ad-hoc command applies to operational scenarios such as business change, such as deploying a configuration file in bulk, restarting a service, installing some packages, and so on.
The Ad-hoc commands have two modules: command, Shell. Many people do not know what their differences are, in fact, very simple.
After you enter a AD-HOC command on the terminal, Ansible generates an executable Python script file and copies it to the remote machine, which contains all the information in the command line.
If you are using a command or shell module, then the subprocess is called in the script. Popen (args,
kwargs) function, command and shell differs in that the command module uses shell=true, and the shell module uses Shell=false, which is a call to the shell, one without.
Shell=true is not recommended in official documentation, as this can lead to shell injection security issues, but in some cases it is convenient to use shell modules, such as I want to delete some files in bulk,
Ansible-i inventory all-m command-a "Rm-f/etc/yum.repos.d/centos. Repo "-u root-s-F 50-kk
If you execute the above command, you will not delete those files, why?
Because your command line contains a wildcard character
, the wildcard must have to be recognized in the shell environment, otherwise it can only remove CentOS. repo this file.
So you need to execute the following command to succeed
Ansible-i inventory all-m shell-a "Rm-f/etc/yum.repos.d/centos
. Repo "-u root-s-F 50-kk
The difference between the executable scripts generated by the two commands is one line
< Module_args = ' rm-f/etc/yum.repos.d/centos. Repo '
Module_args = ' rm-f/etc/yum.repos.d/centos* #USE_SHELL '
For example:
[Email protected] tmp]# ansible slave-m command-a "Rm-f/tmp/test*"-u root-s-F 50-kk
SSH Password:
SUDO password[defaults to SSH password]:
client02 | SUCCESS | Rc=0 >>
client01 | SUCCESS | Rc=0 >>
[[email protected] tmp]# ls
test0001 test.sh txt01 Yum.log
[[email protected] tmp]# ls
test0001 test.sh txt01 Yum.log
[Email protected] tmp]# ansible slave-m shell-a "Rm-f/tmp/test*"-u root-s-F 50-kk
SSH Password:
SUDO password[defaults to SSH password]:
client01 | SUCCESS | Rc=0 >>
client02 | SUCCESS | Rc=0 >>
[[email protected] tmp]# ls
txt01 Yum.log
[[email protected] tmp]# ls
txt01 Yum.log
That's the difference between Shell and command.
See here, must have made you clear a lot of it!
Http://www.cnblogs.com/hemhem/archive/2011/03/14/2087482.html
http://www.ruby-lang.org/en/downloads/
The difference between Shell module and command module in Ansible