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示例
[root@centos7 ~]# cat hosts
[apache]
172.20.21.121
172.20.21.123
[nginx]
172.20.21.120
172.20.21.121
[servers:children]
apache
nginx
[servers:vars]
ansible_ssh_user='root'
ansible_ssh_pass='123456'
[root@centos7 ~]# ansible -i hosts apache --list-hosts
hosts (2):
172.20.21.121
172.20.21.123
[root@centos7 ~]# ansible -i hosts apache -m ping -o
172.20.21.123 | SUCCESS => {"changed": false, "ping": "pong"}
172.20.21.121 | SUCCESS => {"changed": false, "ping": "pong"}
-k示例
不指定module
,默认将使用command
。
[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
选项。
[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.
所有命令参数
[root@localhost ~]# ansible -h
Usage: ansible <host-pattern> [options]
Options:
-a MODULE_ARGS, --args=MODULE_ARGS 模块的参数,如果执行默认COMMAND的模块,即是命令参数,如:“date”,"pwd"等等
module arguments 模块参数
-k, --ask-pass ask for SSH password 登录密码,提示输入SSH密码而不是假设基于密钥的验证
--ask-su-pass ask for su password su切换密码
-K, --ask-sudo-pass ask for sudo password 提示密码使用sudo,sudo表示提权操作
--ask-vault-pass ask for vault password
-B SECONDS, --background=SECONDS 后台运行超时时间
run asynchronously, failing after X seconds
(default=N/A)
-C, --check don't make any changes; instead, try to predict some 只是测试一下会改变什么内容,不会真正去执行;相反,试图预测一些可能发生的变化
of the changes that may occur
-c CONNECTION, --connection=CONNECTION 连接类型使用。可能的选项是paramiko(SSH),SSH和地方。当地主要是用于crontab或启动。
connection type to use (default=smart)
-f FORKS, --forks=FORKS 并行任务数。NUM被指定为一个整数,默认是5
specify number of parallel processes to use
(default=5)
-h, --help show this help message and exit 打开帮助文档API
-i INVENTORY, --inventory-file=INVENTORY 指定库存主机文件的路径,默认为/etc/ansible/hosts
specify inventory host file
(default=/etc/ansible/hosts)
-l SUBSET, --limit=SUBSET 进一步限制所选主机/组模式 --limit=192.168.91.135 只对这个ip执行
further limit selected hosts to an additional pattern
--list-hosts outputs a list of matching hosts; does not execute
anything else
-m MODULE_NAME, --module-name=MODULE_NAME 执行模块的名字,默认使用 command 模块,所以如果是只执行单一命令可以不用 -m参数
module name to execute (default=command)
-M MODULE_PATH, --module-path=MODULE_PATH 要执行的模块的路径,默认为/usr/share/ansible/
specify path(s) to module library
(default=/usr/share/ansible/)
-o, --one-line condense output 压缩输出,摘要输出.尝试一切都在一行上输出。
-P POLL_INTERVAL, --poll=POLL_INTERVAL 调查背景工作每隔数秒。需要- b
set the poll interval if using -B (default=15)
--private-key=PRIVATE_KEY_FILE 私钥路径,使用这个文件来验证连接
use this file to authenticate the connection
-S, --su run operations with su 用 su 命令
-R SU_USER, --su-user=SU_USER 指定SU的用户,默认是root用户
run operations with su as this user (default=root)
-s, --sudo run operations with sudo (nopasswd)
-U SUDO_USER, --sudo-user=SUDO_USER sudo到哪个用户,默认为 root
desired sudo user (default=root)
-T TIMEOUT, --timeout=TIMEOUT 指定SSH默认超时时间, 默认是10S
override the SSH timeout in seconds (default=10)
-t TREE, --tree=TREE log output to this directory 将日志内容保存在该输出目录,结果保存在一个文件中在每台主机上。
-u REMOTE_USER, --user=REMOTE_USER 远程用户, 默认是root用户
connect as this user (default=root)
--vault-password-file=VAULT_PASSWORD_FILE
vault password file
-v, --verbose verbose mode (-vvv for more, -vvvv to enable 详细信息
connection debugging)
--version show program's version number and exit 输出ansible的版本
如果觉得有用,欢迎关注我的微信,有问题可以直接交流: