[root@zxw63 ~]# ansible-doc -s raw - name: Executes a low-down and dirty SSH command raw: executable: # change the shell used to execute the command. Should be an absolute path to the executable. when using privilege escalation (`become‘), a default shell will be assigned if one is not provided as privilege escalation requires a shell. free_form: # (required) the raw module takes a free form command to run. There is no parameter actually named ‘free form‘; see the examples! [root@zxw63 ~]# ansible-doc -s shell - name: Execute commands in nodes. shell: chdir: # cd into this directory before running the command creates: # a filename, when it already exists, this step will *not* be run. executable: # change the shell used to execute the command. Should be an absolute path to the executable. free_form: # (required) The shell module takes a free form command to run, as a string. There‘s not an actual option named "free form". See the examples! removes: # a filename, when it does not exist, this step will *not* be run. stdin: # Set the stdin of the command directly to the specified value. warn: # if command warnings are on in ansible.cfg, do not warn about this particular line if set to no/false. [root@zxw63 ~]# ansible-doc command > COMMAND (/usr/lib/python2.7/site-packages/ansible/modules/commands/command.py) The `command‘ module takes the command name followed by a list of space-delimited arguments. The given command will be executed on all selected nodes. It will not be processed through the shell, so variables like `$HOME‘ and operations like `"<"‘, `">"‘, `"|"‘, `";"‘ and `"&"‘ will not work (use the [shell] module if you need these features). For Windows targets, use the [win_command] module instead. OPTIONS (= is mandatory): - chdir Change into this directory before running the command. [Default: (null)] version_added: 0.6 - creates A filename or (since 2.0) glob pattern, when it already exists, this step will *not* be run. [Default: (null)] = free_form The command module takes a free form command to run. There is no parameter actually named ‘free form‘. See the examples! - removes A filename or (since 2.0) glob pattern, when it does not exist, this step will *not* be run. [Default: (null)] version_added: 0.8 - stdin Set the stdin of the command directly to the specified value. [Default: None] version_added: 2.4 - warn If command_warnings are on in ansible.cfg, do not warn about this particular line if set to `no‘. [Default: yes] type: bool version_added: 1.8 NOTES: * If you want to run a command through the shell (say you are using `<‘, `>‘, `|‘, etc), you actually want the [shell] module instead. The `command‘ module is much more secure as it‘s not affected by the user‘s environment. * `creates‘, `removes‘, and `chdir‘ can be specified after the command. For instance, if you only want to run a command if a certain file does not exist, use this. * The `executable‘ parameter is removed since version 2.4. If you have a need for this parameter, use the [shell] module instead. * For Windows targets, use the [win_command] module instead. AUTHOR: Ansible Core Team, Michael DeHaan METADATA: status: - stableinterface supported_by: core EXAMPLES: - name: return motd to registered var command: cat /etc/motd register: mymotd - name: Run the command if the specified file does not exist. command: /usr/bin/make_database.sh arg1 arg2 creates=/path/to/database # You can also use the ‘args‘ form to provide the options. - name: This command will change the working directory to somedir/ and will only run when /path/to/database doesn‘t exist. command: /usr/bin/make_database.sh arg1 arg2 args: chdir: somedir/ creates: /path/to/database - name: safely use templated variable to run command. Always use the quote filter to avoid injection issues. command: cat {{ myfile|quote }} register: myoutput
使用模块 command或者shell或者raw都能调用对象机器上的某条指令或者某个可执行文
[root@zxw63 ~]# ansible webservers -m raw -a "/tmp/test.sh" 192.168.100.66 | SUCCESS | rc=0 >> this is test shell-script Shared connection to 192.168.100.66 closed. 192.168.100.128 | SUCCESS | rc=0 >> this is test shell-script Shared connection to 192.168.100.128 closed. [root@zxw63 ~]# ansible webservers -m shell -a "/tmp/test.sh" 192.168.100.66 | SUCCESS | rc=0 >> this is test shell-script 192.168.100.128 | SUCCESS | rc=0 >> this is test shell-script [root@zxw63 ~]# ansible webservers -m command -a "/tmp/test.sh" 192.168.100.66 | FAILED | rc=8 >> [Errno 8] Exec format error 192.168.100.128 | FAILED | rc=8 >> [Errno 8] 可执行文件格式错误 [root@zxw63 ~]# ansible webservers -m command -a "sh /tmp/test.sh" 192.168.100.66 | SUCCESS | rc=0 >> this is test shell-script 192.168.100.128 | SUCCESS | rc=0 >> this is test shell-script
[root@zxw63 ~]# ansible webservers -m shell -a "ls -ltr /etc | wc -l" 192.168.100.128 | SUCCESS | rc=0 >> 217 192.168.100.66 | SUCCESS | rc=0 >> 218 [root@zxw63 ~]# ansible webservers -m raw -a "ls -ltr /etc | wc -l" 192.168.100.66 | SUCCESS | rc=0 >> 218 Shared connection to 192.168.100.66 closed. 192.168.100.128 | SUCCESS | rc=0 >> 217 Shared connection to 192.168.100.128 closed. [root@zxw63 ~]# ansible webservers -m command -a "ls -ltr /etc | wc -l" 192.168.100.66 | FAILED | rc=2 >> /etc: total 1820 -rw-r--r--. 1 root root 662 Aug 29 2007 logrotate.conf -rw-r--r--. 1 root root 220 Oct 13 2008 quotagrpadmins -rw-r--r--. 1 root root 148 May 14 2009 asound.conf
原文:https://www.cnblogs.com/zxw-xxcsl/p/11194533.html