Ansible command, Shell, Raw, expect, script, telnet[turn]

Source: Internet
Author: User
Tags ansible modules



This paper mainly introduces several command modules of ansible, including:


    • command-Execute commands on the remote node
    • shell-Let the remote host execute the command under the shell process
    • script-Executes the local script after it is delivered to the remote host
    • raw-Perform low-level and dirty SSH commands
    • expect-Execute commands and respond to prompts
    • telnet-Perform low-level and dirty Telnet commands
Command Module Introduction
    • commandThe module is used to run system commands on the given node, such as echo hello.
    • It does not handle commands through the shell and therefore does not support$HOMEvariables like this and, as well,,<>|,;and&so on are not valid. That iscommand, you cannot use pipe characters in a module.
Module parameters
name remarks
ChDir No CD to this directory before running command commands
Creates No If the file that corresponds to this parameter exists, do not run the command
Free_form Yes Scripts that need to be executed (no real parameter is Free_form)
Executable No Changing the shell used to execute the command should be the absolute path to the executable file.
Removes No If the file corresponding to this parameter does not exist, do not run the command, as opposed to the creates parameter
StdIn (added after 2.4) No Sets the stdin of the command to the specified value
Example
    • List files under the specified directory

[[email protected] ~]# ansible test -m command -a "ls /root"172.20.21.120 | SUCCESS | rc=0 >>anaconda-ks.cfgtest.shwhoami.rst[[email protected] ~]# ansible test -m command -a "ls /root creates=test.sh"172.20.21.120 | SUCCESS | rc=0 >>skipped, since test.sh exists[[email protected] ~]# ansible test -m command -a "ls /root removes=test.sh1"172.20.21.120 | SUCCESS | rc=0 >>skipped, since test.sh1 does not exist


In this case, first change the directory to the root directory, and then see if test.sh exists, and if so, the command will not execute, and if not, execute the command.



As you can see here, the command must exist, but there is no parameter named Free_form parameter .


    • Switch Directory execution commands

[[email protected] ~]# ansible test -m command -a "cat test.sh chdir=/root"172.20.21.120 | SUCCESS | rc=0 >>#!/bin/bashi=0echo $((i+1))[[email protected] ~]# ansible test -m command -a "sh test.sh chdir=/root"172.20.21.120 | SUCCESS | rc=0 >>1
    • Cannot use pipe character

[[email protected] ~]# ansible test -m command -a "ls /root | grep test"172.20.21.120 | FAILED | rc=2 >>/root:anaconda-ks.cfgtest.shwhoami.rstls: 无法访问|: 没有那个文件或目录ls: 无法访问grep: 没有那个文件或目录ls: 无法访问test: 没有那个文件或目录non-zero return code
Precautions
    • To run a command through the shell, for example,,<>etc.,|you actually need theshellmodule.
    • commandThe module is more secure because it is not affected by the user's environment
    • Starting with version 2.4, theexecutableparameters are removed. If you need this parameter, use the Shell module instead.
    • For Windows nodes, usewin_commandmodules instead.
Introduction to Shell Modules


Allow the remote host to execute commands under the shell process to support shell features, such as pipelines. Almost the same as thecommandmodule, but is used when executing the command/bin/sh.


Module parameters
name must-Choose Notes
ChDir No CD to this directory before running command commands
Creates No If the file that corresponds to this parameter exists, do not run the command
Executable No Changing the shell used to execute the command should be the absolute path to the executable file.
Free_form Yes Scripts that need to be executed (no real parameter is Free_form)
Removes No If the file corresponding to this parameter does not exist, do not run the command, as opposed to the creates parameter
StdIn (added after 2.4) No Sets the stdin of the command to the specified value
Example
    • Switch directories, execute commands and maintain output

[[email protected] ~]# ansible test -m shell -a "sh test.sh > result chdir=/root"172.20.21.120 | SUCCESS | rc=0 >>[[email protected] ~]# ansible test -m shell -a "cat result chdir=/root"172.20.21.120 | SUCCESS | rc=0 >>1
Precautions
    • If you want to execute commands safely and reliably, usecommandmodules, which is also a best practice for writing playbook.
Introduction to the Script module
    • scriptThe function of the module is to transfer the local script to the remote host after it executes
    • The given script will be processed by the shell environment on the remote node
    • scriptThe module does not require Python support on the remote system
Module parameters
name must-Choose Default Value Optional Values Notes
ChDir (added after 2.4) No CD to this directory before running command commands
Creates No If the file that corresponds to this parameter exists, do not run the command
Decrypt No yes yes/no This option controls the automatic decryption of source files that use vaults
Free_form Yes Local file path that requires script execution (no real parameter is Free_form)
Removes No If the file corresponding to this parameter does not exist, do not run the command, as opposed to the creates parameter
Example
    • Executing scripts on the remote host

[[email protected] ~]# ansible test -m command -a "ls /root"
172.20.21.120 | SUCCESS | rc=0 >>
anaconda-ks.cfg
test.sh
whoami.rst

[[email protected] ~]# ansible test -m command -a "ls /root creates=test.sh"
172.20.21.120 | SUCCESS | rc=0 >>
skipped, since test.sh exists

