java distributed system deployment learning (6) ansible Ad-hoc and commands module

Ad-Hoc refers to a command executed temporarily under ansible and does not need to be saved. playbook is said after complex commands. When it comes to Ad-hoc, we need to mention modules. All command execution depends on pre-written modules. The default installed ansible already has many modules, such as command, raw, shell, file, cron, etc., which can be viewed through ansible-doc-l.

I. Ad-hoc

1. Direct implementation

Here's another example that is often used in the last few articles:

# ansible localhost -a 'uptime'
localhost | SUCCESS | rc=0 >>
15:03  up 25 days,  3:30, 2 users, load averages: 1.68 2.35 2.23

The execution of an ad-hoc command requires the following format:

Ansible host or group-m module name-a'module parameter'ansible parameter

Hosts and groups are specified in / etc/ansible/hosts. Of course, dynamic Inventory uses hosts that scripts get from external applications. This part can be referred to in detail. Dynamic Inventory
Module name, you can view the currently installed module through ansible-doc-l. When not specified by default, you use the command module. Specifically, you can see the # module_name = command section of / etc/ansible/ansible.cfg. The default module can be modified in the configuration file.
Module parameters can be seen through the "ansible-doc module name" to see the specific usage and subsequent parameters;
The ansible parameters can be seen in 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. Background execution

When the command is executed for a long time, it can also be executed in the background, where - B, - P parameters are used, as follows:

Ansible all-B 3600-a "/ usr/bin/long_running_operation--do-stuff" Backstage execution command 3600s, -B indicates the time of backstage execution.
Ansible all-m async_status-a "jid = 123456789"  Check the status of the task
 Ansible all-B 1800-P 60-a"/usr/bin/long_running_operation--do-stuff"\ Background execution command maximum time is 1800s that is 30 minutes, -P every 60s check state default 15s

Examples are as follows:

