ansible自动化运维,模块小结(二)
1.命令和脚本模块小结
-
command 模块用于执行简易的命令,不包含特殊符号,管道,重定向,通配符
-
举例
#默认模块,可不添加,不支持特殊符号,管道,重定向,通配符
[root@m01 ~]# ansible data -m command -a 'hostname'
[root@m01 ~]# ansible data -a 'hostname' -
-
shell 与command 类似, 支持含特殊符号,管道,重定向,通配符
-
举例
[root@m01 ~]# ansible data -m shell -a "ifconfig|grep eth0" -
-
script 分发脚本并执行脚本
-
举例
ansible nfs -m script -a '/server/scripts/yum.sh' -
2.软件管理模块
-
yum name 软件名字,软件名+版本 state
present installed 安装 absent removed 删除 latest 更新
-
举例
ansible backup -m yum -a 'name=nginx state=present' -
-
yum_repository file=xxxx /etc/yum.repos.d/xxxx.repo name description baseurl #yum源下载地址 state=present或absent enabled=yes gpgcheck=yes gpgkey
-
举例
ansible backup -m yum_repository -a 'file=nginx name=nginx-stable description="nginx yum repo" baseurl="http://nginx.org/packages/centos/$releasever/$basearch/" enabled=yes gpgcheck=no ' -
-
ansible backup -m yum -a 'name=nginx state=present'
3.文件相关模块
3.1file
-
file 创建、删除 文件/目录/软链接 path= 指定文件、目录 (类似于dest) state= directory 目录 touch 文件 link 软链接 owner group mode recurse 递归
#例
#创建文件
ansible all -m file -a "path=/tmp/oldboy.txt state=touch"
ansible all -a 'ls -l /tmp/oldboy.txt'
#创建目录
ansible all -m file -a "path=/tmp/oldboy/a/b/c/ state=directory"
ansible all -a 'tree /tmp/oldboy'
ansible all -m file -a "path=/tmp/oldboy/lidao/a/b/d/d/oldboy.txt state=directory" #创建目录
ansible all -a 'tree /tmp/oldboy'
ansible all -a 'tree -F /tmp/oldboy'
ansible all -m file -a "path=/tmp/old/dao/a/b/d/d/oldboy.txt state=touch" #创建文件
#创建链接文件(软链接)
ansible all -m file -a "src=/etc/hosts path=/tmp/hosts.soft state=link"
ansible all -a 'll /tmp/hosts.soft' #错误
ansible all -a 'ls -l /tmp/hosts.soft'#把 web服务器 wordpress 代码修改为www.www
# recurse 递归修改属主属组
[root@m01 scripts]# ansible web -m file -a 'path=/data/blog/ owner=www group=www recurse=yes state=directory '
172.16.1.7 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 1111,
"group": "www",
"mode": "0755",
"owner": "www",
"path": "/data/blog/",
"size": 4096,
"state": "directory",
"uid": 1111
}
3.2copy
-
copy 远程拷贝(分发文件、目录) src 源 dest 目标 backup 如果目标存在则备份(文件大则不要备份)
owner group mode 权限
content 写入内容 与dest一起使用
#批量分发 hosts文件
cp /etc/hosts /server/
ansible all -m copy -a 'src=/server/hosts dest=/etc/hosts backup=yes'
ansible all -a 'tail -2 /etc/hosts'
ansible all -a 'head -20 /etc/hosts*'
ansible all -m shell -a 'head -20 /etc/hosts*'
ansible all -m shell -a 'head -20 /etc/hosts*~'
ansible all -m shell -a 'cat /etc/hosts*~'
#批量分发 目录
ansible all -m copy -a 'src=/etc/yum.repos.d dest=/tmp/ '
ansible all -a 'ls -l /tmp/'
ansible all -m shell -a 'ls -l /tmp/yum.repos.d/'
#复制代码的时候 修改代码的所有者 www www
ansible all -m copy -a 'src=/data/blog/ dest=/tmp/ owner=www group=www '
3.3 get_url
-
get_url url dest
url_get
ansible all -m url_get -a 'url=https://mirrors.tuna.tsinghua.edu.cn/gnu/gawk/gawk-5.1.0.tar.gz dest=/tmp/'
4 .服务模块
4.1 systemctl
###systemd (c7,c8)
开启或关闭 服务
ansible all -m systemd -a 'name=crond state=stopped'
ansible all -m systemd -a 'name=crond state=started'
ansible all -m systemd -a 'name=crond state=reloaded或restarted'
###开机自启动
ansible all -m systemd -a 'name=crond enabled=yes '
###开机自启动并启动服务
ansible all -m systemd -a 'name=crond enabled=yes state=started'
daemon_reload 未来我们修改了 systemctl对应的配置的时候 需要执行
4.2 service
#C5.C6
5. 模块:用户相关模块
-
group
-
user
#01 添加用户rsync,指定他的uid和gid 999 虚拟用户
##1)添加用户组 rsync gid 999
groupadd -g 999 rsync
##2)添加用户 rsync uid 999 组是rsync -s /sbin/nologin -M
useradd -u 999 -g rsync -s /sbin/nologin -M rsync
[root@m01 ~]# ansible db -m group -a 'name=rsync2 gid=10086 state=present'
172.16.1.51 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 10086,
"name": "rsync2",
"state": "present",
"system": false
}
[root@m01 ~]# ansible db -m user -a 'name=rsync2 uid=10086 group=rsync2 shell=/sbin/nologin create_home=no state=present'
172.16.1.51 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": false,
"group": 10086,
"home": "/home/rsync2",
"name": "rsync2",
"shell": "/sbin/nologin",
"state": "present",
"stderr": "正在创建信箱文件: 文件已存在\n",
"stderr_lines": [
"正在创建信箱文件: 文件已存在"
],
"system": false,
"uid": 10086
}
[root@m01 ~]#
[root@m01 ~]# ansible db -a 'id rsync2'
172.16.1.51 | CHANGED | rc=0 >>
uid=10086(rsync2) gid=10086(rsync2) 组=10086(rsync2)
[root@m01 ~]# ansible db -a 'grep rsync2 /etc/passwd'
172.16.1.51 | CHANGED | rc=0 >>
rsync2:x:10086:10086::/home/rsync2:/sbin/nologin
6. 模块:定时任务相关模块
| cron模块 | Linux定时任务内容 | cron模块格式 |
|---|---|---|
| 注释说明 | #this is backup scripts by lidao996 at 20211111 | name="his is backup..." |
| 分 | 00 | minute=00 |
| 时 | 00 | hour=00 |
| 日 | * | day=* (如果是*号可以不写) |
| 月 | * | month=* |
| 周 | * | weekday=* |
| 指令、脚本 | sh /sum.sh &>/dev/null | job="sh /sum.sh &>/dev/null" |
| 状态 | state=present(默认)|absent |
ansible all -a 'crontab -l'
ansible all -m cron -a 'name="print name to file" minute="*/3" job="echo oldboy &>>/tmp/oldboy.txt" state=present '
ansible all -a 'crontab -l'
ansible all -m cron -a 'name="print name to file" state=absent '
ansible all -a 'crontab -l'
#注意事项:
##01 一定要指定name
##02 不使用的定时任务,可以disabled注释掉。
#垃圾箱
alias rm='mv -t /tmp/ $*'
| cron模块格式 |
|---|
| name="his is backup..." |
| minute=00 |
| hour=00 |
| day=* (如果是*号可以不写) |
| month=* |
| weekday=* |
| job="sh /sum.sh &>/dev/null" |
| state=present(默认)|absent |
| disabled是否注释只有 state=present才会注释 |
7. 模块:磁盘挂载相关模块
#01 把nfs上面的共享挂载到 /mnt目录下面
##01)安装nfs
ansible all -m yum -a 'name=nfs-utils state=present'
##02)挂载nfs
ansible all -m mount -a 'fstype=nfs src=172.16.1.31:/data/zh path=/mnt/lidao-new-nfs-mount--help/a/b/c/d/f/ state=mounted'
#02 注意事项:
## mount 模块中 state 是present 是只修改/etc/fstab
## mount 模块中 state 是mounted 是挂载并修改/etc/fstab
| mount模块 | mount命令 | mount模块 |
|---|---|---|
| 指定文件系统类型 | -t nfs | fstype=nfs |
| 源 | 172.16.1.31:/data/zh | src=172.16.1.31:/data/zh |
| 目标 | /mnt | path=/mnt/new-lidao-mount |
| 状态 | state=present|mounted absent|unmounted remounted |
present # 仅修改配置 开机挂载,仅将挂载配置写入/etc/fstab
mounted # 挂载+修改配置 挂载设备,并将配置写入/etc/fstab
unmounted # 卸载设备,不会清除/etc/fstab写入的配置
absent # 卸载设备,会清理/etc/fstab写入的配置
remounted #重新挂载
8. 模块:防火墙模块
| iptables模块 | iptable命令 | iptables模块 |
|---|---|---|
| 指定表 | -t filter | table=filter |
| action=append(默认)或insert | ||
| 指定链 | -I INPUT | chain=INPUT |
| 源ip | -s 10.0.0.0/24 | source=10.0.0.0/24 |
| 目标ip | -d | destination |
| 协议 | -p tcp | protocol=tcp |
| 源端口 | ||
| 目标端口 | --dport 3306 | destination_port=3306 |
| 策略 | -j DROP | jump=DROP |
| 状态 | state=absent, present(默认) |
#安装iptables
ansible web -m yum -a 'name=iptables-services '
#启动iptables
ansible web -m systemd -a 'name=iptables state=started enabled=no'
#配置使用
#iptables -t filter -I INPUT -s 10.0.0.0/24 -p tcp --dport 3306 -j DROP
ansible web -m iptables -a 'table=filter action=insert chain=INPUT source=10.0.0.0/24 protocol=tcp destination_port=80 jump=DROP state=present'
ansible web -m iptables -a 'table=filter action=insert chain=INPUT source=10.0.0.0/24 protocol=tcp destination_port=80 jump=DROP state=absent'
| 分类 | 模块名字 |
|---|---|
| 命令: | commnad , shell , script |
| 文件: | file,copy,get_url |
| 软件包: | yum,yum_repository |
| 服务: | systemd,service |
| 用户 | user,group |
| 磁盘 | mount |
| 定时任务 | cron |
| 防火墙 | iptables |

浙公网安备 33010602011771号