首页 > 其他 > 详细

ansible-playbook -1 参数说明

时间:2019-07-17 11:54:42      阅读:140      评论:0      收藏:0      [点我收藏+]

1、 ansible-doc

[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.  Theres 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 its not affected by the users 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 doesnt 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

 

2、shell、raw、shell、script

使用模块 command或者shell或者raw都能调用对象机器上的某条指令或者某个可执行文

  1.     command模块,执行远程命令
  2.     script模块 ,在远程主机执行主控端的shell/python脚本
  3.     shell模块 ,执行远程主机的shell/python脚本
  4.     raw模块 ,类似于command模块、支持管道传递

  1、直接运行脚本

[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

  2、是否支持管道

  1.   shell、raw支持管道
  2.   command支持管道
[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

   3、command不支持通配符

 

ansible-playbook -1 参数说明

原文:https://www.cnblogs.com/zxw-xxcsl/p/11194533.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!