1 安装与配置

参考视频教程

1.1 安装

安装ansible管理服务

1
yum instal -y ansible

1.2 配置

1.2.1 ansible.cfg

参考同级目录的ansible.cfg解释说明

1.2.2 hosts

hosts文档并不是/etc/hosts文件,而是/etc/ansible/hosts,主要定义ansible要维护的机器ip机器分组

格式:

1
2
3
4
5
6
[组名]
ip/域名

#未配置ssh免密
[node]
ip ansible_ssh_pass=密码 ansible_ssh_port=ssh的端口号

例如:

1
2
3
4
5
6
7
[all]
192.168.72.151
192.168.72.152
192.168.72.153

[node]
192.168.72.153 ansible_ssh_pass='123456' ansible_ssh_port=22

验证:

1
2
3
ansible 组名 -m 模块名
例如:
ansible all -m ping

2 核心模块

常用的模块有以下,更多请参数官方文档

分类 模块分类 解释
命令和脚本模块 command模块 默认的模块 执行简单命令,不支持特殊符号
shell模块 执行命令,支持特殊符号
script模块 分发脚本执行
文件 file 创建目录,文件,软连接
copy 远程分发文件,修改权限,所有者,备份
服务 systemd 服务管理
service 服务管理(ubuntu等)
软件包 yum源 yum_repository
yum yum命令
get_url 下载软件
系统管理 mount模块 挂载
crons模块 定时任务
用户管理 group模块 管理用户组
user模块 管理用户
其他可以研究 unarchive 压缩解压
rsync模块
数据库模块 mysql_db,mysql_user
其他 ansible管理docker,k8s,zibbix,grafana
用于调试 ping 检查ansizble与其他节点连通
debug 用于检查/显示 变量
….. ……

2.1 命令与脚本类模块

2.1.1 command模块

仍支持简单命令,不支持特殊符号,管道

该模块是默认模块,ansible不加上模块,默认就使用这个模块

1
2
3
ansible all -m conmand -a '命令'
#等同于
ansible all -a '命令' #省略 -m command

2.1.2 shell模块

与command模块类似,shell模块支持特殊符号,执行脚本

1
ansible all -m shell '~/test.sh'
  • ansible中的颜色
    • 绿色:表示正常
    • 黄色:表示执行正常,状态变化
    • 红色:表示错误,输出错误信息
    • 紫色:表示警告,建议

2.1.3 script模块-传输脚本到被管理端并执行脚本

传输脚本

执行脚本

1
2
3
4
5
6
#安装vim
cat ./vim_install.sh
yum install -y vim

#使用scirpt模块执行脚本
ansible all -m script -a './vim_install.sh'

2.2 文件与目录管理模块

  • file 创建文件,目录
  • copy 远程传输文件,目录,类似于scp.

2.2.1 file模块

管理文件或者目录

filfe模块中选项
path 路径(目录,文件) 必须要写
src 源文件一般用于link(创建软连接模块) 用于指定源文件
state 状态(模式)
directory 创建目录
file(默认) 更新文件,如果文件不存在也不创建
link 创建软件连接
touch 创建文件
absent 删除文件

格式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#创建目录
ansible 组名 -m file -a 'path=路径 state=directory'
#列出组名下目录或文件
ansible 组名 -a 'ls -l /路径'

#创建文件
ansible 组名 -m file -a 'path=路径/文件名 state=touch'

#创建软件文件
ansible 组名 -m file -a 'src=路径1 path=路径/文件名 state=link'

#删除文件/目录/软连接
ansible 组名 -m file -a 'path=路径 state=absent'

#创建文件并配置所有者,用户组,权限
ansible 组名 -m file -a 'path=路径/文件名 owner=root group=root mode=777 state=touch'

2.2.2 cop远程传输模块

copy模块
src source源文件
dest destination目标
backpu backup=yes则会在覆盖前进行备份
mode 修改权限
owner 修改为指令所有者
group 修改为指定用户组

格式:

1
2
3
4
5
#传输文件
ansible 组名 -m copy -a 'src=路径1 dest=路径2'

#传输并覆盖
ansible 组名 -m copy -a 'src=路径1 dest=路径2 backup=yes'

2.3 服务管理模块

systemctl 命令

启动/关闭/重启服务

开启自启动/开机不自启动

2.3.1 systemcd

systemd模块
name 用于指定服务名称
enabled yes开机启动,false开机不自启动
state 表示服务开,关,重启….
state=started 开启
state=stoppped 关闭
state=reloaded 重读配置
state=restarted 重启(关闭后再开启)
daemon-reload yes是否重新加载对应的服务的管理配置文件

