ansible常见语法:
ansible [hosts] -m [模块] -a '参数'范例:ansible all -m command -a 'ifconfig'
常见模块及其用法说明
查询模板ansible-doc -l 查询模块帮助:ansible-doc -s [模块名]
1.ping模块(不用参数)
主要检测主机是否在线
[root@iZ2ze928npym0vwwtobqr1Z .ssh]# ansible all -m ping 192.168.0.101 | SUCCESS => { "changed": false, "ping": "pong" }
2.setup模块(同样无参数,输出主机的信息,信息较多,这里就不写了,只写命令)
用法:ansible all -m setup
3.cron模块:定时任务模块
用法:ansible all -m cron -a '参数' 常见参数: state: present:安装(不加默认) absent:移除 name:命名 job:添加一个命令 job="/bin/echo hello" minute:分 hour:小时 day:天 month:月 weekday:周 范例:ansible all -m cron -a 'minute="*/10" job="/bin/echo hello" state="present" name="test
4.command模块:用于执行常见的linux命令(默认模块,不指定-m默认就是command)
范例:ansible all -m command -a 'ifconfig' 或者ansible all -a 'ifconfig'
5.shell模块:执行常见shell命令,说了command模块,为什么还要shell呢,原因很简单,command不支持传参到远程,shell支持,常见的管道符shell支持
范例:ansible all -m shell -a 'echo "123"|passwd --stdin user1'
6.user模块,用户模块,主要操作用户相关
常见模块: group:指定组 home :指定家目录 name:用户名 shell:指定shell state:指定创建还是删除 system:是否系统用户 uid :UID 范例: ansible all -m user -a 'name=tommy group=test uid=1001'
7.group:组模块
常见模块: gid :指定GID name:组名 state :组是否存在,及创建或者删除 system:是否系统组 范例: ansible all -m group -a 'name=tomcat gid=601'8.yum:yum软件的安装卸载
常见模块: name:包名,也可以带上版本号 state:present(安装)latest(最新)absent(卸载) 范例: ansible all -m yum -a 'name=httpd state=latest'9.service:服务管理
常见模块: enabled :是否开机自启 name:服务名 runlevel:运行级别 state : 状态 started|stopped|restarted 启动 停止 重启 范例: ansible all -m service -a 'name=httpd state=started enable=yes'10.script:本地脚本复制到远程主机执行
范例: ansible all -m script -a 'free_form=/tmp/1.sh'11.copy:文件复制模块
常见模块: src:本地源路径 dest:远程文件路径 mode:权限 范例: ansible all -m copy -a 'src=/etc/passwd dest=/tmp/passwd mode=644'
12.file模块:文件模块
a 'path= mode= owner= group= state={directory|link|present|absent} src='
以上只是罗列常见模块常见用法,所有用法可以帮助
评论区