Ansible可以使用命令行方式进行自动化管理,它的命令行管理工具都是由一系列模块、参数所支持的
文章目录
1、command模块
命令格式
-m选项指定使用模块,默认使用command模块,用于在被管理主机上运行命令。
ansible [主机] [-m 模块] [-a args]
列出所有以安装的模块:按q退出
ansible-doc -l
fortios_router_community_list Configure community lists in Fortinet's Forti...
azure_rm_devtestlab_info Get Azure DevTest Lab facts
ecs_taskdefinition register a task definition in ecs
avi_alertscriptconfig Module for setup of AlertScriptConfig Avi RES...
tower_receive Receive assets from Ansible Tower
netapp_e_iscsi_target NetApp E-Series manage iSCSI target configura...
azure_rm_acs Manage an Azure Container Service(ACS) instan...
fortios_log_syslogd2_filter Filters for remote system server in Fortinet'...
junos_rpc Runs an arbitrary RPC over NetConf on an Juni...
na_elementsw_vlan NetApp Element Software Manage VLAN
pn_ospf CLI command to add/remove ospf protocol to a ...
pn_snmp_vacm CLI command to create/modify/delete snmp-vacm
cp_mgmt_service_sctp Manages service-sctp objects on Check Point o...
onyx_ospf Manage OSPF protocol on Mellanox ONYX network...
icx_command Run arbitrary commands on remote Ruckus ICX 7...
cs_snapshot_policy Manages volume snapshot policies on Apache Cl...
nxos_install_os Set boot options like boot, kickstart image a...
cnos_static_route Manage static IP routes on Lenovo CNOS networ...
win_eventlog Manage Windows event logs
vmware_category Manage VMware categories
vmware_host_feature_info Gathers info about an ESXi host's feature cap...
avi_cluster Module for setup of Cluster Avi RESTful Objec...
[root@shuai roles]#
列出yum模块描述信息和操作动作
ansible-doc -s yum
查看对方系统的时间
'所有hosts主机执行date命令'
ansible all -a 'date'
20.0.0.46 | CHANGED | rc=0 >>
2020年 10月 16日 星期五 13:56:41 CST
20.0.0.47 | CHANGED | rc=0 >>
2020年 10月 16日 星期五 13:56:41 CST
'指定IP执行date'
ansible 20.0.0.46 -m command -a 'date'
20.0.0.46 | CHANGED | rc=0 >>
2020年 10月 16日 星期五 14:02:51 CST
'指定分类执行date'
ansible webservers -m command -a 'date'
20.0.0.46 | CHANGED | rc=0 >>
2020年 10月 16日 星期五 14:04:01 CST
'如果不加-m模块,则默认运行command模块'
ansible all -a 'ls /'
2、cron模块【计划性任务】
cron模块用于定义任务计划。其中有两种状态(state ):present表示添加(省略状态默认使用),absent表示移除。
两种状态(state):present表示添加(可以省略),absent表示移除
查看cron模块信息
ansible-doc -s cron
创建计划性任务
*/1:间隔频率 -m:指定模块 -a:指定参数
ansible mysql -m cron -a 'minute="*/1" job="/usr/bin/echo heihei" name="shuai heihei"'
20.0.0.47 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"shuai heihei"
]
}
'查看计划任务'
20.0.0.47 | CHANGED | rc=0 >>
#Ansible: shuai heihei
*/1 * * * * /usr/bin/echo heihei
'被管理者查看执行任务'
crontab -l
#Ansible: shuai heihei
*/1 * * * * usr/bin/echo heihie
您在 /var/spool/mail/root 中有新邮件
vim /var/spool/mail/root
....省略信息...
heihei
移除计划性任务
ansible mysql -m cron -a 'name="shuai heihei" state=absent'
20.0.0.47 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": []
}
'被管理者查看'
[root@localhost ~]# crontab -l
[root@localhost ~]# crontab -l
已经没了
3、user模块
user模块用于创建新用户和更改、删除已存在的用户。其中name选项用来指明创建的用户名称
user模块是请求的是useradd, userdel, usermod三个指令
查看user模块信息
ansible-doc -s user
创建用户shuai01
ansible mysql -m user -a 'name="shuai01"'
0.0.0.47 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1001,
"home": "/home/shuai01",
"name": "shuai01",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1001
}
查看用户信息
ansible mysql -m command -a 'grep "bash$" /etc/passwd'
20.0.0.47 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
liu:x:1000:1000:liu:/home/liu:/bin/bash
shuai01:x:1001:1001::/home/shuai01:/bin/bash '刚刚创建的'
'被管理者查看创建用户'
grep "bash$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
liu:x:1000:1000:liu:/home/liu:/bin/bash
shuai01:x:1001:1001::/home/shuai01:/bin/bash
删除用户shuai01
ansible mysql -m user -a 'name="shuai01" state=absent'
'查看用户'
ansible mysql -m command -a 'grep "bash$" /etc/passwd'
20.0.0.47 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
liu:x:1000:1000:liu:/home/liu:/bin/bash
4、group模块
group模块用于对用户组进行管理
group模块请求的是groupadd, groupdel, groupmod 三个指令
查看group模块信息
ansible-doc -s group
创建组mysql
ansible mysql -m group -a 'name=mysql gid=330 system=yes'
查看创建组
ansible mysql -a 'tail /etc/group'
20.0.0.47 | CHANGED | rc=0 >>
tcpdump:x:72:
liu:x:1000:liu
mysql:x:330: '创建的组'
创建用户shui02添加到组
ansible mysql -m user -a 'name=shuai02 uid=110 system=yes group=mysql'
'查看用户信息'
ansible mysql -a 'tail /etc/passwd'
20.0.0.47 | CHANGED | rc=0 >>
shuai02:x:110:330::/home/shuai02:/bin/bash
ansible mysql -a 'id shuai02'
20.0.0.47 | CHANGED | rc=0 >>
uid=110(shuai02) gid=330(mysql) 组=330(mysql)
5、copy模块
copy模块用于实现文件复制和批量下发文件。其中使用src来定义本地源文件路径,使用dest定义被管理主机文件路径,使用content则是通过指定信息内容来生成目标文件。
查看copy模块I信息
ansible-doc -s group
复制文件
将本地文件/etc/fstab复制到被管理主机上的/opt/fstab.bk,将所有者设置为root,权限设置为640.
ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.bk owner=root mode=640'
查看opt目录
ansible mysql -a 'ls /opt'
20.0.0.47 | CHANGED | rc=0 >>
fstab.bk
rh
查看fstab.bak文件
ansible mysql -a 'cat /opt/fstab.bk'
将heihei写入/opt/fstab.bk中
ansible mysql -m copy -a 'content="heihei" tom=/opt/fstab.bk'
'查看文件'
ansible mysql -a 'cat /opt/fstab.bk'
6、file模块
file模块来设置文件属性。其中使用path指定文件路径,使用src定义源文件路径,使用name或dest来替换创建文件的符号链接。
查看file模块信息
ansible-doc -s file
创建用户liu02
ansible mysql -m user -a 'name=liu02 system=yes'
创建组
ansible mysql -m group -a 'name=liu02 system=yes'
修改文件的属主属组权限
ansible mysql -m file -a 'owner=liu02 group=liu02 mode=644 path=/opt/fstab.bk'
'被管理者查看属主属组'
[root@localhost opt]# ll -h
总用量 4.0K
-rw-r--r--. 1 liu02 liu02 541 10月 16 15:22 fstab.bk
设置/opt/fatsb.link为/opt/fastb.bk的链接文件
ansible mysql -m file -a 'path=/opt/fstab.link src=/opt/fstab.bk state=link'
'查看一下'
ansible mysql -a 'ls /opt'
20.0.0.47 | CHANGED | rc=0 >>
fstab.bk
fstab.link
rh
删除一个文件
ansible mysql -m file -a 'path=/opt/fstab.bk state=absent'
'查看是否删除'
ansible mysql -a 'ls /opt'
20.0.0.47 | CHANGED | rc=0 >>
fstab.link
rh
创建一个文件test
ansible mysql -m file -a "path=/opt/test state=touch"
'已经创建成功'
ansible mysql -a 'ls /opt'
20.0.0.47 | CHANGED | rc=0 >>
fstab.link
rh
test
7、ping模块
ping,模块是用来检测指定主机的连通性。
查看ping模块的信息
ansible-doc -s ping
测试被管理者主机是否在线
ansible all -m ping
8、yum模块
yum模块复责在被管理主机上安装与卸载软件包,其中使用name指定要安装的软件包,使用state指定安装软件包的状态,present、latest用来表示安装,absent表示卸载。
查看yum模块信息
ansible-doc -s yum
安装zsh软件包
ansible mysql -m yum -a 'name=zsh'
查看zsh是否安装
ansible mysql -a 'rpm -q zsh'
Enter passphrase for key '/root/.ssh/id_rsa':
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'. If you need to use command
because yum, dnf or zypper is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.
20.0.0.47 | CHANGED | rc=0 >>
zsh-5.0.2-34.el7_8.2.x86_64
卸载ash软件包
ansible mysql -m yum -a 'name=zsh state=absent'
9、service模块
service模块用来控制管理服务的运行状态。其中enabled表示是否开机自启动,取值为true或false,使用name定义服务名称,使用state指定服务状态,取值分别为started、stoped、restarted.
查看service模块的信息
ansible-doc -s service
安装httpd服务
ansible mysql -m yum -a 'name=httpd'
'查询是否安装'
ansible mysql -a 'rpm -q httpd'
启动httpd服务并设置开机自启动
ansible mysql -m service -a 'enabled=true name=httpd state=started'
查看httpd服务状态
ansible mysql -a 'systemctl status httpd'
10、shell模块
shell模块可以在被管理主机上运行命令,并支持像管道符等功能的复杂命令。
创建用户使用无交互模式给用户设置密码
'创建用户niu'
ansible mysql -m user -a 'name=niu'
无交互给予用户niu设置密码
ansible mysql -m shell -a 'echo 123123|passwd --stdin niu'
Enter passphrase for key '/root/.ssh/id_rsa':
20.0.0.47 | CHANGED | rc=0 >>
更改用户 niu 的密码 。
passwd:所有的身份验证令牌已经成功更新。
11、script模块
script模块可以将本地脚本复制到被管理主机上进行运行。需要注意的是,使用相对路径来指定脚本。
编写一个本地脚本shuai.sh
vim /opt/shuai.sh
#!/bin/bash
echo "hello ansible from script"> /opt/script.txt
'增加执行权限'
chmod +s /opt/shuai.sh
'执行脚本'
ansible mysql -m script -a '/opt/shuai.sh'
'查看'
ansible mysql -a 'cat /opt/script.txt'
20.0.0.47 | CHANGED | rc=0 >>
hello ansible from script
12、setup模块
setup模块收集、查看被管理主机的facts。每个被管理主机在接并运行管理命令之前,都会将自己的相关信息(操作系统、IP地址)发送给控制主机。
查看mysql组的facts信息
ansible mysql -m setup
20.0.0.47 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"20.0.0.47",
"192.168.122.1"
],
"ansible_all_ipv6_addresses": [
"fe80::bc12:ac9e:842e:5fdd"
],
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64",
"ansible_bios_date": "07/29/2019",
"ansible_bios_version": "6.00",
"ansible_cmdline": {
.....省略信息.......
以上皆是Ansible常用的模块,如果用到其他模块可以使用ansible-doc工具。