格式:

1
2
3
4
5
6
7
ansible 组名 -m systemd -a 'name=服务器 enable=是否启动开机自启yes/false state=服务操作'

#例如
1.启动sshd服务
ansible all -m systemd -a 'name=sshd enable=yes state=started'
2.重新加载sshd服务
ansible all -m systemd -a 'name=sshd reload'

2.3.2 service 对比

服务管理模块 systemd services
开机自启动 enabled enabled
服务名称 name name
服务开关重启 state state
运行级别 runlevel(运行级别)
重启加载systemctl配置 deamon_reload=yes systemctl daemon-reload

2.4 软件包

yum_repository (管理yum源)

yum (yum命令)

get_url (wget命令)

2.4.1 yum源

yum源模块 yum_repository yum源配置文件内容
name [eple] yum源中名字(中括号里面的名称)
description name=xxxxx yum源注释说明
baseurl yum源中baseurl下载地址
enabled 是否启动这个源 yes/no
gpgcheck 是否启动gpgcheck功能 on
file 指定yum源的文件 自动添加.repo 默认与模块名字一致
1
2
3
4
5
6
7
8
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

格式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ansible all -m yum_repository -a 'name=名称 description=描述/解释 baserurl=下载地址  gpgcheck=yes/no  enable=yes/no'

例如:增加nginx yum源配置
ansible all -m yum_repository -a 'name="nginx stable repo" description="nginx yum" baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=no enabled=no'

#上述yum配置也可以 可以使用copy模块,先交yum源写好,现分发下载,方式多种多样
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1

#注意使用'&&'特殊符号不能使用默认的command模块,需要使用 shell模块
ansible all -m shell -a 'yum clean all && yum makecache'

2.4.2 yum模块

通过yum命令安装软件

yum 模块
name 指定软件名称
state installed 安装(pre)
removed 删除(absent)
lastest 安装或更新

格式:

1
2
3
4
5
6
7
8
9
ansible 组名 -m yum -a 'name=软件名 state=installed/removed/lastest'

例如:安装vim
ansible all -m yum -a 'name=vim state=installed'
例如: 安装多个软件包,vim,net-tools
ansible all -m yum -a 'name=vim,net-tools state=installed'

#也可以使用命令方式安装
ansibel all -a 'yum install -y vim '

2.4.3 get_url模块(wget)

get_url下载功能
url 指定需要下载的地址
dest 下载到哪个目录(目录必须存在,否则下载失败)

格式:

1
2
3
4
5
6
7
8
9
ansbel 组名 -m get_url -a "url=下载地址 path=保存到目标目录"

例如:下载nginx软件包
1.检查目录是否存在
ansible local1 -a 'ls -l ~/nginx/'
2.如果不存在,必须先创建
ansible local1 -a 'mkdir -p ~/nginx/'
3.下载nginx源码包
ansible local1 -m get_url -a 'url=http://nginx.org/download/nginx-1.24.0.tar.gz dest=~/nginx/'

2.5 系统管理模块

mount 挂载nfs模块

cron 管理定时任务模块

2.5.1 mount模块

mount模块
fstype 指定文件系统类型
src 源地址(nfs服务端地址 eg:192.168.72.151)
path 挂载点(源地址挂载本地目录的位置)
state state参数可用的值:
absent 卸载并修改/etc/fstab
unmounted 卸载不修改/etc/fstab
present 仅修改/etc/fstab
mounted 挂载修改/etc/fstab
remounted 重新挂载

格式:

1
2
3
4
5
6
7
8
9
10
ansible 组名 -m mount -a 'fstype=文件类型 src=nfs地址/目标地址 path=本地挂载路径 state=mounted/remounted/present/unmounted/absent '
例如:挂载nfs地址
1.检查挂载路径是否存在
ansible nfs -a 'ls -l /data'
2.挂载nfs并检查挂载情况
ansible nfs -m mount -a 'fstype=nfs src=192.168.72.151/share path=/data state=mounted'
ansible nfs -a 'df -h'
ansible nfs -a 'tail -n 4 /etc/fstab'
3.卸载nfs
ansible nfs -m mount -a 'fstype=nfs src=192.168.72.151/share path=/data state=absent'

2.5.2 cron定时任务模块

- 星号(*):表示所有分钟

- 逗号(,):用于指定多个分钟,例如:——0,15,30,45——表示每小时的0、15、30和45分钟

