作者:@青青子衿悠悠我心 本文为作者原创,转载请注明出处:https://www.cnblogs.com/jhno1/p/13879310.html
一、命令模块
1.command模块
命令模块 适合使用简单的命令 无法支持"<" ,">" ,"|" ,";" ,"&" 等符号
官方文档:https://docs.ansible.com/ansible/latest/modules/command_module.html
参数
选项/默认值
释义
chdir
在执行命令前,进入到指定目录中
creates
判断指定文件是否存在,如果存在,不执行后面的操作
removes
判断指定文件是否存在,如果存在,执行后面的操作
free_form
必须要输入一个合理的命令
[root@m01 ~]
web01 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 110 460 19 401 667
Swap: 2047 0 2047
[root@m01 ~]
web01 | FAILED | rc=1 >>
|: Unknown host
ifconfig: `--help ' gives usage information.non-zero return code
ps:当ansible命令没指定模块时,默认使用command模块
#2.chdir
[root@m01 ~]# ansible dkaiyun -m command -a "chdir=/data ls -l"
web01 | CHANGED | rc=0 >>
total 4
-rw-r--r-- 1 root root 158 Jan 12 11:11 hosts
backup01 | CHANGED | rc=0 >>
total 4
-rw-r--r-- 1 root root 4 Jan 13 18:06 lol.txt
nfs01 | CHANGED | rc=0 >>
total 4
-rw-r--r-- 1 root root 13 Jan 17 18:45 bbb.txt
#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
必须要输入一个合理的命令
[root@m01 ~]
web01 | CHANGED | rc=0 >>
inet 10.0.0.7 netmask 255.255.255.0 broadcast 10.0.0.255
[root@m01 ~]
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@m01 ~]
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@m01 ~]
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)模块实例
[root@m01 ~]
[root@m01 ~]
[root@m01 ~]
[root@m01 ~]
[root@m01 ~]
[root@m01 ~]
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 ~]
2.创建目录并授权
[root@m01 ~]
3.递归创建目录
[root@m01 ~]
1)如果目录不存在则创建并递归授权
2)如果目录上级存在,则只授权新创建的目录
4.递归授权目录
[root@m01 ~]
5.创建文件
[root@m01 ~]
6.删除文件
[root@m01 ~]
7.做软连接
[root@m01 ~]
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 ~]
[root@m01 ~]
[root@linux /code]
ba1f2511fc30423bdbb183fe33f3dd0f index.html
[root@m01 ~]
4.fetch模块
抓取文件到管理机上
官方文档:https://docs.ansible.com/ansible/latest/modules/fetch_module.html
参数
选项/默认值
释义
src(required)
要获取的远程系统上的文件,必须是文件,而不是目录
dest
用于保存文件的目录
[root@m01 ~]
web01 | CHANGED => {
"changed" : true ,
"checksum" : "da39a3ee5e6b4b0d3255bfef95601890afd80709" ,
"dest" : "/root/web01/root/lol.txt" ,
"md5sum" : "d41d8cd98f00b204e9800998ecf8427e" ,
"remote_checksum" : "da39a3ee5e6b4b0d3255bfef95601890afd80709" ,
"remote_md5sum" : null
}
[root@m01 ~]
/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)实例
[root@m01 ~]
[root@m01 ~]
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)实例
[root@m01 ~]
[root@m01 ~]
六、用户模块
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 ~]
[root@m01 ~]
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)实践
[root@m01 ~]
[root@m01 ~]
[root@m01 ~]
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)实践
[root@m01 ~]
[root@m01 ~]
[root@m01 ~]
[root@m01 ~]
八、挂载模块
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 ~]
2.配置nfs
[root@m01 ~]
3.创建用户
[root@m01 ~]
[root@m01 ~]
4.创建目录并授权
[root@m01 ~]
5.启动
[root@m01 ~]
3)客户端使用模块挂载
[root@m01 ~]
[root@m01 ~]
九、安全策略模块
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 ~]
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)防火墙配置实践
[root@m01 ~]
[root@m01 ~]
[root@m01 ~]
[root@m01 ~]
十、压缩解压模块
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台不同物理内存的机器2G、4G、16G
写一个playbook的情况下,我需要获取到对应主机的内存并作出计算,写判断。
3.setup-使用
1.获取所有主机信息
[root@m01 ~]
2.获取主机名(使用setup获取的信息,指定对应的小标题获取指定的信息)
[root@m01 ~]
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 加持,快人一步
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
· 本地部署 DeepSeek:小白也能轻松搞定!
· 基于DeepSeek R1 满血版大模型的个人知识库,回答都源自对你专属文件的深度学习。
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 大人,时代变了! 赶快把自有业务的本地AI“模型”训练起来!
· Tinyfox 简易教程-1:Hello World!