[[email protected] ~]# ansible test -m command -a "ls /root removes=test.sh1"
172.20.21.120 | SUCCESS | rc=0 >>
skipped, since test.sh1 does not exist
Precautions
    • In general, using ansible modules is better than push scripts
    • When the script executes, the SSH connection plug-in is forced to be-tt伪ttyassigned.伪ttyswithout the stderr channel, all stderr are sent to the standard output. If you need standard output and standard error separation, use tocopymodule.
Introduction to RAW Modules
    • rawModules are primarily used to perform some low-level, dirty SSH commands, rather than throughcommandmodules. Therawmodule is only available for the following two scenarios, the first of which is on older (Python 2.4 and earlier) hosts, and the other on any device that does not have Python installed (such as a router). In any other case, the useshellorcommandmodule is more appropriate.
    • Likescriptmodules,rawmodules do not require Python on a remote system
Module parameters
name must-Choose Notes
Executable No Changing the shell used to execute the command should be the absolute path to the executable file.
Free_form Yes Scripts that need to be executed (no real parameter is Free_form)
Example
    • Executing scripts on the remote host

[[email protected] ~]# ansible test -m command -a "cat test.sh chdir=/root"
172.20.21.120 | SUCCESS | rc=0 >>
#!/bin/bash
i=0
echo $((i+1))

[[email protected] ~]# ansible test -m command -a "sh test.sh chdir=/root"
172.20.21.120 | SUCCESS | rc=0 >>
1
Precautions
    • If you want to execute commands safely and reliably, it is best to useshellorcommandmodules instead.
    • If you are using raw from playbook, you may need to use thegather_facts: nodisable fact collection
Expect module introduction
    • expectThe module is used to execute a command on the given node and respond to the prompt.
    • It does not handle commands through the shell and therefore does not support$HOMEvariables like this and, as well,,<>|,;and&so on are not valid. That iscommand, you cannot use pipe characters in a module.
Usage requirements (on the host that executes the module)
    • Python >= 2.6
    • Pexpect >= 3.3
Module parameters
name must-Choose Default Value Notes
ChDir No CD to this directory before running command commands
Command Yes Command Module Execution Command run
Echo No Whether to echo your response string
Responses Yes The expected string/regular expression and the string are mapped to respond. If the response is a list, successive matches will return a continuous response. The list feature is a new feature in 2.1.
Creates No If the file that corresponds to this parameter exists, do not run the command
Removes No If the file corresponding to this parameter does not exist, do not run the command, as opposed to the creates parameter
Timeout No 30 Wait for the expected time in seconds
Example
    • Executing scripts on the remote host

[[email protected] ~]# ansible test -m command -a "ls /root | grep test"
172.20.21.120 | FAILED | rc=2 >>
/root:
anaconda-ks.cfg
test.sh
whoami.rstls: Cannot access |: No such file or directory
ls: Cannot access grep: No such file or directory
ls: Cannot access test: No such file or directory non-zero return code
Precautions
    • If you want to run a command through the shell (for example, you are using, and<>so on|), you must specify a shell in the command, for example/bin/bash -c "/path/to/something | grep else".
    • Theresponseskey below is a Python regular expression match, with a prefix for case-insensitive searches?i.
    • By default, if you encounter problems more than once, their string responses are repeated. If a continuous problem match requires a different response than a string response, use a list of strings as a response.
    • expectModules are designed for simple scenarios, and for more complex requirements, you shouldshellconsiderscriptusing expect code in or modules
Introduction to the Telnet module
    • expectThe module is used to perform some low-level and dirty Telnet commands, not through the module subsystem.
    • It does not handle commands through the shell and therefore does not support$HOMEvariables like this and, as well,,<>|,;and&so on are not valid. That iscommand, you cannot use pipe characters in a module.
Module parameters
name must-Choose Default Value Notes
Command Yes Commands executed in a Telnet session
Host No Remote_addr Host/target to execute the command
Password Yes Login Password
Pause No 1 Pause seconds between each issuing of a command
Port No 23 Remote port
Prompts No [u‘$‘] List of expected prompts before sending the next command
Timeout No 30 Remote operation time-out
User No Remote_user Logged in user
Example
    • Executing scripts on the remote host

[[email protected] ~]# ansible test -m shell -a "sh test.sh > result chdir=/root"
172.20.21.120 | SUCCESS | rc=0 >>


[[email protected] ~]# ansible test -m shell -a "cat result chdir=/root"
172.20.21.120 | SUCCESS | rc=0 >>
1
Precautions
    • If you want to run a command through the shell (for example, you are using, and<>so on|), you must specify a shell in the command, for example/bin/bash -c "/path/to/something | grep else".
    • Theresponseskey below is a Python regular expression match, with a prefix for case-insensitive searches?i.
    • By default, if you encounter problems more than once, their string responses are repeated. If a continuous problem match requires a different response than a string response, use a list of strings as a response.
    • expectModules are designed for simple scenarios, and for more complex requirements, you shouldshellconsiderscriptusing expect code in or modules


Hoxis
Links: https://www.jianshu.com/p/8661c107448d
Source: Pinterest


Ansible command, Shell, Raw, expect, script, telnet[turn]


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.