Ansible module command, Shell, Raw, script

Source: Internet
Author: User
Tags python script

Environment:

Ansible End:

ip:192.168.100.129

Hostname:node1.lansgg.com

Client side:

ip:192.168.100.131

Hostname:v2.lansgg.com

ip:192.168.100.132

Hostname:v3.lansgg.com

[Email protected] ansible]# Pwd/etc/ansible[[email protected] ansible]# cat Hosts[testservers] 192.168.100.131192.168.100.132[[email protected] ansible]#

1. Command format

Synopsis Ansible 

Module Command Detail Query

Ansible-doc ModuleName

Such as:

[[email protected] ansible]# ansible-doc commandless 436copyright  (C)   1984-2009 mark nudelmanless comes with no warranty, to the extent  permitted by law. for information about the terms of redistribution,see the file  Named readme in the less distribution. homepage: http://www.greenwoodsoftware.com/less> command  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). options  (= is mandatory):- chdir        cd  into this directory before running the command [default:         None]- creates        a  filename, when it already exists, this step will *not* be         run. [Default: None]- executable         change the shell used to execute the command.  Should be an        absolute path to  the executable. [default: none]= free_form        the command module takes a free form command to  Run.  there is        no parameter actually  named  ' Free form ' . see the examples!         [Default: None]- removes        a filename,  when it does not exist, this step will *not* be         run. [Default: None]- warn         if command warnings are on in ansible.cfg, do not  Warn about        this particular line if set  to no/false. [default: true]notes:  if you want to run  a command through the shell  (say you are using          ' < ',  ' > ',  ' | ', &NBSP;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. Examples:# example from ansible playbooks.- command: /sbin/shutdown -t now# 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. 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

Explanation: Command for Module name, ChDir, create and other parameters specific to this module

2. Command of Ansible module

This module is the default module for Ansible, and is also one of the common modules

Example: Viewing the last two rows of a remote host's passwd

[Email protected] ansible]# ansible testservers-m command-a ' tail-2/etc/passwd ' 192.168.100.131 | Success | Rc=0 >>postfix:x:89:89::/var/spool/postfix:/sbin/nologinsshd:x:74:74:privilege-separated SSH:/var/empty/ sshd:/sbin/nologin192.168.100.132 | Success | Rc=0 >>sshd:x:74:74:privilege-separated Ssh:/var/empty/sshd:/sbin/nologintcpdump:x:72:72::/:/sbin/nologin

Use parameters to modify the working directory of course

[Email protected] ansible]# ansible testservers-m command-a ' pwd ' 192.168.100.132 | Success | Rc=0 >>/root192.168.100.131 | Success | rc=0 >>/root[[email protected] ansible]# ansible testservers-m command-a ' chdir=/tmp/pwd ' 192.168.100.131 | Success | Rc=0 >>/tmp192.168.100.132 | Success | Rc=0 >>/tmp


the differences and usage scenarios for Shell, command, script, raw modules for Ansbile tools

Command module [execute remote command]

[Email protected] ansible]# ansible testservers-m command-a ' uname-n '

Script module [shell/python scripts on the remote host executing the master side ]

[Email protected] ansible]# ansible testservers-m script-a '/etc/ansible/test.sh

Shell module [Shell/python script to execute remote host]

[Email protected] ansible]# ansible testservers-m shell-a ' bash/root/test.sh '

Raw module [similar to command module, support pipeline delivery ]

[[email protected] ansible]# ansible testservers-m raw-a "ifconfig eth0 |sed-n 2p |awk ' {print \$2} ' |awk-f: ' {print \ $ ' "


This article is from the "Big Wind" blog, please be sure to keep this source http://lansgg.blog.51cto.com/5675165/1745009

Ansible module command, Shell, Raw, script

Related Article

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.