- 中划线(-):用于指定一个时间段,例如:——10-20——表示从10分钟到20分钟

- 斜杠(/):用于指定一个时间间隔,例如:——*/5——表示每5分钟

cron 定时任务模块 定时任务配置中的内容
name 定时任务名称(必写),对应下面注释的内容
minute */2 分钟 minute=“*/2”
hour 小时
day
month
week
job 命令或脚本 指定命令或脚本(定向到空) job=‘/sbin/ntpdate ntp1.aliyun.com &>dev/null’
state present 添加定时任务
absent 删除定时任务
1
2
3

#1.sync time aliyun
0/2 * * * * * /sbin/ntpdate ntp1.alyun.com &>dev/null #

格式:

1
2
3
4
5
6
7
ansible all -m cron -a 'anme=名称 minute=分钟 hour=时 day=天 month=月 week=周 job=脚本或命令 state=present /absent'

例如:增加定时任务,每2秒执行一次时间同步
ansible all -m cron -a 'name="sync time" minute="*/2" job="/sbin/ntpdate ntp1.alyun.com &>dev/null" state=present'

#删除定时脚本
ansible all -m cron -a 'name="sync time" minute="*/2" job="/sbin/ntpdate ntp1.alyun.com &>dev/null" state=absent'

2.6 用户管理模块

2.6.1 user模块

user模块
name 用户名
uid 指定uid
group 指定用户组
shell 指定用户的命令解释器
create_home 是否创建家目录(yes/no)
state present 添加用户
absent 删除用户

格式:

1
2
3
4
5
ansible 组名 -m user -a 'name=用户名 uid=指定uid group=指定用户组 shell=解释器 create_home=yes/no state=present/absent'
例如:增加一个用户
ansible local1 -m user -a 'name=test uid=10099 shell=/sbin/nologin create_home=no state=present'
#删除用户
ansible local1 -m user -a 'name=test uid=10099 shell=/sbin/nologin create_home=no state=absent'

3.ansible-playbook(剧本)

将一组任务或命令按一定的顺序编排后批量/重复执行的文件

ansible ad-hoc vs playbook区别

含义 应用场景
ad-hoc ans命令运行对应模块与选项.(类似于命令) 临时,测试
playbook 通过文件执行(类似于脚本) 用户于批量管理,部署,收集信息,分发…(文件重复执行)

playbokk vs shell脚本

应用场景
playbook剧本 批量管理,批量部署,批量分发等
shell脚本 某一台,服务脚本,系统巡检,定时备份

3.1 Playbook剧本的使用

3.1.1剧本格式解释

  • 剧本后缀是 .yaml或.yml
  • 缩进,不要使用tab键
1
2
3
4
5
6
7
8
9
10
----
- host: all #组名/某台设备的Ip等
remote_user: root #执行的用户
vars: #指定变量
fils_name: oldboylinux #定义变量 fils_name
tasks: #指定剧本的任务/操作
- name: touch new files #操作的名称1
shell: touch /tmp/{{fils_name}} #执行的操作1
- name: touch new files #操作的名称2
shell: touch /tmp/{{fils_name}} #执行的操作2
  • 核心格式剧本中所有的内容要对齐
  • 对齐的时候不能使用tab键
  • 只能使用空格,2个空格成对,相对的一级2个,二级就4个

空格 空格

空格 空格 空格 空格

  • hosts用于指定在哪些主机执行指令
  • tasks:用于对这些主机,运行什么模块及选项

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
vim 01.add_cron.yml
----
- hosts:all
task:
- name: add cron sync time
cron: name="sync time" minute="*/2" job="/sbin/ntpdate ntp1.alyun.com &>dev/null" state=present


#执行脚本
ansible-playbook -i hosts -C 01.add_cron.yml

-i: 指定hosts文件
-C: 检查yaml/yml文件的格式

案例1:部署nfs服务器并启动
vim 01.add_cron.yml
----
- hosts:local
task:
- name: 01.部署nfs服务端软件
yum:
name: nfs-utils
state: installed

- name: 02.修改配置文件
lineinfile:
path: /etc/exports
line: "/data 192.168.72.0/24(rw)"
state: present
backup: yes

- name: 03.创建对应的目录,权限
file:
path: /data/
owner: nfsnoboby
group: nfsnoboby
state: directory

- name: 04.启动服务-rpc服务
systemd:
name: rpcbind
enabled: yes
state: started

- name: 05.启动服务-nfs服务
systemd:
name: nfs
enabled: yes
state: started

3.2 ansible变量

