command模块和shell模块的区别
- command模块命令不启动shell解释器,直接通过ssh执行命令
- command不支持bash特性(不支持管道,重定向等功能)
- 所有需要调用shell的功能都无法使用
shell模块不能使用交互式命令:vim ,top 等
使用chdir参数切换工作目录
[root@control ~]# ansible node1 -m shell -a "chdir=/tmp touch my.txt"
如果已经有秘钥文件id_rsa,则不创建秘钥
creates 文件名 :文件存在,不执行shell命令
removes 文件名:文件不存在,不执行shell命令
[root@control ~]# ansible test -m shell -a "ssh-keygen -f ~/.ssh/id_rsa -N '' creates=~/.ssh/id_rsa"
如果没有unzip软件包,则不执行解压命令
[root@control ~]# ansible test -m shell -a "unzip xx.zip removes=/bin/unzip"
script模块
- 允许本地写脚本,拷贝到被管理端并执行脚本
- 脚本可以是任何脚本(shell脚本,python脚本,perl 脚本),可以没有-x权限(执行权限)
写一个给远程主机安装http服务的脚本,并把服务启动
[root@localhost ansible]# vim test.sh
#!/bin/bash
yum -y install httpd
systemctl start httpd
使用webserver执行test.sh脚本
[root@localhost ansible]# ansible webserver -m script -a "./test.sh"
验证:被控机
file模块 (创建文件,修改权限)
可以创建文件、目录、链接;修改权限与属性
创建文件file.txt
state的值可以有:
absent 【删除】
directory 【新建目录】
hard 【硬链接】
link 【新建快捷方式】【软连接】
touch 【创建空文件】
[root@localhost ansible]# ansible webserver -m file -a "path=/tmp/file.txt state=touch"
创建目录file
[root@localhost ansible]# ansible webserver -m file -a "path=/tmp/file state=directory"
修改文件或目录权限
path=/tmp/file.txt 文件或目录名
owner=sshd 修改文件所有者
group=adm 修改文件或目录的组
mode=0777 修改文件或目录权限
[root@localhost ansible]# ansible webserver -m file -a "path=/tmp/file.txt owner=sshd group=adm mode=0777"
给/etc/host.txt文件创建一个链接文件/tmp/host.txt
[root@localhost ansible]# ansible webserver -m file -a "src=/etc/hosts path=/tmp/host.txt state=link"
copy模块 【拷贝(推送)文件】
backup=yes #如果目标主机有同名文件,则先备份
content 【插入内容】
dest 【被控机位置】
通过content可以直接提供文件内容,默认不回车,\n表示回车
[root@localhost ansible]# ansible webserver -m copy -a "content='hello world\n' dest=/tmp/new.txt"
幂等性:任意执行所产生的影响与第一次执行的影响相同。
1的1次方是1
1的2次方是1
1的3次方是1
1的4次方是1
1的5次方是1
1的6次方是1
1的7次方是1
1的8次方是1
1的9次方是1
重复同样的操作,不会执行。体现幂等性
fetch模块 【拉取文件】
与copy类似,但是作用相反
可以将被控机的文件拷贝到本地
[root@localhost ansible]# ansible webserver -m fetch -a "src=/etc/passwd dest=a44.txt"
lineinfile模块 【替换一整行文本】
在修改单个文件单行内容时可以使用Lineinfile
line 【插入内容】
insertafter 【插入位置】
[root@localhost ansible]# ansible webserver -m lineinfile -a "path=/etc/issue line='hello word'"
[root@localhost ansible]# ansible webserver -m lineinfile -a "path=/etc/issue line='insert' insertafter='Kernel'"
regexp 【正则匹配某一行】【全行替换】
[root@localhost ansible]# ansible webserver -m lineinfile -a "path=/etc/issue regexp='hello' line='ni hao'"
在/etc/issue文件中正则匹配包含hello行,把整行内容替换为ni hao
如果无法匹配到hello,则在文件最后一行添加ni hao
如果有多行内容包含hello,则替换最后一行
replace模块 【替换关键字】
[root@localhost ansible]# ansible webserver -m replace -a "path=/etc/issue regexp=insert replace=study"
将/etc/issue文件全文所有的insert替换为study
user模块 【实现Linux系统账户管理】
远程webserver组中的所有主机并创建系统账户user01,并设置对应的账户属性
[root@localhost ansible]# ansible webserver -m user -a "name=user01 uid=1010 group=adm groups=daemon,root home=/home/user01"
修改账户密码
[root@localhost ansible]# ansible webserver -m user -a "name=user01 password={{'123456'|password_hash('sha512')}}"
删除账户user01
state=absent 删除用户
状态=删除
[root@localhost ansible]# ansible webserver -m user -a "name=user01 state=absent"
删除user01账户同时删除家目录和邮箱等
remove=true
[root@localhost ansible]# ansible webserver -m user -a "name=user01 state=absent remove=true"
yum_repository模块 【创建或修改yum源配置文件】
ansible webserver -m yum_repository -a "name=myyum description=hello baseurl=file://media gpgcheck=no"
yum模块 【安装,卸载,升级软件包】
安装 present
ansible webserver -m yum -a "name=unzip state=present"
升级 latest
ansible webserver -m yum -a "name=unzip state=latest"
卸载 absent
ansible webserver -m yum -a "name=unzip state=absent"
service模块 【服务管理模块(启动·关闭·重启)】
启动 started
ansible webserver -m service -a "name=httpd state=started"
查看http服务状态
systemctl status httpd
关闭 stopped
ansible webserver -m service -a "name=httpd state=stopped"
重启 restarted
ansible webserver -m service -a "name=httpd state=restarted"
enabled=yes #设置开机自启动
ansible webserver -m service -a "name=httpd state=started enabled=yes"
逻辑卷相关模块
lvg模块:创建、删除卷组VG,修改卷组大小
lvol模块:创建、删除逻辑卷,修改
手动硬盘分区
[root@node1 ~]# yum install -y parted
[root@node1 ~]# parted /dev/sdb mklabel gpt
警告: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue?
是/Yes/否/No? yes
信息: You may need to update /etc/fstab.
[root@node1 ~]# parted /dev/sdb mkpart primary 1 50%
信息: You may need to update /etc/fstab.
[root@node1 ~]# parted /dev/sdb mkpart primary 50% 100%
信息: You may need to update /etc/fstab.
[root@node1 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 199G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 3.9G 0 lvm [SWAP]
└─centos-home 253:2 0 145.1G 0 lvm /home
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 10G 0 part
└─sdb2 8:18 0 10G 0 part
sr0 11:0 1 4.4G 0 rom
安装lvm2软件包
ansible webserver -m yum -a "name=lvm2"
创建名称为myvg的卷组,该卷组由/dev/sdb1组成
ansible webserver -m lvg -a "vg=myvg pvs=/dev/sdb1"
使用myvg这个卷组创建一个名称为mylv的逻辑卷
ansible webserver -m lvol -a "lv=mylv vg=myvg size=2G"
删除逻辑卷必须使用强制删除force=yes才能成功
state:present(创建)
absent(删除)
ansible webserver -m lvol -a "lv=mylv vg=myvg state=absent force=yes"
删除卷组myvg
ansible webserver -m lvg -a "vg=myvg state=absent force=yes"
他山之石,可以攻玉。