ansible常用模块

发布时间 2023-08-15 21:23:01作者: 村尚chun叔

ansible常用模块

ansible提供了多少个模块给你用
ansible-doc -l |wc -l

ping模块

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ping_module.html#ansible-collections-ansible-builtin-ping-module

测试所有组的机器连通性
ansible all -m ping

host模块

修改远程主机名
ansible 192.168.10.107  -m hostname -a 'name=node1'

file模块

https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module

创建目录
ansible group1 -m file -a 'path=/test state=directory'

创建文件
ansible group1 -m file -a 'path=/test/111 state=touch'

递归修改owner,group,mode
ansible group1 -m file -a 'path=/test recurse=yes owner=ftp group=daemon mode=1777'

删除目录
ansible group1 -m file -a 'path=/test state=absent'

创建文件并指定owner,group,mode等
ansible group1 -m file -a 'path=/tmp/111 state=touch owner=ftp group=daemon mode=1777'

删除文件
ansible group1 -m file -a 'path=/test/111 state=absent'

创建软连接文件
ansible group1 -m file -a 'src=/etc/fstab path=/tmp/fstab state=link'

创建硬链接文件
ansible group1 -m file -a 'src=/etc/fstab path=/tmp/fstab state=hard'

copy模块

在master上准备一个文件,拷贝此文件到group1的所有机器上
echo master > /tmp/222
ansible group1 -m copy -a 'src=/tmp/222 dest=/tmp/333'

如果目标文件已经存在,则不覆盖
ansible group1 -m copy -a 'src=/tmp/222 dest=/tmp/333 force=no'

如果目标文件已经存在,则会强制覆盖
ansible group1 -m copy -a 'src=/tmp/222 dest=/tmp/333 force=yes'

使用backup参数控制是否备份文件
ansible group1 -m copy -a 'src=/etc/fstab dest=/tmp/333 backup=yes'

/etc/yum.repos.d后面不带/符号,则表示把/etc/yum.repos.d整个目录拷贝到/tmp/目录下
ansible group1 -m copy -a 'src=/etc/yum.repos.d dest=/tmp/'

/etc/yum.repos.d/后面带/符号,则表示把/etc/yum.repos.d/目录里的所有文件拷贝到/tmp/目录下
ansible group1 -m copy -a 'src=/etc/yum.repos.d/ dest=/tmp/'

fetch模块

fetch模块把远程机器的文件拷贝到本地,不能从远程拷贝目录到本地
ansible group1  -m fetch -a 'src=/tmp/1.txt dest=/tmp/'

user模块

https://docs.ansible.com/ansible/latest/modules/user_module.html#user-module

user模块用于管理用户账号和用户属性,创建aaa用户,默认为普通用户,创建家目录
ansible group1 -m user -a ‘name=aaa state=present’

创建bbb系统用户,并且登录shell环境为/sbin/nologin
ansible group1 -m user -a 'name=bbb state=present system=yes  shell="/sbin/nologin" '

删除bbb用户,使用remove=yes参数让其删除用户的同时也删除家目录
ansible group1 -m user -a 'name=bbb state=absent remove=yes'
模块参数 参数描述
create_home 创建家目录,设置no则不创建家目录
group 创建用户组
name 创建用户的名字
password 创建用户的密码
uid 创建用户的UID
shell 用户登录解释器
state Absent(删除用户)present(默认参数,创建)
expires 账户过期时间

group模块

https://docs.ansible.com/ansible/latest/modules/group_module.html#group-

group模块用于管理用户组和用户组属性。
ansible group1 -m group -a 'name=groupa gid=3000 state=present'

删除组(如果有用户的gid为此组,则删除不了)
2ansible group1 -m group -a 'name=groupa state=absent'

cron模块

https://docs.ansible.com/ansible/latest/modules/cron_module.html#cron-module

cron模块用于管理周期性时间任务
创建一个cron任务,不指定user的话,默认就是root。
如果minute,hour,day,month,week不指定的话,默认都为*
ansible group1 -m cron -a 'name="test cron1" user=root job="touch /tmp/111" minute=*/2' 

删除cron任务
ansible group1 -m cron -a 'name="test cron1" state=absent'

yum_repository模块

yum_repository模块用于配置yum仓库,增加一个/etc/yum.repos.d/local.repo配置文件
ansible group1 -m yum_repository -a "name=local description=localyum baseurl=file:///mnt/ enabled=yes gpgcheck=no"

删除/etc/yum.repos.d/local.repo配置文件
ansible group1 -m yum_repository -a "name=local state=absent" 

yum模块

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/yum_module.html#examples

yum模块用于使用yum命令来实现软件包的安装与卸载
使用yum安装httpd,httpd-devel软件,state=latest表示安装最新版本
ansible group1 -m yum -a 'name=httpd,httpd-devel state=latest' 

使用yum卸载httpd,httpd-devel软件
ansible group1 -m yum -a 'name=httpd,httpd-devel state=absent' 

service/systemd模块

service适用于centos6前的系统
systemd命令应用于centos7系统

