Ad-hoc 命令是一种可以快速输入的命令,而且不需要保存起来的命令。就相当于 bash 中的一句话 shell。本文将介绍一些平时使用 Ad-Hoc 命令时用到较多的参数。
Ansible 常用参数说明
使用ansible -h
命令就可以列出所有的命令参数,下面列举了常用的一些参数。
option | 说明 |
---|---|
-v |
输出详细信息 |
-i |
指定inventory文件,默认使用/etc/ansible/hosts |
-f |
fork的进程个数,默认为5 |
--list-hosts |
列出主机列表,并不会执行其他操作 |
-private-key=xxx |
指定ssh连接使用的文件 |
-m |
指定module,默认为command |
-a |
指定module的参数 |
-o |
精简输出内容 |
-k |
提示输入密码 |
-K |
提示输入sudo密码,与-sudo 一起使用 |
-T |
设置连接超时时间 |
-B |
设置后台运行并设置超时时长 |
使用示例
-i示例
1 | [root@centos7 ~]# cat hosts |
-k示例
不指定module
,默认将使用command
。1
2
3
4[root@centos7 ~]# ansible -i hosts apache -k -o -a "echo hello"
SSH password:
172.20.21.123 | SUCCESS | rc=0 | (stdout) hello
172.20.21.121 | SUCCESS | rc=0 | (stdout) hello
ansible-doc -s取得更详细信息
希望知道更加详细的module的信息,最好的方法是使用ansible自带的ansible-doc
的-s
选项。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25[root@centos7 ~]# 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@centos7 ~]# ansible-doc -s expect
- name: Executes a command and responds to prompts.
expect:
chdir: # Change into this directory before running the command.
command: # (required) The command module takes command to run.
creates: # A filename, when it already exists, this step will *not* be run.
echo: # Whether or not to echo out your response strings.
removes: # A filename, when it does not exist, this step will *not* be run.
responses: # (required) Mapping of expected string/regex and string to respond with.
If the response is a list, successive
matches return successive responses. List
functionality is new in 2.1.
timeout: # Amount of time in seconds to wait for the expected strings.
所有命令参数
1 | [root@localhost ~]# ansible -h |