RHCE学习教程之Ansible运维基础

ansible是自动化运维利器,可以通过一台Linux服务器批量控制其他服务器。

本文主要介绍RHCE学习中的一些知识点,主要包含ansible的一些用法。

关键词:RHCE Ansible

配置文件优先级

  1. ANSIBLE_CONFIG环境变量;
  2. 当前/home目录下ansible.cfg;
  3. /home目录下.ansible.cfg(隐藏文件);
  4. /etc/absible/ansible.cfg

配置非root用户提权

1
2
3
4
5
6
7
8
[defaults]         
inventory= ./hosts //自定义被控主机清单文件,这里叫hosts
remote_user= alice //被控主机使用的账户,如果不用普通用户则无需本行以及后续配置
[privilege_escalation] //提升权限的配置
become=True //是否要提权 True是要提权
become_method=sudo //提权方式用sudo
become_user=root //提权时成为root
become_ask_pass=False //提权操作时无需密码

想不起来使用下面的命令,可以生成默认的配置文件,然后根据提示取消相应的注释。

1
ansible-config init > ansible.cfg

配置Hosts

1
2
3
4
5
6
7
8
9
10
11
[test01] //组名
node1
[test02]
node2
[web]
node[3:4] //组名精简写法
[test05]
node5
[webserver:children] //children关键字,代表嵌套组,webserver包含下面的组
web

常见方式

命令方式

1
2
3
4
5
ansible all -m ping
ansible all -m command -a 'ls /opt/abc.txt' // 不支持管道,默认模块
ansible all -m shell -a 'echo "nameserver 1.1.1.1" >> /etc/reslove.conf' // 支持管道
ansible node1 -m shell -a 'echo 123 | passwd --stdin tom' // 支持管道,设置密码
ansible node1 -m script -a 'test.sh' // 将脚本上传到节点执行

playbook方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- hosts: all
tasks:
- name: ping test
ping:
- name: ping test
command: |
echo 'ok' /tmp/ok
- name: resolv
command: |
echo "nameserver 1.1.1.1" >> /etc/resolv.conf
- shell: |
echo 123 | passwd --stdin tom
- script: |
test.sh

常见模块

refer:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/

file模块

一般用来创建文件、目录、链接文件,以及删除文件

参数 用法
path 路径
state 执行动作。touch:创建普通文件,directory是目录,link是链接文件(src是源,dest是目标),absent是删除
owner 属主
group 属组
mode 权限
1
2
3
4
5
6
7
ansible node1 -m file -a 'state=touch path=/opt/123'
ansible node1 -m file -a 'state=directory path=/opt/456 '
ansible node1 -m file -a 'state=link src=/etc/hosts dest=/var/tmp/hosts'
ansible node1 -m file -a 'state=absent path=/opt/123'
ansible node1 -m file -a 'state=touch path=/opt/123 owner=alice'
ansible node1 -m file -a 'state=touch path=/opt/123 group=alice'
ansible node1 -m file -a 'state=touch path=/opt/123 mode=4777'

playbook

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
---
- hosts: all
tasks:
- file:
state: touch
path: /opt/123
- file:
state: directory
path: /opt/456
- file:
state: link
src: /etc/hosts
dest: /var/tmp/hosts
- file:
state: absent
path: /opt/123
- file:
state: touch
path: /opt/456
owner: alice
- file:
state: touch
path: /opt/1234
group: alice
- file:
path: /opt/1234
mode: 4777

yum_repository模块

为被控主机搭建yum仓库,记不住yum后面的文字可以先使用ansible-doc -l | grep yum,再使用ansible-doc yum_repository

参数 用法
name 仓库名
description 描述
file yum配置文件的名字
baseurl 软件源地址
gpgcheck gpgkey检测
gpgkey 如果检测key,指定key路径
enabled 是否开启,默认值为1是开启
1
2
ansible all -m yum_repository -a 'name=appstream description=appstrem baseurl=http://172.25.0.254/rhel9/AppStream gpgcheck=0'
ansible all -m yum_repository -a 'name=baseos description=baseos baseurl=http://172.25.0.254/rhel9/BaseOS gpgcheck=0'
1
2
3
4
5
6
7
8
9
10
11
12
13
---
- hosts: all
tasks:
- yum_repository:
name: appstream
description: appstream
baseurl: http://172.25.0.254/rhel9/AppStream
gpgcheck: 0
- yum_repository:
name: baseos
description: baseos
baseurl: http://172.25.0.254/rhel9/BaseOS
gpgcheck: 0

yum模块

使用yum安装软件

参数 用法
state present/安装,absent/卸载,latest/升级
name 指定软件包名
1
2
3
4
5
6
---
- hosts: all
tasks:
- yum:
name: net-tools
state: latest

Copy模块

将控制主机的文件拷贝到被控主机

参数 用法
src 源文件
dest 目标
owner 属主
group 属组
mode 权限
backup backup=yes,拷贝时备份控制主机的同名文件,防止被覆盖
1
ansible node1 -m copy dest=/etc/hostname src=/var/tmp/hostname owner=alice mode=1777 backup=yes
1
2
3
4
5
6
7
8
9
---
- hosts: node1
tasks:
- copy:
src: /etc/hostname
dest: /var/tmp/hostname
owner: alice
mode: 1777
backup: yes

User模块

管理账户的模块