service模块用于控制服务的启动,关闭,开机自启动等。
启动vsftpd服务,并设为开机自动启动
ansible group1 -m service -a 'name=vsftpd state=started enabled=on'

关闭vsftpd服务,并设为开机不自动启动
ansible group1 -m service -a 'name=vsftpd state=stopped enabled=false'
如果使用systemctl 管理程序的话,可以使用systemd模块,systemctl 可以 控制程序启/停,reload,开机启动,观察程序状态(status)等,掌握使用后管理就更方便了

主要参数
daemon_reload:在执行任何其他操作之前运行守护进程重新加载,以确保systemd已经读取其他更改
enabled:服务是否开机自动启动yes|no。enabled和state至少要有一个被定义
masked:是否将服务设置为masked状态,被mask的服务是无法启动的
name:必选项,服务名称
no_block(2.3后新增):不要同步等待操作请求完成
state:对当前服务执行启动,停止、重启、重新加载等操作(started,stopped,restarted,reloaded)
user:使用服务的调用者运行systemctl,而不是系统的服务管理者

练习安装、启动nginx服务

1.安装nginx服务
[root@master-61 ~]#ansible web -m yum -a "name=nginx state=installed"

2.启动服务
[root@master-61 ~]#ansible web -m systemd -a "name=nginx state=started"

3.查询状态,这里ansible未直接提供status参数,你可以借助command模块即可
[root@master-61 ~]#ansible web -a "systemctl status nginx"

4.停止nginx服务
[root@master-61 ~]#ansible web -m systemd -a "name=nginx state=stopped"

5.设置nginx开机自启
[root@master-61 ~]#ansible web -m systemd -a "name=nginx state=started enabled=yes"

6.检查nginx状态
[root@master-61 ~]#ansible web -a "systemctl is-enabled nginx"

[root@master-61 ~]#ansible web -a "systemctl status nginx"

7.关闭开机自启、且停止服务
[root@master-61 ~]#ansible web -m systemd -a "name=nginx state=stopped  enabled=no"

8.再次检查状态
[root@master-61 ~]#ansible web  -m shell -a "systemctl is-enabled nginx;systemctl status nginx"

script模块

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/script_module.html#ansible-collections-ansible-builtin-script-module

script模块用于在远程机器上执行本地脚本。
在master上准备一个脚本
master# vim /tmp/1.sh
#!/bin/bash
mkdir /tmp/haha
touch /tmp/haha/{1..10}
在group1的远程机器里都执行master上的/tmp/1.sh脚本(此脚本不用给执行权限)
ansible group1 -m script -a '/tmp/1.sh'
选项参数 选项说明
creates 定义一个文件是否存在,若不存在,则运行相应命令;存在则跳过
free_form(必须) 参数信息中可以输入任何系统命令,实现远程管理
removes 定义一个文件是否存在,如果存在,则运行相应命令;如果不存在则跳过

mount模块

https://docs.ansible.com/ansible/latest/collections/ansible/posix/mount_module.html#mount

给web机器挂载nfs目录(只写入/etc/fstab而不挂载)
ansible web -m mount -a "src='172.16.1.1:/nfs-nginx-data' path=/usr/share/nginx/html fstype=nfs state=present"

给web机器挂载nfs目录(立即挂载且写入/etc/fstab)
ansible web -m mount -a "src='172.16.1.31:/nfs-nginx-data' path=/usr/share/nginx/html fstype=nfs state=mounted"

取消挂载,以及删除fstab记录
ansible web -m mount -a "src='172.16.1.31:/nfs-nginx-data' path=/usr/share/nginx/html fstype=nfs state=absent"

取消挂载,不删除fstab记录
ansible web -m mount -a "src='172.16.1.31:/nfs-nginx-data' path=/usr/share/nginx/html fstype=nfs state=umounted"
参数总结
mounted 挂载设备且写入fstab,创建挂载点
present 仅写入fstab 不立即挂载
absent  卸载且删除fstab记录,删除挂载点
umounted 只卸载不删除fstab记录

archive模块

https://docs.ansible.com/ansible/latest/collections/community/general/archive_module.html#examples

支持压缩类型
bz2
gz ← (default)
tar
xz
zip

指定format即可
压缩/etc配置文件到指定路径
ansible web -m archive -a "path=/etc dest=/opt/etc.tgz"

压缩/var/log为zip类型到指定路径
ansible web -m archive -a "path=/var/log dest=/opt/log.zip format=zip"

unarchive模块

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/unarchive_module.html#examples

unarchive模块是远程解压缩,而不是在本机直接解压缩

指定目录必须存在
ansible web -m file -a "path=/opt/etc_file state=directory"

解压缩
[root@master-61 ~]#ansible web -m unarchive -a "src=/opt/etc.tgz dest=/opt/etc_file/ remote_src=yes"
将管理机的压缩包,解压到远程机器上
1.生成etc.tgz数据
[root@master-61 ~]#cd / && tar -zcf /opt/etc.tgz etc


2.远程解压到web机器上
[root@master-61 /]#ansible web -m unarchive -a "src=/opt/etc.tgz  dest=/tmp/"