文章目录
ansible命令行模块
一、ansible-doc命令 ——默认模块 - 执行命令
适合使用简单的命令,无法支持"<",">","|",";","&"等符号
ansible-doc命令常用于获取模块信息及其使用帮助
//列出所有已安装的模块;注:按q退出
ansible-doc -l
//-s列出yum模块描述信息和操作动作
ansible-doc -s yum
二、ansible 常用模块
命令格式: ansible [主机] [-m 模块] [-a args]
参数
1、command模块
//指定ip执行date
ansible 192.168.182.22 -m command -a 'date'
//指定分类执行date
ansible webserver -m command -a 'date'
ansible mysql -m command -a 'date'
//所有hosts主机执行date
ansible all -m command -a 'date'
//如果不加-m模块,则默认运行command模块
ansible all -a 'ls /'
2、cron模块
该模块适用于管理cron
计划任务的,其使用的语法跟我们的crontab
文件中的语法一致
查看cron模块信息
ansible-doc -s cron
#查看cron模块信息
ansible-doc -s cron
#webserver:分类 -m指定模块 -a输出模块内的指令 分钟:每分钟,工作:输出hello,工作名称:test
ansible webserver -m cron -a 'minute="*/1" job="/usr/bin/echo hello" name="test"'
#查看计划性任务命令
ansible webserver -a 'crontab -l'
#移除计划性任务
ansible webserver -m cron -a 'name="test" state=absent'
3、user模块(用户管理)
模块主要是用来管理用户账号
user模块是请求的是useradd,userdel,usermod三个指令
查看user模块信息
ansible -a user
//创建用户test01
ansible mysql -m user -a 'name="test01"'
//查看创建的用户信息
ansible mysql -a 'tail /etc/passwd'
//删除用户test01
ansible mysql -m user -a 'name="test01" state=absent'
4、group模块 (用户(组)模块)
该模块主要用于添加或删除组。
group模块请求的是groupadd,groupdel,groupmod三个指令。
查看group模块信息
ansible-doc -s group
ansible mysql -m user -a 'name="test01"'
//创建组
ansible mysql -m group -a 'name=mysql gid=300 system=yes'
//查看组信息
ansible mysql -a 'tail /etc/group'
//使用user模块向组里添加成员
ansible mysql -m user -a 'name=test01 uid=306 system=yes group=mysql'
ansible mysql -a 'id test01'
5、copy模块 ( 复制模块)
对文件进行有效的复制
ansible-doc -s copy
ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.back'
ansible mysql -a 'ls /opt'
ansible mysql -a 'cat /opt/fstab.back'
ansible mysql -m copy -a 'content="hello lic" dest=/opt/test.txt'
ansible mysql -a 'cat /opt/test.txt'
6、file模块 (指定文件属性)
ansible-doc -s file
ansible mysql -m user -a 'name=mysql system=yes'
ansible mysql -m file -a 'owner=mysql group=mysql mode=600 path=/opt/test.txt'
ansible mysql -a 'ls -l /opt/test.txt'
#创建
#ansible mysql -m file -a 'path=/opt/abc.txt state=touch'
ansible mysql -m file -a 'src=/opt/test.txt path=/opt/test.txt.link state=link'
ansible mysql -a 'ls -l /opt'
#移除文件/opt/test.txt
ansible mysql -m file -a 'path=/opt/test.txt state=absent'
7、ping模块(测试连通状态)
ansible all -m ping
8、service模块(管理服务状态)
service:用于管理服务运行状态
ansible-doc -s service
[root@localhost ~]# ansible webservers -m service -a 'name=httpd enabled=true state=started'
##启动httpd服务,前提是将httpd服务安装好
192.168.182.22 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started",
......
[root@localhost ~]# ansible webservers -m service -a 'name=httpd enabled=true state=stopped' #关闭服务
9、shell 模块 (免交互)
shell 模块可以使用"<",">","|",";","&"等符号特殊符号,使用方法与 command 模块一致。
ansible-doc -s shell
ansible mysql -m user -a 'name=zhangsan'
ansible mysql -m shell -a 'echo 123123 | passwd --stdin zhangsan'
10、yum模块 (安装/卸载软件包)
yum:使用yum软件包管理器安装,升级,降级,删除和列出软件包和组
[root@localhost ~]# ansible webservers -m yum -a 'name=httpd'
##安装httpd服务
[root@localhost ~]# ansible webservers -m yum -a 'name=httpd'
192.168.182.22 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"httpd"
]
},
......
[root@localhost ~]# ansible mysql -m yum -a 'name=httpd state=absent'
##移除httpd服务
11、script 模块 (执行脚本)
[root@localhost ~]# cd /opt/
[root@localhost opt]# vim test.sh
#!/bin/bash
echo "this is test script" > /opt/script.txt
chmod 666 /opt/script.txt
[root@localhost opt]# chmod +x test.sh
[root@localhost opt]# ansible all -m script -a 'test.sh' #使用script模块执行创建的脚本
192.168.200.30 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.200.30 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.200.30 closed."
],
"stdout": "",
"stdout_lines": []
}
192.168.200.40 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.200.40 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.200.40 closed."
],
"stdout": "",
"stdout_lines": []
}
12、setup 模块 -(收集信息)
ansible-doc -s setup
#获取MySQL组主机的facts信息
ansible mysql -m setup
Ansible facts 是远程系统的信息,主要包含IP地址,操作系统,以太网设备,mac 地址,时间/日期相关数据,硬件信息等信息。