一、命令模块
1.command模块
命令模块 适合使用简单的命令 无法支持"<" ,">" ,"|" ,";" ,"&" 等符号
官方文档:https://docs.ansible.com/ansible/latest/modules/command_module.html
参数
选项/默认值
释义
chdir
在执行命令前,进入到指定目录中
creates
判断指定文件是否存在,如果存在,不执行后面的操作
removes
判断指定文件是否存在,如果存在,执行后面的操作
free_form
必须要输入一个合理的命令
[root@m01 ~ ]# ansible 'web01' - m command - a 'free -m'
web01 | CHANGED | rc= 0 >>
total used free shared buff/ cache available
Mem: 972 110 460 19 401 667
Swap: 2047 0 2047
#1. command命令不支持特殊符号
[root@m01 ~ ]# ansible 'web01' - m command - a "ifconfig eth0 | awk 'NR==2 {print $2}'"
web01 | FAILED | rc= 1 >>
| : Unknown host
ifconfig: `
ps:当ansible命令没指定模块时,默认使用command模块
#2. chdir
[root@m01 ~ ]# ansible dkaiyun - m command - a "chdir=/data ls -l"
web01 | CHANGED | rc= 0 >>
total 4
- rw- r
backup01 | CHANGED | rc= 0 >>
total 4
- rw- r
nfs01 | CHANGED | rc= 0 >>
total 4
- rw- r
#3. creates
[root@m01 ~ ]# ansible dkaiyun - m command - a "touch /data/lol.txt creates=/data/lol.txt"
[WARNING]: Consider using the file module with state= touch rather than running touch. If you need to use command because file is insufficient you can add
warn= False to this command task or set command_warnings= False in ansible.cfg to get rid of this message.
nfs01 | CHANGED | rc= 0 >>
backup01 | SUCCESS | rc= 0 >>
skipped, since / data/ lol.txt exists
web01 | CHANGED | rc= 0 >>
#4. removes
[root@m01 ~ ]# ansible dkaiyun - m command - a "rm -f /data/hosts removes=/data/hosts"
nfs01 | SUCCESS | rc= 0 >>
skipped, since / data/ hosts does not exist
backup01 | SUCCESS | rc= 0 >>
skipped, since / data/ hosts does not exist
[WARNING]: Consider using the file module with state= absent rather than running rm. If you need to use command because file is insufficient you can add
warn= False to this command task or set command_warnings= False in ansible.cfg to get rid of this message.
web01 | CHANGED | rc= 0 >>
2.shell模块
类似command 模块升级版—万能模块,可以使用"<" ,">" ,"|" ,";" ,"&" 等符号特殊符号.
官方文档:https://docs.ansible.com/ansible/latest/modules/shell_module.html
参数
选项/默认值
释义
chdir
在执行命令前,进入到指定目录中
creates
判断指定文件是否存在,如果存在,不执行后面的操作
removes
判断指定文件是否存在,如果存在,执行后面的操作
free_form
必须要输入一个合理的命令
#shell模块识别特殊符号,但是不支持 $符
[root@m 01 ~]# ansible 'web01' -m shell -a "ifconfig eth0 | awk 'NR==2 {print $2 }'"
web01 | CHANGED | rc=0 >>
inet 10.0 .0 .7 netmask 255.255 .255 .0 broadcast 10.0 .0 .255
#可以使用 撬棍 \ 转义 $符,就可以识别了
[root@m 01 ~]# ansible 'web01' -m shell -a "ifconfig eth0 | awk 'NR==2 {print \$2}'"
web01 | CHANGED | rc=0 >>
10.0 .0 .7
3.script模块
script 模块可以帮助我们在远程主机上执行 ansible 管理主机上的脚本,也就是说,脚本一直存在于 ansible 管理主机本地,不需要手动拷贝到远程主机后再执行。
官方文档:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/script_module.html
参数
选项/默认值
释义
chdir
在执行命令前,进入到指定目录中
creates
判断指定文件是否存在,如果存在,不执行后面的操作
removes
判断指定文件是否存在,如果存在,执行后面的操作
free_form
必须要输入一个合理的命令
[root@m 01 ~]# ansible 'web_group' -m script -a '/root/mkdir.sh'
web03 | CHANGED => {
"changed" : true ,
"rc" : 0 ,
"stderr" : "Shared connection to web03 closed.\r\n" ,
"stderr_lines" : [
"Shared connection to web03 closed."
],
"stdout" : "" ,
"stdout_lines" : []
}
web01 | CHANGED => {
"changed" : true ,
"rc" : 0 ,
"stderr" : "Shared connection to web01 closed.\r\n" ,
"stderr_lines" : [
"Shared connection to web01 closed."
],
"stdout" : "" ,
"stdout_lines" : []
}
#验证文件
[root@m 01 ~]# ansible 'web_group' -m shell -a 'ls -ld /123'
web01 | CHANGED | rc=0 >>
drwxr-xr-x 2 root root 6 Sep 17 17 :26 /123
web03 | CHANGED | rc=0 >>
drwxr-xr-x 2 root root 6 Sep 17 17 :26 /123
二、软件管理模块
1.yum_repository 模块
yum_repository模块可以帮助我们管理远程主机上的yum仓库。
官方文档:https://docs.ansible.com/ansible/latest/modules/yum_repository_module.html
时间参数
含义
name参数
必须参数,用于指定要操作的唯一的仓库ID,也就是”.repo”配置文件中每个仓库对应的”中括号”内的仓库ID
baseurl参数
此参数用于设置yum仓库的baseurl
description参数
此参数用于设置仓库的注释信息,也就是”.repo”配置文件中每个仓库对应的”name字段”对应的内容。
file参数
此参数用于设置仓库的配置文件名称,即设置”.repo”配置文件的文件名前缀,在不使用此参数的情况下,默认以name参数的仓库ID作为”.repo”配置文件的文件名前缀,同一个’.repo’配置文件中可以存在多个yum源
enabled参数
此参数用于设置是否激活对应的yum源,此参数默认值为yes,表示启用对应的yum源,设置为no表示不启用对应的yum源。
gpgcheck参数
此参数用于设置是否开启rpm包验证功能,默认值为no,表示不启用包验证,设置为yes表示开启包验证功能。
gpgcakey参数
当gpgcheck参数设置为yes时,需要使用此参数指定验证包所需的公钥
state参数
默认值为present,当值设置为absent时,表示删除对应的yum源
[root@m01 ~ ]
EXAMPLES:
yum_repository:
name: epel
description: EPEL YUM repo
baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
file: external_repos
gpgcheck: no
mirrorlist: http://mirrorlist.repoforge.org/el7/mirrors-rpmforge
enabled: no
state: absent
[nginx-stable ]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@m01 ~ ]
[root@m01 ~ ]
[root@m01 ~ ]
name:
description:
baseurl:
file:
gpgcheck:
mirrorlist:
enabled: no
state:
absent
present
2.yum模块
使用yum软件包管理器安装,升级,降级,删除和列出软件包和组。
官方文档:https://docs.ansible.com/ansible/latest/modules/yum_repository_module.html
参数
选项/默认值
释义
name(required)
指定软件名称信息
state
absent/removed
将软件进行卸载(慎用)
=
present/installed
将软件进行安装
latest
安装最新的软件 yum update
[root@m01 ~ ]
EXAMPLES:
- name: install the latest version of Apache
yum:
name: httpd
state: latest
name:
httpd
http://
/usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm
state:
latest
present
absent
[root@m01 ~]
1 .找到网上的包
https: //mirrors.aliyun.com/zabbix /zabbix/ 4.0 /rhel/7 /x86_64/zabbix-agent-4.0 .1 -1 .el7.x86_64.rpm
2 .安装
[root@m01 ~]
1 .上传包(上传到web端)
2 .安装
[root@m01 ~]
三、文件管理模块
1.copy模块
主要用于将管理主机上的数据信息传送给多台主机
官方文档:https://docs.ansible.com/ansible/latest/modules/copy_module.html
参数
选项/默认值
释义
src
指定将本地管理主机的什么数据信息进行远程复制
backup
no* yes
默认数据复制到远程主机,会覆盖原有文件(yes 将源文件进行备份)
content
在文件中添加信息
dest(required)
将数据复制到远程节点的路径信息
group
文件数据复制到远程主机,设置文件属组用户信息
mode
文件数据复制到远程主机,设置数据的权限 eg 0644 0755
owner
文件数据复制到远程主机,设置文件属主用户信息
remote_src
no* yes
如果设置为yes,表示将远程主机上的数据进行移动操作如果设置为no, 表示将管理主机上的数据进行分发操作
ps:(required)为必须使用的参数
*为默认参数
copy模块在复制数据时,如果数据为软链接文件,会将链接指定源文件进行复制
修改权限时候 需要加0 例如:chmod 0644 0755
1)帮助语法
[root@m01 ~ ]
EXAMPLES:
- name: Copy file with owner and permissions
copy:
src: /srv/myfiles/foo.conf
dest: /etc/foo.conf
owner: foo
group: foo
mode: '0644'
backup: yes
follow: yes
content: '# This file was moved to /etc/other.conf'
src:
dest:
owner:
group:
mode:
backup:
yes
no
follow:
yes
no
content:
2)模块实例
#推送nginx官方源
[root@m01 ~ ]# ansible 'web_group' - m copy - a 'src=/etc/yum.repos.d/nginx.repo dest=/etc/yum.repos.d/'
#推送文件并授权
[root@m01 ~ ]# ansible 'web_group' - m copy - a 'src=/root/test.conf dest=/etc/nginx/conf.d/ owner=root group=root mode=777'
#推送文件并备份
[root@m01 ~ ]# ansible 'web_group' - m copy - a 'src=/code/index.html dest=/code owner=nginx group=nginx mode=0644 backup=yes'
#识别软链接
[root@m01 ~ ]# ansible 'web_group' - m copy - a 'src=/root/test dest=/tmp owner=nginx group=nginx mode=0644 follow=yes'
[root@m01 ~ ]# ansible 'web_group' - m copy - a 'src=/root/test dest=/tmp owner=nginx group=nginx mode=0644 follow=no'
#直接写入内容到文件
[root@m01 ~ ]# ansible 'web_group' - m copy - a 'content="123456" dest=/etc/rsync_password mode=0600'
2.file模块
实现创建/删除文件信息 对数据权限进行修改
官方文档:https://docs.ansible.com/ansible/latest/modules/file_module.html
参数
选项/默认值
释义
dest/path/name(required)
将数据复制到远程节点的路径信息
group
文件数据复制到远程主机,设置文件属组用户信息
mode
文件数据复制到远程主机,设置数据的权限 eg 0644 0755
owner
文件数据复制到远程主机,设置文件属主用户信息
src
指定将本地管理主机的什么数据信息进行远程复制
state
absent
将数据进行删除
=
directory
创建一个空目录信息
=
file
查看指定目录信息是否存在
=
touch
创建一个空文件信息
=
hard/link
创建链接文件
1)帮助语法
[root@m01 ~ ]
EXAMPLES:
- name: Change file ownership, group and permissions
file:
path: /etc/foo.conf
owner: foo
group: foo
mode: '0644'
state: link,hard,touch,directory,absent
recurse: yes
src:
dest:
path:
owner:
group:
mode:
state:
link
touch
directory
absent
recurse:
yes
2)file模块实践
1 .创建目录
[root@m01 ~]# ansible 'web_group ' -m file -a 'path =/code state=directory'
#相当于到远程机器 mkdir /code
2 .创建目录并授权
[root@m01 ~]# ansible 'web01 ' -m file -a 'path =/code state=directory owner=nginx group =nginx mode=755 '
#相当于执行 mkdir /code && chown -R nginx.nginx /code && chmod 755 /code
3 .递归创建目录
[root@m01 ~]# ansible 'web01 ' -m file -a 'path =/code/wordpress/wp-content/uploads state=directory owner=nginx group =nginx mode=755 '
1 )如果目录不存在则创建并递归授权
2 )如果目录上级存在,则只授权新创建的目录
4 .递归授权目录
[root@m01 ~]# ansible 'web01 ' -m file -a 'path =/code state=directory owner=nginx group =nginx mode=755 recurse=yes'
5 .创建文件
[root@m01 ~]# ansible 'web01 ' -m file -a 'path =/code/1 .txt state=touch owner=nginx group =nginx mode=666 '
6 .删除文件
[root@m01 ~]# ansible 'web01 ' -m file -a 'path =/code/index.html state=absent'
7 .做软连接
[root@m01 ~]# ansible 'web01 ' -m file -a 'src =/code/wordpress dest=/code/link state=link'
#注意:
1 .创建文件时,上层目录必须存在
3. get_url 模块
用于将文件或软件从http、https或ftp下载到本地节点上或被管理机节点上
官方文档:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/get_url_module.html
参数
选项/默认值
释义
url
下载文件的地址
dest
下载保存的路径
mode
下载之后的权限设置数据的权限 eg 0644 0755
checksum
下载时进行验证
1)帮助语法
[root@m01 ~ ]
EXAMPLES:
- name: Download foo.conf
get_url:
url: http://example.com/path/file.conf
dest: /etc/foo.conf
mode: '0440'
checksum:
sha256:http://example.com/path/sha256sum.txt
url:
dest:
mode:
checksum:
sha256:http://example.com/path/sha256sum.txt
md5
2)模块实例
#下载包
[root@m01 ~ ]# ansible 'web_group' -m get_url -a 'url=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm dest=/tmp/'
#下载包并授权
[root@m01 ~ ]# ansible 'web_group' -m get_url -a 'url=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm dest=/tmp/ mode=777'
#下载包时验证
[root@linux /code ]# md5sum index.html
ba1f2511fc30423bdbb183fe33f3dd0f index.html
[root@m01 ~ ]# ansible 'web03' -m get_url -a 'url=http://10.0.0.7/index.html dest=/tmp mode=777 checksum =md5:ba1f2511fc30423bdbb183fe33f3dd0f'
4.fetch模块
抓取文件到管理机上
官方文档:https://docs.ansible.com/ansible/latest/modules/fetch_module.html
参数
选项/默认值
释义
src(required)
要获取的远程系统上的文件,必须是文件,而不是目录
dest
用于保存文件的目录
[root@m01 ~ ]# ansible web01 -m fetch -a "src=/root/lol.txt dest=/root"
web01 | CHANGED => {
"changed" : true ,
"checksum" : "da39a3ee5e6b4b0d3255bfef95601890afd80709" ,
"dest" : "/root/web01/root/lol.txt" ,
"md5sum" : "d41d8cd98f00b204e9800998ecf8427e" ,
"remote_checksum" : "da39a3ee5e6b4b0d3255bfef95601890afd80709" ,
"remote_md5sum" : null
}
[root@m01 ~ ]# tree ~
/root
└── web01
└── root
└── lol.txt
2 directories, 1 file
四、服务模块
1.service模块
用于管理服务运行状态
官方文档:https://docs.ansible.com/ansible/latest/modules/service_module.html
参数
选项/默认值
释义
enabled
no yes
设置服务是否开机自启动 如果参数不指定,原有服务开机自启动状态进行保留
name (required)
设置要启动/停止服务名称
state=
reloaded
平滑重启
=
restarted
重启
=
started
启动
=
stopped
停止
1)帮助语法
[root@m01 ~ ]
EXAMPLES:
- name: Start service httpd, if not started
service:
name: httpd
state: started
enabled: yes
name: httpd
state:
started
stopped
restarted
reloaded
enabled:
yes
no
2)实例
#1.停止nginx服务
[root@m01 ~ ]# ansible web03 -m service -a 'name=nginx state=stopped'
#2.启动httpd服务,并加入开机自启
[root@m01 ~ ]# ansible web03 -m service -a 'name=httpd state=started enabled=yes'
2.systemd模块
ansible 通过systemd模块,可以对服务的是否开机启动进行配置
官方文档:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/systemd_module.html
enabled
no yes
设置服务是否开机自启动 如果参数不指定,原有服务开机自启动状态进行保留
name (required)
设置要启动/停止服务名称
state=
reloaded
平滑重启
=
restarted
重启
=
started
启动
=
stopped
停止
1)帮助语法
[root@m01 ~ ]
EXAMPLES:
- name: Start service httpd, if not started
systemd:
name: httpd
state: started
enabled: yes
daemon_reload: yes
name: httpd
state:
started
stopped
restarted
reloaded
enabled:
yes
no
daemon_reload:
2)实例
#1.停止nginx服务
[root@m01 ~ ]# ansible web03 -m systemd -a 'name=nginx state=stopped'
#2.启动httpd服务,并加入开机自启
[root@m01 ~ ]# ansible web03 -m systemd -a 'name=httpd state=started enabled=yes'
六、用户模块
1.group模块
远程批量创建用户组信息
官方文档:https://docs.ansible.com/ansible/latest/modules/group_module.html
参数
选项/默认值
释义
gid
指创建的组ID信息
name
指创建组名称信息
state
absent
删除指定的用户组
=
present
创建指定的用户组
1)帮助语法
EXAMPLES:
- name: Ensure group "somegroup" exists
group:
name: somegroup
state:
present
absent
gid: 666
2)实例
#创建用户组
[root@m01 ~ ]# ansible web_group -m group -a 'name=www state=present gid=666'
#删除用户组
[root@m01 ~ ]# ansible web_group -m group -a 'name=www state=absent'
2.user模块
远程批量创建用户信息
官方文档:https://docs.ansible.com/ansible/latest/modules/user_module.html
参数
选项/默认值
释义
password
请输入密码信息
name
指定用户名信息
uid
指定用户uid信息
group
指定用户主要属于哪个组
groups
指定用户属于哪个附加组信息
shell
/bin/bash或/sbin/nologin
指定是否能够登录
create_home
yes/no
是否创建家目录信息
home
指定家目录创建在什么路径 默认/home
ps:password设置密码时不能使用明文方式,只能使用密文方式,可以给用户设置密码 还可以给用户修改密码。
1)帮助语法
- name: Add the user 'johnd' with a specific uid and a primary group of 'admin'
user:
name: johnd
comment: John Doe
uid: 1040
group: admin
groups: admins,developers
shell: /bin/bash
append: yes
remove: yes
generate_ssh_key: yes
ssh_key_bits: 2048
ssh_key_file: .ssh/id_rsa
expires: 1422403387
state:
present
absent
create_home:yes/no
password
2)实践
#1.创建用户
[root@m01 ~ ]# ansible web_group -m user -a 'name=www uid=666 group=www shell=/sbin/nologin state=present'
#2.仅删除用户
[root@m01 ~ ]# ansible web_group -m user -a 'name=www state=absent'
#3.删除用户及用户组
[root@m01 ~ ]# ansible web_group -m user -a 'name=www state=absent remove=yes'
#注意:
1. 如果用户名字跟组名字相同,删除用户是会将组也删除
2. 当组下面有多个用户,删除的与组同名的用户也不会删除组
七、定时模块
1.cron模块
定时任务模块
官方文档:https://docs.ansible.com/ansible/latest/modules/cron_module.html
参数
选项/默认值
释义
minute/hour/day/month/weekday
和设置时间信息相关参数
job
和设置定时任务相关参数
name(required)
设置定时任务注释信息
state
absent
删除指定定时任务
disabled
yes
将指定定时任务进行注释
=
no
取消注释
ps:时间参数不写时,默认为 *
1)帮助语法
EXAMPLES:
- name: Ensure a job that runs at 2 and 5 exists. Creates an entry like "0 5,2 * * ls -alh > /d
cron:
name: " check dirs"
minute: "0"
hour: "5,2"
day: "2"
month: "2"
weekday: "2"
job: "ls -alh > /dev/null"
state:
absent
present
2)实践
#1.添加定时任务
[root@m01 ~ ]# ansible web01 -m cron -a 'name="时间同步" minute=*/10 job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null"'
#2.修改定时任务(不修改名字,只修改内容)
[root@m01 ~ ]# ansible web01 -m cron -a 'name="时间同步" job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null"'
#3.删除定时任务(一定要用name参数)
[root@m01 ~ ]# ansible web01 -m cron -a 'name="时间同步" state=absent'
#4.注释定时任务
[root@m01 ~ ]# ansible web01 -m cron -a 'name="时间同步" job="/usr/sbin/ntpdate time1.aliyun.com &> /dev/null" disabled=yes'
八、挂载模块
1.mount模块
用于批量管理主机进行挂载卸载操作
官方文档:https://docs.ansible.com/ansible/latest/modules/mount_module.html
参数
选项/默认值
释义
fstype
指定挂载的文件系统类型
opts
指定挂载的参数信息
path
定义一个挂载点信息
src
定义设备文件信息
state
absent
会进行卸载,也会修改fstab文件信息
=
unmounted
会进行卸载,不会修改fstab文件
=
present
不会挂载,只会修改fstab文件
=
mounted
会进行挂载,会修改fstab文件
ps:在进行挂载的时候,使用state =mounted
在进行卸载的时候,使用state =absent
1)帮助语法
EXAMPLES:
- name: Mount DVD read-only
mount:
path: /mnt/dvd
src: /dev/sr0
fstype: nfs
opts: ro,noauto
state:
present
unmounted
mounted
absent
2)准备挂载的服务端
1. 安装nfs
[root@m01 ~ ]# ansible nfs - m yum - a 'name=nfs-utils state=present'
2. 配置nfs
[root@m01 ~ ]# ansible nfs - m copy - a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)" dest=/etc/exports'
3. 创建用户
[root@m01 ~ ]# ansible nfs - m group - a 'name=www gid=666'
[root@m01 ~ ]# ansible nfs - m user - a 'name=www uid=666 group=www shell=/sbin/nologin create_home=no'
4. 创建目录并授权
[root@m01 ~ ]# ansible nfs - m file - a 'path=/data state=directory owner=www group=www'
5. 启动
[root@m01 ~ ]# ansible nfs - m systemd - a 'name=nfs state=started'
3)客户端使用模块挂载
#挂载目录,并加入开机自动挂载
[root@m01 ~ ]# ansible web01 -m mount -a 'src=172.16.1.31:/data path=/code/wordpress fstype=nfs state=mounted'
#取消挂载,并取消开机自动挂载
[root@m01 ~ ]# ansible web01 -m mount -a 'src=172.16.1.31:/data path=/code/wordpress fstype=nfs state=absent'
九、安全策略模块
1.selinux模块
管理远端主机的 SELINUX 防火墙;
官方文档:https://docs.ansible.com/ansible/latest/collections/ansible/posix/selinux_module.html
参数
说明
state
Selinux模式:enforcing、permissive、disabled
polocy
targeted
1)帮助语法
EXAMPLES:
- name: Enable SELinux
selinux:
policy: targeted
state:
enforcing
disabled
- name: Put SELinux in permissive mode, logging actions that would be blocked.
ansible.posix.selinux:
policy: targeted
state: permissive
- name: Disable SELinux
ansible.posix.selinux:
state: disabled
2)关闭selinux
[root@m01 ~ ]# ansible web01 -m selinux -a 'state=disabled'
2.firewalld模块
firewalld模块用来添加、删除防火墙规则。
官方文档:https://docs.ansible.com/ansible/latest/collections/ansible/posix/firewalld_module.html
参数
说明
state
必须参数,指定防火墙策略状态,enable表示策略生效,disable表示策略禁用,present表示新建策略,absent表示删除策略
service
向防火墙添加/删除的服务名称,该服务必须在firewall-cmd --get-services可以查询到
port
要从防火墙添加或删除端口或端口范围,必须以端口/协议,端口范围/协议的形式书写
permanent
保存策略,在下次启动时自动加载
immediate
配置永久策略后立即生效
interface
添加/删除 出入防火墙的接口
offline
脱机状态运行防火墙
zone
添加/删除防火墙区域,有如下区域可供配置
drop
丢弃所有进入的包,而不给出任何响应
block
拒绝所有外部发起的连接,允许内部发起的连接
public
允许指定的进入连接
external
同上,对伪装的进入连接,一般用于路由转发
dmz
允许受限制的进入连接
work
允许受信任的计算机被限制的进入连接,类似 workgroup
home
同上,类似 homegroup
internal
同上,范围针对所有互联网用户
trusted
信任所有连接
source
指定从防火墙添加/删除的网段
指定从防火墙添加/删除的网段
非永久性规则的生效时间
1)帮助语法
EXAMPLES:
- firewalld:
service: https
permanent:
yes
no
state:
enabled
disable
port: 8081 /tcp 161 -162 /udp
zone: dmz
rich_rule:
source: 192.0 .2 .0 /24
masquerade:
yes
no
interface: eth2
2)防火墙配置实践
#1.允许访问http,永久生效
[root@m01 ~ ]# ansible web01 -m firewalld -a 'service=http permanent=yes state=enabled'
#2.允许80端口被访问,临时生效
[root@m01 ~ ]# ansible web01 -m firewalld -a 'port=80/tcp state=enabled'
#3.允许10.0.0.0/24网段访问22端口
[root@m01 ~ ]# ansible web01 -m firewalld -a 'rich_rule="rule family=ipv4 source address=10.0.0.0/24 service name=ssh accept" state=enabled'
#4.允许10.0.0.0/24网段访问所有服务
[root@m01 ~ ]# ansible web01 -m firewalld -a 'source=10.0.0.0/24 zone=trusted state=enabled permanent=yes'
十、压缩解压模块
1.unarchive 解压模块
解压压缩包
官方文档:https://docs.ansible.com/ansible/latest/modules/unarchive_module.html
1)帮助语法
- name: Unarchive a file that is already on the remote machine
unarchive:
src: /tmp/foo.zip
dest: /usr/local/bin
remote_src:
yes
no
2)实例
[root@m01 /package ]
[root@m01 /package ]
2.archive 压缩模块
打包成压缩包
官方文档:https://docs.ansible.com/ansible/latest/modules/archive_module.html
1)帮助语法
EXAMPLES:
- name: Compress directory /path/to/foo/ into /path/to/foo.tgz
archive:
path: /path/to/foo
dest: /path/to/foo.tgz
format:bz2, gz, tar, xz, zip
2)实例
[root@m01 /package ]
十一、主机信息模块-setup
1.setup-主机信息模块
setup 模块用于收集远程主机的一些基本信息。
官方文档:https: //docs.ansible.com/ansible /latest/user _guide/sample_setup.html
2.setup模块需求
为什么要讲这个模块?
这个模块非常实用,在公司中总会有一些需求
比如:
1 .根据不同主机不同IP 创建对应IP 的目录
2 .根据不同主机不同主机名创建对应主机名的目录
3 .自动化运维平台需要自动获取到主机的IP 地址,内存信息,磁盘信息,主机名...等
4 .如果安装数据库,分配内存为物理内存的80 %,此时有3 台不同物理内存的机器2 G、4 G、16 G
写一个playbook的情况下,我需要获取到对应主机的内存并作出计算,写判断。
3.setup-使用
1. 获取所有主机信息
[root@m01 ~ ]# ansible web01 -m setup
2. 获取主机名(使用setup获取的信息,指定对应的小标题获取指定的信息)
[root@m01 ~ ]# ansible web01 -m setup -a 'filter=ansible_fqdn'
4.setup-常用的参数
ansible_all_ipv4_addresses:仅显示ipv4的信息。
ansible_devices:仅显示磁盘设备信息。
ansible_distribution:显示是什么系统,例:centos,suse等。
ansible_distribution_major_version:显示是系统主版本。
ansible_distribution_version:仅显示系统版本。
ansible_machine:显示系统类型,例:32 位,还是64 位。
ansible_eth0:仅显示eth0的信息。
ansible_hostname:仅显示主机名。
ansible_kernel:仅显示内核版本。
ansible_lvm:显示lvm相关信息。
ansible_memtotal_mb:显示系统总内存。
ansible_memfree_mb:显示可用系统内存。
ansible_memory_mb:详细显示内存情况。
ansible_swaptotal_mb:显示总的swap内存。
ansible_swapfree_mb:显示swap内存的可用内存。
ansible_mounts:显示系统磁盘挂载情况。
ansible_processor:显示cpu 个数(具体显示每个cpu 的型号)。
ansible_processor_vcpus:显示cpu 个数(只显示总的个数)。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库