setup模块(查看系统自带变量)
##只查看一台主机的信息
ansible gyh -m setup --limit 192.168.35.11
command模块
ping模块
shell模块
ansible php -m shell "rm -rf /tmp/*"
copy模块(复制)
##将本地主机的文件复制到gyh组内的其他远程主机上
ansible gyh -m copy "src=/root/a.txt dest=/root"
##加上remote_src表示远程主机自己把本地的a.txt复制到/tmp下
ansible gyh -m copy "src=/root/a.txt dest=/tmp remote_src=yes"
template模块(也可以实现复制,但会将文件中的变量名字替换为变量的值,查看变量用setup模块)
ansible gyh -m template -a "src=/root/a.txt dest=/root"
ansible gyh -m template -a "src=/root/a.txt dest=/tmp remote_src=yes"
file模块
ansible gyh -m file "path=/tmp/* state=absent"
这样是删除不了的,*的问题。
yum模块
##安装
ansible gyh -m yum -a "name=apr* state=present"
ansible gyh -m yum -a "name=httpd state=present"
##卸载
ansible gyh -m yum -a "name=mariadb state=absent"
service模块(用于启动和关闭服务)
ansible gyh -m service -a "name=httpd state=started"
ansible gyh -m service -a "name=httpd state=stopped"
ansible gyh -m service -a "name=httpd state=restarted"
##用shell模块查看服务状态
ansible gyh -m shell -a "ss -anpt |grep httpd"
user模块
#将明文密码加密
openssl passwd -crypt 123.com
#得到的结果
h2RLQiLzjyVnk
#创建
ansible gyh -m user -a "name=aaa password=h2RLQiLzjyVnk state=present"
#/sbin/nologin可以有效地禁止该用户登录系统,但仍然可以保留其他权限和功能,比如允许用户通过 FTP 访问文件、执行特定的服务或应用等
ansible gyh -m user -a "name=aaa password=h2RLQiLzjyVnk state=present shell=/sbin/nologin"
#没有删除宿主目录
ansible gyh -m user -a "name=aaa state=absent"
#删除宿主目录
ansible gyh -m user -a "name=aaa state=absent remove=yes"
centos用户id
0 root
1-199 系统自带用户
2-999 程序用户
给软件提供访问文件的权限,当一个程序想要访问文件时,必须通过程序用户获取权限
1000-60000 普通用户
磁盘分区
parted模块(磁盘分区)
device:要分区的模块
number:分区号
part_start:开始位置,默认是磁盘起始扇区
part_end:结束位置,默认是磁盘最后扇区
10GiB=1024MiB
10GB=1000MB
ansible gyh -m parted -a "device=/dev/sdb part_type=primary number=2 part_end=5GiB state=present"
ansible gyh -m parted -a "device=/dev/sdb part_type=primary number=2 part_start=5GiB part_end=10GiB state=present"
ansible gyh -m parted -a "device=/dev/sdb part_type=extended number=3 part_start=10GiB state=present"
#逻辑分区从5开始,
ansible gyh -m parted -a "device=/dev/sdb part_type=logical number=5 part_start=11GiB part_end=15GiB state=present"
ansible gyh -m parted -a "device=/dev/sdb part_type=logical number=5 part_start=16GiB part_end=19GiB state=present"
512字节只能记录5个主分区,并且mbr硬盘不能超过2T
filesystem模块
ansible gyh -m shell -a "mkswqp /dev/sdb6"
ansible gyh -m shell -a "swapon /dev/sdb6"
ansible gyh -m filesystem -a "device=/dev/sdb1 fstype=xfs"
ansible gyh -m filesystem -a "device=/dev/sdb2 fstype=xfs"
mount模块
#没有立即生效,需要重启或者手动挂载
ansible gyh -m mount -a "src=/dev/sdb1 path=/sdb1 state=present fstype=xfs"
#手动挂载
ansible gyh -m shell -a "mount /dev/sdb1 /sdb1"
ansible gyh -m lvg -a "vg=test pvs=/dev/sdc,/dev/sdd state=present"
ansible gyh -m lvol -a "vg=test lv=test1 size=30g state=present"
ansible gyh -m filesystem -a "device=/dev/test/test1 fstype=xfs"
ansible gyh -m file -a "path=/sdb state=directory"
ansible gyh -m mount -a "src=/dev/test/test1 path=/sdb state=present fstype=xfs"
ansible gyh -m shell -a "mount /dev/test/test1 /sdb"
ansible gyh -m lvg -a "vg=test pvs=/dev/sdc,/dev/sdd,/dev/sde state=present"
ansible gyh -m lvol -a "vg=test lv=test1 size=50g state=present"
ansible gyh -m shell -a "df -hT /sdb"
ansible gyh -m shell -a "xfs_growfs /dev/test/test1"
ansible gyh -m shell -a "df -hT /sdb"