Ansible's shell and script module

Source: Internet
Author: User

Using the shell module, the remote command is executed through/bin/sh, so we can use the various command methods in the terminal input;
But we define our own environment variables in. Bashrc/.bash_profile The shell module is not recognized because it is not loaded, and if you need to use a custom environment variable, you need to execute the statement that loads the custom script at the very beginning;

The use of shell modules can be divided into two blocks:

shell module uses the same as the command parameter

[[email protected] ~]# ansible-doc -s shellless 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- name: execute commands in nodes .  action: 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=              # 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     &nbsP;           # a filename, when it  does not exist, this step will *not* be run.       warn                    # if command warnings are on in ansible.cfg,  do not warn about this particular line if set to                                 no/false.
If there are fewer statements to execute, it can be written together, and the environment can be loaded ... [Email protected] ~]# ansible web-m shell-a '. ~/.bash_profile;ps-ef|grep httpd ' web | SUCCESS | rc=0 >>root 4406 4405 0 06:32 pts/0 00:00:00/bin/sh-c. ~/.bash_profile;ps-ef|grep httpdroot 4410 4406 0 06:32 pts/0 00:00:00 grep httpd


The scripts module can write a script locally and then execute it on the remote server. As follows

[[email protected] ~]# ansible-doc -s scriptless 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- name: runs a local script  on a remote node after transferring it  action: script       creates                 # a filename, when it already exists, this  step will *not* be run.      free_form=              # path to the local script file followed by  optional arguments.      removes                 # a filename, when it does  not exist, this step will *not* be run.

Name: Copy the local script to the remote host and run it
Action:script
Creates # a filename, when the file exists, the command does not execute
free_form= # Local Script path
Removes # A filename, this file does not exist, then the command does not execute

[email protected] ~]# cat time.sh #!/bin/bashdate >/tmp/shijian

Using the script module, the use is quite a path or an absolute path can be used.

[Email protected] ~]# ansible web-m script-a ' time.sh ' web | SUCCESS = {"Changed": True, "RC": 0, "stderr": "", "stdout": "", "Stdout_lines": []}


Verify the content on the Web server

[Email protected] ~]# ansible web-a ' Cat/tmp/shijian '

web | SUCCESS | Rc=0 >>

Sun May 1 06:35:30 CST 2016



This article is from the "Village Boys" blog, please make sure to keep this source http://noodle.blog.51cto.com/2925423/1769474

Ansible's shell and script module

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.