变量分类
命令行 l临时使用,较少使用
变量文件vars_files 某一台主机使用,较少用
主机组共用的变量文件 group_vars 应用范围广泛,使用较多
ansible内置变量(facts变量) 收集主机的基本信息,ip地址、主机名、系统及版本….

3.2.1 局部变量

写在剧本的hosts下,使用vars声明

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
----
- host: all
vars: #指定变量
dir_name: /data
file_name: a.txt
tasks:
- name: mkdir
file:
path: "{{dir_name}}"
state: directory
- name: touch
file:
path:"{{dir_name}}/{{file_name}}"
state: touch

3.2.2 全局变量

3.2.2.1 独立文件方式

写在独立文件当中,在剧本中引入,vars_file:指定变量文件的位置 ,剧本比较大的时候

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
----
- host: all
vars_file: ./var.yml #指定引入变量文件
tasks:
- name: mkdir
file:
path: "{{dir_name}}"
state: directory
- name: touch
file:
path:"{{dir_name}}/{{file_name}}"
state: touc


#变量文件
cat ./var.yml
dir_name: /data
file_name: a.txt

3.2.2.2 group_vars方式

将剧本的变量写在对应组的文件下,group_vars下,该目录不要放入剧本,只放变量文件,剧本会自动引入该变量,适用于多组中的使用变量值不一样的情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[root@k8s-master ansible]# tree group_vars/
group_vars/
├── all
│   └── vars.yml
├── node
│   └── vars.yml
└── test
└── vars.yml

----
- host: all #hosts自主引入变量文件
tasks:
- name: mkdir
file:
path: "{{dir_name}}"
state: directory
- name: touch
file:
path:"{{dir_name}}/{{file_name}}"
state: touc
- host: node #hosts自主引入变量文件
tasks:
- name: mkdir
file:
path: "{{dir_name}}"
state: directory
- name: touch
file:
path:"{{dir_name}}/{{file_name}}"
state: touc

- host: test #hosts自主引入变量文件
tasks:
- name: mkdir
file:
path: "{{dir_name}}"
state: directory
- name: touch
file:
path:"{{dir_name}}/{{file_name}}"
state: touc

3.3 内置变量

3.3.1 facas变量

ansible内置变量,执行剧本,有个默认的任务(task),收集每个主机的基本信息

1
2
3
4
5
6
#查询facts变量内容 
ansible ip地址 -i host -m setup
例如:
ansible 192.168.72.152 -i hosts -m setup
#注意:没有免密登录时,可以无法使用ip,可以修改hosts文件新建一个分组来控制执行设备

facts变量 说明
ansible_hostname 主机名称
ansible_memtotal_mb 内存总大小 单位(MB)
ansible_processor_vcpus cpu数量
ansible_processor_cores 核心
ansible_distribution 系统发行名称
ansible_distribution_version 系统版本
ansible_all_ipv4_addresses 默认网卡地址:eth0或者其他
ansible_default_ipv4.alias 网卡名称
ansible_date_time.date 当前时间,格式为:yyyy-mm-dd

更多变量请参数同级目录的:facts.json文件

1
2
3
4
5
6
7
8
9
10
11
12
#使用facts变量
---
- hosts: all
# gather_facts: yes #关闭facts系统变量,不收集系统数据,剧本执行速度就提高,默认打开
task:
- name: 创建文件并写入系统基本信息
lineinfile:
path: "/tmp/{{ansible_hostname}}"
create: yes
line:
- "{{ansible_hostname}}\n {{ansible_hostname}}"
- "{{ansible_hostname}}\n {{ansible_hostname}}"

3.3.2 register变量

收集上一个任务执行的结果,并声明一个变量保存起来,返回的json值

1
2
3
4
5
6
7
8
9
10
---
- hosts : all
gather_facts: no #关闭facts系统变量,不收集系统数据,剧本执行速度就提高
tasks:
- name: 01 主机名称
shell: "hostname"
register: hostname
- name: 02 输出主机名称
debug:
msg: " {{hostname.stdout}} "
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#hostname输出:
{
"stderr_lines": [],
"changed": True,
"end": "2023-09-14 17:34:30.937590",
"failed": False,
"stdout": "k8s-master", #标准输出
"cmd": "hostname",
"rc": 0, #执行
"start": "2023-09-14 17:34:30.929767",
"stderr": "",
"delta": "0:00:00.007823",
"stdout_lines": ["k8s-master"],
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
}
}

结束语:*每天学会一个小技巧🤗

2e3ffde12a4e2e0328c44885e9ff5e8ec2d81ef2