第四章 Ansible常用模块详解

一、命令模块

1.command模块

命令模块 适合使用简单的命令 无法支持"<",">","|",";","&"等符号
官方文档:https://docs.ansible.com/ansible/latest/modules/command_module.html#command-module
参数 选项/默认值 释义
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: `--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#shell-module
参数 选项/默认值 释义
chdir 在执行命令前,进入到指定目录中
creates 判断指定文件是否存在,如果存在,不执行后面的操作
removes 判断指定文件是否存在,如果存在,执行后面的操作
free_form 必须要输入一个合理的命令
#shell模块识别特殊符号,但是不支持 $符
[root@m01 ~]# 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@m01 ~]# 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@m01 ~]# 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@m01 ~]# 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#yum-repository-module
时间参数 含义
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 ~]# ansible-doc yum_repository
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

#创建nginx官方源
[root@m01 ~]# ansible 'web_group' -m yum_repository -a 'name="nginx-stable" description="nginx stable repo" baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=no enabled=yes file=nginx'

#添加源:不修改 file,只要name不相同就是添加源
[root@m01 ~]# ansible 'web_group' -m yum_repository -a 'name="nginx-mainline" description="nginx mainline repo" baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/ gpgcheck=no enabled=yes file=nginx'

#修改源:不修改 file 和 name,改其他的内容都会修改源
[root@m01 ~]# ansible 'web_group' -m yum_repository -a 'name="nginx-mainline" description="nginx mainline repo" baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/ gpgcheck=no enabled=yes file=nginx'

    name: 			#yum源 []里面的内容
    description: 	#yum源 name的值
    baseurl: 		#yum源 中的仓库地址
    file: 			#yum源的名字
    gpgcheck: 		#yum源是否检查
    mirrorlist: 	#源的列表
    enabled: no		#源是否启动
    state: 
    	absent		#删除
    	present		#添加

2.yum模块

使用yum软件包管理器安装,升级,降级,删除和列出软件包和组。
官方文档:https://docs.ansible.com/ansible/latest/modules/yum_repository_module.html#yum-repository-module
参数 选项/默认值 释义
name(required) 指定软件名称信息
state absent/removed 将软件进行卸载(慎用)
= present/installed 将软件进行安装
latest 安装最新的软件 yum update
[root@m01 ~]# ansible-doc yum
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		#本地的rpm包
state:
	latest						#安装最新的版本
	present						#安装
	absent						#卸载
#名字安装httpd
[root@m01 ~]# ansible 'web_group' -m yum -a 'name=httpd state=present'
#类似于在远程机器上执行 yum install -y httpd

#使用网上软件包安装
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 ~]# ansible 'web_group' -m yum -a 'name=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.1-1.el7.x86_64.rpm state=present'
#类似于在远程机器上执行 yum install -y https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.1-1.el7.x86_64.rpm

#使用本地包安装
1.上传包(上传到web端)
2.安装
[root@m01 ~]# ansible 'web_group' -m yum -a 'name=/tmp/nginx-1.16.1-1.el7.ngx.x86_64.rpm state=present'
#类似于在远程机器上执行 yum localinstall -y /tmp/nginx-1.16.1-1.el7.ngx.x86_64.rpm

三、文件管理模块

1.copy模块

主要用于将管理主机上的数据信息传送给多台主机
官方文档:https://docs.ansible.com/ansible/latest/modules/copy_module.html#copy-module
参数 选项/默认值 释义
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 ~]# ansible-doc copy
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: 			#源路径(要进行copy的文件,文件在控制端)
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#file-module
参数 选项/默认值 释义
dest/path/name(required) 将数据复制到远程节点的路径信息
group 文件数据复制到远程主机,设置文件属组用户信息
mode 文件数据复制到远程主机,设置数据的权限 eg 0644 0755
owner 文件数据复制到远程主机,设置文件属主用户信息
src 指定将本地管理主机的什么数据信息进行远程复制
state absent 将数据进行删除
= directory 创建一个空目录信息
= file 查看指定目录信息是否存在
= touch 创建一个空文件信息
= hard/link 创建链接文件
1)帮助语法
[root@m01 ~]# ansible-doc file
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#get_url
参数 选项/默认值 释义
url 下载文件的地址
dest 下载保存的路径
mode 下载之后的权限设置数据的权限 eg 0644 0755
checksum 下载时进行验证
1)帮助语法
[root@m01 ~]# ansible-doc get_url
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#fetch-module
参数 选项/默认值 释义
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#service-module
参数 选项/默认值 释义
enabled no yes 设置服务是否开机自启动 如果参数不指定,原有服务开机自启动状态进行保留
name (required) 设置要启动/停止服务名称
state= reloaded 平滑重启
= restarted 重启
= started 启动
= stopped 停止

1)帮助语法

[root@m01 ~]# ansible-doc service
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#systemd
参数 选项/默认值 释义
enabled no yes 设置服务是否开机自启动 如果参数不指定,原有服务开机自启动状态进行保留
name (required) 设置要启动/停止服务名称
state= reloaded 平滑重启
= restarted 重启
= started 启动
= stopped 停止

1)帮助语法

[root@m01 ~]# ansible-doc systemd
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#group-module
参数 选项/默认值 释义
gid 指创建的组ID信息
name 指创建组名称信息
state absent 删除指定的用户组
= present 创建指定的用户组

1)帮助语法

EXAMPLES:
- name: Ensure group "somegroup" exists
  group:
    name: somegroup				#组名字
    state: 
    	present					#创建用户组
    	absent					#删除用户组
    gid: 666					#用户组ID
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#user-module
参数 选项/默认值 释义
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							#用户id
    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#cron-module
参数 选项/默认值 释义
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#mount-module
参数 选项/默认值 释义
fstype 指定挂载的文件系统类型
opts 指定挂载的参数信息
path 定义一个挂载点信息
src 定义设备文件信息
state absent 会进行卸载,也会修改fstab文件信息
= unmounted 会进行卸载,不会修改fstab文件
= present 不会挂载,只会修改fstab文件
= mounted 会进行挂载,会修改fstab文件
ps:在进行挂载的时候,使用state=mounted
在进行卸载的时候,使用state=absent
1)帮助语法
EXAMPLES:
# Before 2.3, option 'name' was used instead of 'path'
- name: Mount DVD read-only
  mount:
    path: /mnt/dvd			#挂载的目录(nfs客户端)
    src: /dev/sr0			#远端被挂载的目录 (nfs服务端)
    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			 #防火墙配置的源ip
    masquerade: 
    	yes					 		#开启ip伪装
    	no							#关闭ip伪装
    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#unarchive-module
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)实例
#1.解压控制端的包到受控端
[root@m01 /package]# ansible web01 -m unarchive -a 'src=/package/php.tar.gz dest=/tmp/'

#2.解压受控端的包到受控端
[root@m01 /package]# ansible web03 -m unarchive -a 'src=/package/php.tar.gz dest=/tmp/ remote_src=yes'

2.archive 压缩模块

打包成压缩包
官方文档:https://docs.ansible.com/ansible/latest/modules/archive_module.html#archive-module
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)实例
#1.打包站点目录
[root@m01 /package]# ansible web01 -m archive -a 'path=/code dest=/tmp/code.tar.gz'

十一、主机信息模块-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 ~]# 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个数(只显示总的个数)。
posted @ 2020-10-26 16:48  年少纵马且长歌  阅读(156)  评论(0编辑  收藏  举报