参数 用法
state present/创建,absent/删除
name 名字
uid 定义uid号
password 定义密码
group 基本组
groups 附加组
append append=yes,追加附加组
remove remove=yes,删除用户相关文件
1
ansible node1 -m user -a 'name=zhangsan state=absent remove=yes'
1
2
3
4
5
6
7
8
9
10
11
---
- hosts: all
tasks:
- user:
name: wf09
uid: 101
password: '{{"123456"|password_hash("sha256")}}'
group: wf09
groups: admin
state: present
append: yes

Group模块

管理组

参数 用法
state present/创建,absent/删除
name 组名
gid gid组号
1
2
3
4
5
6
- hosts: all
tasks:
- group:
state: present
name: wf09
gid: 101

Service模块

管理服务

参数 用法
state started/开启服务,stopped/关闭服务,restarted/重启服务
name 服务名
1
2
3
4
5
- hosts: all
tasks:
- service:
state: restarted
name: httpd

安装collection

1
2
3
4
5
6
7
# vim ansible.cfg
collections_paths=/home/alice/ansible/collection
# 官方网站 https://galaxy.ansible.com
# bash执行
ansible-galaxy collection install http://server1.lab0.example.com/materials/ansible-posix-1.5.1.tar.gz -p /home/alice/ansible/collection
# 查看firewalld
ansible-doc ansible.posix.firewalld

pip 模块

python的包管理模块

1
2
3
4
5
---
- hosts: all
tasks:
- pip:
name: firewalld

用firewall之前还需要用yum安装firewalld安装包。

firewalld模块

管理防火墙

参数 用法
state enabled/添加规则,disabled/删除规则
service 指定服务(协议)
permanent permanent=yes,永久生效
immediate immediate=yes,立刻生效
port 22
1
2
3
4
5
6
7
8
--- 
- hosts: all
tasks:
- firewalld:
state: enabled
service: http
permanent: yes
immediate: yes

parted模块

对硬盘分区

参数 用法
device 操作的设备
label 分区表类型 mbr/msdos, gpt
number 分区序号
part_start 分区起始位置
part_end 分区结束位置
state present/创建,absent/删除,info/查看信息

1
2
3
4
5
6
7
8
9
---
- hosts: node5
tasks:
- parted:
state: present
device: /dev/vdb
number: 2
part_start: 3GiB
part_end: 4GiB

filesystem模块

赋予文件系统

参数 用法
dev 定义分区
fstype 分区表类型 mbr/msdos, gpt
number 分区序号
part_start 分区起始位置
part_end 分区结束位置
state present/创建,absent/删除,info/查看信息
1
2
3
4
5
6
7
---
- hosts: node5
tasks:
- filesystem:
dev: /dev/vdb2
fstype: xfs
force: yes

lvg模块

管理卷组

参数 用法
state present/创建,absent/删除
vg 卷组名
pvs 指定物理卷
1
2
3
4
5
6
---
- hosts: node5
tasks:
- lvg:
vg: myvg
pvs: /dev/vdb2

lvol模块

管理卷组

参数 用法
lv 逻辑卷名
size 大小
vg 空间来自哪个卷组
state present/创建,absent/删除
force 是否强制
1
2
3
4
5
6
7
8
9
10
11
12
13
---
- hosts: node5
tasks:
- lvol:
lv: mylv
size: 50m
vg: myvg
- hosts: node5
tasks:
- lvol:
lv: mylv
size: 500m # 再次执行扩容
vg: myvg

lineinfile模块

参数 用法
dest 目标文件
regexp 查找替换内容
line 要替换/添加什么内容
insertbefore 在某行上添加
insertafter 在某行下添加
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
---
- hosts: all
tasks:
- copy:
src: ./txt
dest: /tmp/txt
owner: alice
- hosts: all
tasks:
- lineinfile:
dest: /tmp/txt
regexp: txt
line: abc
- hosts: all
tasks:
- shell: |
cat /tmp/txt
register: info # 注册变量
- debug:
var: info.stdout

replace模块

可以替换文档中的某些字符串

参数 用法
path 要替换文件的路径
regexp 要匹配的内容
replace 要替换/添加什么内容
backup 是否要备份
1
2
3
4
5
6
7
- hosts: all
tasks:
- replace:
path: /home/zabbix/etc/zabbix_agentd.conf
regexp: Server=.*
replace: Server=1.1.1.1
backup: no

debug模块

可以输出常量字符,或者变量中的信息

参数 用法
msg 输出常量字符串,如果是变量要加{{ }}
var 输出变量,无需{{}}
1
2
3
4
5
6
7
8
- hosts: all
tasks:
- debug:
msg: "{{ansible_memfree_mb}}"
- debug:
var: ansible_memfree_mb
- debug:
msg: ok

setup模块

可以获取被控主机的各自信息并定义成变量

查看某个node的变量

1
ansible node1 -m setup | less
变量 用法
ansible_enp1s0.ipv4.address enp1s0网卡的ipv4地址
ansible_hostname 主机名
ansible_memfree_mb 内存剩余大小
ansible_fqdn 完整主机名
ansible_bios_version bios版本
ansible_devices.vda.size 第一块硬盘大小
ansible_devices.vda.partitions.vda1.size 第一块硬盘第一个分区大小
ansible_lvm.lvs.root.size_g 逻辑卷root的大小
ansible_kernel 内核版本信息

关闭剧本默认执行的变量收集任务

1
2
3
4
5
- hosts: node1
gather_facts: no //关闭setup任务,可以加快剧本运行速度
tasks:
- debug:
msg: "ok"