ansible 操作目标 -m 模块名 -a 模块参数
运程命令模块:
模块包括command(默认)、script、shell。
command :在远程执行权限内的shell命令
script:在远程主机执行控制端的脚本文件。
shell:在控制端执行远程主机上的shell脚本文件
copy模块:
控制端向目标主机拷贝文件,类似scp
ansible webservers -m copy -a "src=/root/ dest=/tmp/"
stat模块:
获取远程主机文件状态信息。
ansible webservers -m stat -a "path=/root/filename"
get_url模块:
实现在远程主机下载指定url到远程主机上
ansible webservers -m get_url -a "url=http://www.baidu.com dest=/root/index.html"
yum 模块:
实现包管理 有yum 和apt-get两种
ansible webservers -m apt -a "pkg=curl state=latest"
ansible webservers -m yum -a "name=curl state=latest"
cron模块:
远程主机crontab操作
ansible webservers -m cron -a "name='check file' hour='5,2' job='ls -al' "
mout模块:
远程主机分挂载:
ansible wenservers -m mount -a "name=/mnt/data src=/dev/sda fstype =ext3"
service 模块:
远程主机系统服务管理:
ansible webservers -m service -a "name=httpd state=restarted"
ansible webservers -m service -a "name=httpd state=started"
sysctl 包管理模块:
远程主机sysctl配置
user 模块:
远程主机系统用户管理
添加用户fuchao
ansible webservers -m user -a "name=fuchao "
删除用户:
ansible webservers -m user -a "name=fuchao state=absent remove=yes"
[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的版本