# ansible -B 3600 -P 0 -a 'watch ls'
background launch... | success >> {
    "ansible_job_id": "411650646689.13501",
    "results_file": "/root/.ansible_async/411650646689.13501",
    "started": 1
# ansible -m async_status -a 'jid=411650646689.13501' | success >> {
    "ansible_job_id": "411650646689.13501",
    "changed": false,
    "finished": 0,
    "results_file": "/root/.ansible_async/411650646689.13501",
    "started": 1

If the - P or - P parameter is not specified as 0, the task will be refreshed by the - P direct parameter until the execution of the command exceeds the time specified by the - B parameter:

# ansible -B 3600  -a 'watch ls'
background launch... | success >> {
    "ansible_job_id": "397200656414.15008",
    "results_file": "/root/.ansible_async/397200656414.15008",
    "started": 1
} | success >> {
    "ansible_job_id": "397200656414.15008",
    "changed": false,
    "finished": 0,
    "results_file": "/root/.ansible_async/397200656414.15008",
    "started": 1
<job 397200656414.15008> polling on, 3585s remaining

But here on the ansible that I use, the failure information is directly overdue, and the status information is not printed regularly. There are doubts here.

# ansible localhost -B 60 -P 10 -a 'top'
localhost | FAILED | rc=-1 >>
async task did not complete within the requested time

2. commands module

As mentioned above, ansbile itself has brought many modules, which can be viewed through ansible-doc-l. Here, we will combine command, shell, raw, script module to understand its usage.
The above four modules belong to Class commands.

  • Command module, which can be directly executed by following the command to be executed through-a, but if there is a part of the command with the following characters, it will not execute successfully "so variables like $HOME and operations like"<"," >", |", and "&" will not work(use the shell module if you need these features)";
  • The shell module is used in the same way as the command, but it is executed through / bin/sh, so the shell module can execute any command, just as it executes locally, "It is almost exactly like the command module but runs the command through a shell (/bin/sh) on the remote node."
  • The raw module, like the shell module, can also execute arbitrary commands, just as it executes locally, "Executes a low-down and dirty SSH commands, not going through the module subsystem. There is no change handler support for this module. This module does not require Python on the remote system"
  • The script module is to execute the shell of the management side on the managed host. Its principle is to copy the shell to the remote host and then execute it on the remote host. The principle is similar to that of the ray module: "This module does not require python on the remote system, much like the raw module."

Note: The difference between the raw module and the COM and shell module is that they do not have chdir, create, removes parameters. The function of chdir parameters is to cut to the directory specified by chdir first, and then execute the following commands. This parameter will be found in many later modules.

The command module contains the following options:

  • Create: A filename that does not execute when the file exists
  • free_form: linux instructions to be executed
  • chdir: Switch to the specified directory before executing the instruction
  • removes: A filename that does not execute when the file does not exist
  • executable: Switch the shell to execute instructions, which must be an absolute path

command module, raw module, shell module example:

# ansible -m command -a 'ps auxf|grep snmp' | FAILED | rc=1 >>
ERROR: Unsupported option (BSD syntax)
********* simple selection *********  ********* selection by list *********
-A all processes                      -C by command name
-N negate selection                   -G by real group ID (supports names)
-a all w/ tty except session leaders  -U by real user ID (supports names)
-d all except session leaders         -g by session OR by effective group name
-e all processes                      -p by process ID
T  all processes on this terminal     -s processes in the sessions given
a  all w/ tty, including other users  -t by tty
g  OBSOLETE -- DO NOT USE             -u by effective user ID (supports names)
r  only running processes             U  processes for specified users
x  processes w/o controlling ttys     t  by tty
*********** output format **********  *********** long options ***********
-o,o user-defined  -f full            --Group --User --pid --cols --ppid
-j,j job control   s  signal          --group --user --sid --rows --info
-O,O preloaded -o  v  virtual memory  --cumulative --format --deselect
-l,l long          u  user-oriented   --sort --tty --forest --version
-F   extra full    X  registers       --heading --no-heading --context
                    ********* misc options *********
-V,V  show version      L  list format codes  f  ASCII art forest
-m,m,-L,-T,H  threads   S  children in sum    -y change -l format
-M,Z  security data     c  true command name  -c scheduling class
-w,w  wide output       n  numeric WCHAN,UID  -H process hierarchy
# ansible -m raw -a 'ps auxf|grep snmp' | success | rc=0 >>
root      5580 25.0  0.0  12876  1792 pts/2    Ss+  12:36   0:00      \_ bash -c ps auxf|grep snmp
root      5607  0.0  0.0   5720   832 pts/2    S+   12:36   0:00          \_ grep snmp
root     24364  0.0  0.0  70416  6696 ?        SNl  May15   0:22 /usr/sbin/snmpd -r -A -LF i /var/log/net-snmpd.log -p /var/run/
# ansible -m shell -a 'ps auxf|grep snmp' | success | rc=0 >>
root      5803  0.0  0.0  11308  1308 pts/2    S+   12:36   0:00              \_ /bin/sh -c ps auxf|grep snmp
root      5805  0.0  0.0   4260   572 pts/2    S+   12:36   0:00                  \_ grep snmp
root     24364  0.0  0.0  70416  6696 ?        SNl  May15   0:22 /usr/sbin/snmpd -r -A -LF i /var/log/net-snmpd.log -p /var/run/

As you can see from the above execution, I added a pipeline here. The command module made an error while executing, and both the raw module and the shell module were normal.
Examples of using chdir:

# ansible -m command -a 'chdir=/tmp/361way touch test.file' | success | rc=0 >>
# ansible -m shell -a 'chdir=/tmp/361way touch test2.file' | success | rc=0 >>
# ansible -m raw -a 'chdir=/tmp/361way touch test3.file' | success | rc=0 >>

From the above results, all three commands were successfully executed. However, the first two files were successfully created by viewing them on a remote host:

# ls /tmp/361way
test.file  test2.file

The result files executed using the raw module are also created normally, but not in the directory specified by chdir, but in the current user's home directory executed.

# ls ~/test3.file

creates and removes examples:

Here I create a / tmp/361way/server.txt file on the test host. The results are as follows:

# ansible -a 'creates=/tmp/361way/server.txt uptime' | success | rc=0 >>
skipped, since /tmp/361way/server.txt exists
# ansible -a 'removes=/tmp/361way/server.txt uptime' | success | rc=0 >>
 15:11pm  up 28 days  0:34,  2 users,  load average: 0.75, 0.46, 0.39

Examples of script modules:

# cat
df -hl
ps auxf|grep snmp
# ansible -m script -a '' | success >> {
    "changed": true,
    "rc": 0,
    "stderr": "OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb .....slightly"

There are a lot of output and it looks messy, but check out the stdout section, which is actually the result of execution. It can be used together with pipes. It can be used as follows:

# ansible -m script -a '' |egrep '>>|stdout'

Change from:

Keywords: ansible shell Session Python

Added by mikusan on Sun, 19 May 2019 07:26:17 +0300