ansible模块

ad-hoc

临时命令,执行完毕结束,不会保存
使用场景
在多台机器上查看某个进程是否启动,或拷贝指定文件到本地...
命令
ansible 'web' -m command -a 'df -h'

command 执行shell 的模块

[root@localhost project_1]# ansible web -m command -a "df -h" -i hosts 
192.168.0.178 | CHANGED | rc=0 >>
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root  8.0G  1.1G  7.0G   13% /
devtmpfs                 475M     0  475M    0% /dev
tmpfs                    487M     0  487M    0% /dev/shm
tmpfs                    487M  7.7M  479M    2% /run
tmpfs                    487M     0  487M    0% /sys/fs/cgroup
/dev/sda1               1014M  133M  882M   14% /boot
tmpfs                     98M     0   98M    0% /run/user/0
web1 | CHANGED | rc=0 >>
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root  8.0G  1.1G  7.0G   13% /
devtmpfs                 475M     0  475M    0% /dev
tmpfs                    487M     0  487M    0% /dev/shm
tmpfs                    487M  7.7M  479M    2% /run
tmpfs                    487M     0  487M    0% /sys/fs/cgroup
/dev/sda1               1014M  133M  882M   14% /boot
tmpfs                     98M     0   98M    0% /run/user/0

ansible web -m command -a "df -h" -i hosts -f 10
-f forks 并发数
使用 ad-hoc执行命令,注意返回结果的颜色
绿色 管理端主机没有被修改
黄色 被管理端主机发现变更
红色 有故障,注意提示

常用模块

yum模块(present安装 absent卸载 latest升级 exclude排除 指定仓库enablerepo)

ansible-doc yum
安装最新的Apache软件
ansible web -m yum -a "name=httpd state=latest" -i hosts
安装最新的apache软件,通过epel仓库安装
ansible web -m yum -a "name=httpd state=latest enablerepo=epel" -i hosts
通过url安装rpm软件
ansible web -m yum -a "name=https://mirrors.cnnic.cn/zabbix/zabbix/5.2/rhel/7/x86_64/zabbix-agent-5.2.7-1.el7.x86_64.rpm state=latest " -i hosts
更新所有软件包,排除kernel foo相关的
ansible web -m yum -a "name=* state=latest exclude=kernel,foo" -i hosts
删除apache软件
ansible web -m yum -a "name=hjttpd state=absent" -i hosts
安装软件
state=present 也可以

copy模块(src dest owner group mode backup content)

修改apache的配置文件 修改端口

[root@localhost project_1]# rpm -qc httpd

找到被控端配置文件

将配置文件传输到主控端
scp /etc/httpd/conf/httpd.conf root@ip:路径
在主控端修改好端口,推送到远端服务(owner属主 group属组 mode权限)
ansible web -m copy -a "src=/root/project_1/httpd.conf dest=/etc/httpd/conf/httpd.conf owner=root group=root mode=644" -i hosts
推送远端服务开启备份(有改动就会产生备份 backup=yes)
ansible web -m copy -a "src=/root/project_1/httpd.conf dest=/etc/httpd/conf/httpd.conf owner=root group=root mode=644 backup=yes" -i hosts
往远程主机写入文件
ansible web -m copy -a "content=Httpserver... dest=/var/www/html/index.html" -i hosts 5

get_url 模块(force 是否覆盖本地 checksum=md5: 校验MD5)

下载文件到本地
ansible web -m get_url -a "url=http://fj.xuliangwei.com/public/ip.txt dest=/var/www/html/ip.txt force=no" -i hosts
需要添加登录名密码的网站,使用url_password、url_username参数
下载文件进行md5校验
md5sum 拿到文件的MD5值
ansible web -m get_url -a "url=http://fj.xuliangwei.com/public/ip.txt dest=/var/www/html/ip.txt checksum=md5:md5值" -i hosts

file模块 创建 授权

创建文件,设定属主,属组,权限
ansible web -m file -a "path=/var/www/html/zz.html state=touch owner=apache group=apache mode=644" -i hosts
创建目录
ansible web -m file -a "path=/var/www/html/dd state=directory owner=apache group=apache mode=755" -i hosts
递归授权目录(recurse 递归)
ansible web -m file -a "path=/var/www/html/ owner=apache group=apache recurse=yes" -i hosts

service模块

重启httpd服务
ansible web -m service -a "name=httpd state=restarted" -i hosts
state:reloaded, restarted, started, stopped,
加入开机自启
ansible web -m service -a "name=httpd state=started enabled=yes" -i hosts
重启网卡
ansible web -m service -a "name=network stats=restarted args=eth0" -i hosts

group

创建news基本组,指定gid为999
ansible web -m group -a "name=news gid=9999 state=present" -i hosts
创建http系统组,指定gid为8888
ansible web -m group -a "name=http gid=8888 system=yes state=present" systemc=true 也行
删除组
ansible web -m group -a "name=news state=present" -i hosts

user

创建joh用户,uid1040,adm组
ansible web -m user -a "name=joh uid=1040 group=adm" -i hosts

创建用户,登录shell是/sbin/nologin,追加bin,sys两个组
ansible web -m user -a "name=john shell=/sbin/nologin groups=bin,sys" -i hosts
创建用户,添加登录密码,创建家目录(ansible不认明文的密码)
ansible localhost -m debug -a "msg={{'1234' | password_hash('sha512','salt')}}
先加密
ansible web -m user -a 'name=jsm password=$6$salt$7PXWnHTsTMXzlf9gxFI7b3wPc2v5fBrRHlMlmQQ4HMUoUWbOIfEiuGYXxWwpQYW6.1fTRyuCquk.4L3n9Td6C1 create_home=yes' -i hosts
移除用户
ansible web -m user -a "name=joh state=absent" -i hosts # remove=yes 删除家目录
创建用户 为用户创建2048字节的私钥,存放在~/http/.ssh/id_rsa
ansible web -m user -a "name=httpd generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa" -i hosts

corn

添加定时任务,每分钟执行一次
nsible web -m cron -a "name=job1 job='ls /dev/null'" -i hosts
添加定时任务,每天凌晨2点和5点各执行一次
ansible web -m cron -a "name=job2 job='ls /dev/null' minute=0 hour=2,5" -i hosts
关闭定时任务,让定时任务失效
ansible web -m cron -a "name=job2 job='ls /dev/null' minute=0 hour=2,5 disabled=yes" -i hosts

mount

posted on 2023-06-26 15:24  sprr  阅读(9)  评论(0编辑  收藏  举报