## ansible
“`sh
[root@m01 ~]# yum install -y python3-pip
[root@m01 ~]# pip3 install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple ansible
[root@m01 ~]# echo ‘export PATH=/usr/local/bin/:$PATH’ >>/etc/profile
[root@m01 ~]# source /etc/profile
[root@m01 ~]# ansible –version
[root@m01 /etc/ansible]# cat ansible.cfg
[defaults]
interpreter_python=/usr/bin/python3
host_key_checking = False
deprecation_warnings = False
[inventory]
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]
[root@m01 /etc/ansible]# cat hosts
[web]
172.16.1.[7:8]
[backup]
172.16.1.41
[nfs]
172.16.1.31
[data:children]
backup
nfs
“`
\Typora\ansible\image-20250107123925048.png)
在主机清单中指定用户密码,
“`sh
172.16.1.9 ansible_user=root ansible_password=XZnh@95599 absible_port=22
“`
\Typora\ansible\image-20250107123941746.png)
71:host_key_checking = False 这个配置没有关闭.关闭主机认证.
### 5.1 命令与脚本类模块
#### 1 command模块
“`sh
[root@m01 ~]# ansible all -m command -a ‘ip a s ens33’
-m command是默认的,可以省略
[root@m01 ~]# ansible all -a ‘ip a s ens33’
“`
#### 2 shell模块
与command模块相似,但是shell支持特殊符号
“`sh
[root@m01 ~]# ansible all -m shell -a ‘find /etc/sysconfig/network-scripts/ -name *ens33’
[root@m01 ~]# ansible all -m shell -a ip a show ens33 | awk -F ‘[ /]+’ ‘NRR==3{print \$3}'”
“`
#### 3 script模块
“`sh
ansible all -m script -a ‘/server/scripts/ansible/scripts.sh’
“`
文件相关模块
#### 4 file模块
案例05-创建/opt/ysl.txt
“`sh
[root@m01 ~]# ansible all -m file -a ‘path=/opt/ysl.txt state=touch’
“`
案例06-创建目录/app/
“`sh
[root@m01 ~]# ansible all -m file -a ‘path=/app/a/ state=directory’
“`
案例07-创建软连接,/etc/hosts创建软连接到/opt下
“`sh
[root@m01 ~]# ansible all -m file -a ‘src=/etc/hosts path=/opt/hosts state=link’
“`
案例08-创建/ans-backup目录,所有者是rsync,权限是700
“`sh
[root@m01 ~]# ansible all -m file -a ‘path=/ans-backup owner=rsync group=rsync mode=700 state=directory’
mode参数最好写4位0700,rsync必须存在,不然会报错
“`
案例09-删除/ans-backup
“`sh
[root@m01 ~]# ansible all -m file -a ‘path=/ans-backup state=absent’
“`
#### 5 copy模块
ansible-doc -s
案例10-分发属性好的/etc/hosts文件,如果文件存在则备份下
“`sh
[root@m01 ~]# ansible all -m copy -a ‘src=/etc/hosts dest=/etc/hosts backup=yes’
可以分发文件、压缩包,不推荐目录
copy是推送
fetch是拉取
[root@m01 ~]# ansible all -m fetch -a ‘src=/etc/hostname dest=/root/’
拉取后会在/root/目录下出现以所有主机清单的IP命名的目录,目录里有etc目录,hostnamectl在etc目录下
“`
#### 6 服务管理-systemd
systemd模块适用于目前大部分的Linux系统.
service模块适用于管理旧的Linux系统
案例11-开启crond服务并设置开机启动
案例12-关闭firewalld服务不让开机启动
案例13-重启sshd服务
“`sh
[root@m01 ~/172.16.1.31/etc]# ansible all -m systemd -a ‘name=crond enabled=yes state=started’
[root@m01 ~/172.16.1.31/etc]# ansible all -m systemd -a ‘name=firewalld enabled=no state=stopped’
[root@m01 ~]# ansible all -m systemd -a ‘name=sshd state=restarted’
“`
#### 7 软件管理
yum模块
“`sh
[root@m01 ~]# ansible all -m yum -a ‘name=htop,tree,lrzsz,sshpass’
[root@m01 ~]# ansible all -m yum -a ‘name=htop state=absent’
[root@m01 ~]# ansible all -m yum -a ‘name=rsync state=present’ 安装,默认可以不写
[root@m01 ~]# ansible all -m yum -a ‘name=rsync state=latest’ 安装或更新
“`
#### 8 get_url模块
相当于wget命令
推荐在管理节点下载好,再使用copy模块分发
“`sh
[root@m01 ~]# ansible all -m get_url -a ‘url=”https://nginx.org/download/nginx-1.26.1.tar.gz” dest=/root/’
“`
#### 9 yum_repository模块
推荐写好后,再使用copy模块分发
“`sh
ansible backup \
-m yum_repository \
-a ‘name=epel
description=”Extra Packages for Enterprise Linux 7 –
$basearch”
baseurl=”http://mirrors.aliyun.com/epel/7/$basearch”
enabled=yes
gpgcheck=no
“`
#### 10 用户管理
user用户管理 : useradd,userdel,passwd,usermod修改用户信息
案例15-创建www-ans用户,uid2000的虚拟用户
“`sh
[root@m01 ~]# ansible all -m user -a ‘name=www uid=2000 shell=/sbin/nologin create_home=no state=present’
“`
案例16-批量更新密码
方法1
“`sh
[root@m01 ~]# ansible all -m user -a “name=ysl password={{‘ysl’ | password_hash(‘sha512’ , ‘ysl’)}} state=present”
将密码ysl生成哈希值,再加入ysl生成哈希,使别人不能破解
{{ ‘1’ | password_hash(‘sha512’, ‘ysl’) }}
表示1是密码,经过管道,传递给了password_hash()插件, sha512加密算法,ysl
是随机字符用于生成随机加密后的密码
“`
方法2
“`sh
[root@m01 ~]# ansible all -m shell -a ‘echo ysl007 | passwd –stdin ysl’
“`
#### 11 group模块
添加组
“`sh
[root@m01 ~]# ansible all -m group -a ‘name=ysltest gid=2005 state=present’
“`
删除组
“`sh
[root@m01 ~]# ansible all -m group -a ‘name=ysltest state=absent’
“`
#### 12 cron模块
作用:用于管理系统的定时任务.
替代了crontab -e或vi /var/spool/cron/xxx用户功能
\Typora\ansible\image-20250107124106193.png)
创建定时任务
“`sh
[root@m01 ~]# ansible all -m cron -a ‘name=”test” job=”echo ysl >> /tmp/ysl.txt” state=present’
name是注释,如果是ansible添加的,可以通过注释来删除定时任务
“`
删除定时任务
“`sh
[root@m01 ~]# ansible all -m cron -a ‘name=”test” state=absent’
“`
#### 13 mount模块
案例17-挂载nfs01已有的共享目录到web01上的/mnt下
“`sh
[root@m01 ~]# ansible web -m mount -a ‘fstype=nfs src=172.16.1.31:/data/ path=/mnt state=mounted’
“`
| state参数可使用的值 | 含义 |
| ——————- | ———————– |
| absent | 卸载并修改fstab |
| unmounted | 卸载不修改/etc/fstab |
| present | 仅修改/etc/fstab 不挂载 |
| mounted | 挂载并修改/etc/fstab |
| remounted | 重新挂载 |
卸载
“`sh
ansible 172.16.1.7 -m mount -a ‘path=/mnt/ state=absent’
“`
### 5.2 剧本与变量
yaml格式的文件:空格,冒号
“`sh
—
– hosts: all
tasks:
– name: 01 打开冰箱门
shell: echo 01 >> /tmp/bingxiang.log
– name: 02 放进冰箱
shell: echo 02 >> /tmp/bingxiang.log
– name: 03 关上冰箱
shell: echo 03 >> /tmp/bingxiang.log
“`
### 5.3 案例02-创建目录并分发
步骤:
1.创建目录/server/files
2.管理机的/etc/hosts文件发送过去
“`sh
[root@m01 /server/scripts/playbook]# cat 02.dist_file.yml
—
– hosts: all
tasks:
– name: 01 创建目录
file:
path: /server/files/
state: directory
– name: 02 分发文件
copy:
src: /etc/hosts
dest: /server/files/
“`
### 5.4 案例03-批量添加定时任务
定时同步时间ntp.aliyun.com
“`sh
[root@m01 /server/scripts/playbook]# cat 03.cron_ntp.yml
—
– hosts: all
tasks:
– name: 批量同步时间
cron:
name: “sync time”
job: “/sbin/ntpdate ntp.aliyun.com > /dev/null 2>&1”
state: present
[root@m01 /server/scripts/playbook]#
“`
### 5.5 案例04-nfs服务
nfs服务端:在xxx上部署nfs服务,共享/backup-nfs目录,all_squash,匿名用户:www-ans uid 2999 2999
nfs客户端:web挂载 /ans-upload目录挂载nfs服务端共享的/backup-nfs(永久挂载)
服务端流程:
\1. 部署nfs-utils
\2. 修改配置文件 /etc/exports
/backup-nfs 172.16.1.0/24(rw,all_squash)
\3. 创建共享目录,改所有者
\4. 启动服务rpcbind,nfs
客户端:
\1. 安装nfs-utils
\2. 挂载与永久挂载
“`yaml
[root@m01 /server/scripts/playbook]# cat 04.nfs_mount.yaml
—
– hosts: nfs
tasks:
– name: 01.部署nfs-utils
yum:
name: rpcbind,nfs-utils
state: present
– name: 02.修改配置文件
lineinfile:
path: /etc/exports
line: /nfs/ans 172.16.1.0/24(rw,anonuid=2999,all_squash)
– name: 03.创建组
group:
name: www-ans
gid: 2999
state: present
– name: 04.创建用户
user:
name: www-ans
group: www-ans
uid: 2999
create_home: no
shell: /sbin/nologin
state: present
– name: 05.创建并修改数组
file:
path: /nfs/ans
owner: www-ans
group: www-ans
mode: 0755
state: directory
– name: 06.启动rpc服务
systemd:
name: rpcbind
state: started
enabled: yes
– name: 07.启动nfs服务
systemd:
name: nfs
state: reloaded
enabled: yes
– hosts: web
tasks:
– name: 01.安装服务
yum:
name: nfs-utils,rpcbind
state: present
– name: 02.创建组
group:
name: www-ans
gid: 2999
state: present
– name: 04.创建用户
user:
name: www-ans
group: www-ans
uid: 2999
create_home: no
shell: /sbin/nologin
state: present
– name: 05.创建并修改数组
file:
path: /app/web/ansv2-upload/
owner: www-ans
group: www-ans
mode: 0755
state: directory
– name: 05.挂载
mount:
fstype: nfs
src: 172.16.1.31:/nfs/ans
path: /app/web/ansv2-upload/
state: mounted
“`
### 5.6 剧本里创建文件添加多行
“`yaml
– name: Insert multiple lines into a file
blockinfile:
path: /path/to/your/file
block: |
This is the first line
This is the second line
This is the third line
create: yes
“`
### 5.7 ansible变量
#### 1 vars
ansible 变量命名规则:
\1. 与shell类似,不能以数字开头.
\2. 变量中仅包含_下划线即可.不要包含其他特殊符号.
案例05-批量创建/ysl/ysl/upload
“`yaml
[root@m01 /server/scripts/playbook]# cat 05.mk_dir.yaml
—
– hosts: all
vars:
dir: /ysl/ysl/upload
file: ysl.txt
tasks:
– name: mkdir directory
file:
path: “{{ dir }}”
state: directory
– name: touch file
file:
path: “{{ dir }}/{{ file }}”
state: touch
“`
如果变量是某个选项的开头,则变量引用的时候需要加上双引号
如果不是开头,则可以不用添加引号
多个变量在一起的时候只需要一个引号,多添加引号会报错
“{{ dir }}/{{ file }}”这个是对的,”{{ dir }}”/”{{ file }}”这个是错的
vars局限性:
vars只能在当前剧本中生效
使用两个花括号包起来,花括号中间有空格
在hosts和tasks中间使用
#### 2 变量文件vars_files
在hosts和tasks中间使用
例:
“`yaml
– hosts: web
vars_files:
– ./vars.yml
tasks:
vars.yaml格式
可以顶格写
cat vars.yaml
dir: /server/ans-test-01/
file: test02.txt
“`
#### 3 组变量
在剧本目录下创建一个all目录,再创建vars.yml
或者根据主机清单的分组建多个目录,然后再各个目录创建vars.yaml
“`yaml
[root@m01 /server/scripts/playbook]# tree group_vars/
group_vars/
├── all
│ └── vars.yml
├── backup
│ ├── nfs
│ └── vars.yml
└── web
└── vars.yml
“`
“`yaml
案例07-测试组变量
vim 07.group_vars_test.yml
—
– hosts: all
tasks:
– name: 01.创建目录
file:
path: “{{ dir }}”
state: directory
– name: 02.创建用户
user:
name: “{{ user }}”
shell: /sbin/nologin
create_home: no
state: present
– hosts: web
tasks:
– name: 01.创建目录
file:
path: “{{ web_upload }}”
state: directory
– hosts: nfs
tasks:
– name: 01.创建目录
file:
path: “{{ nfs_dir }}”
state: directory
创建vars文件
[root@m01 /server/scripts/playbook]# cat group_vars/all/vars.yml
#1.all
dir: /server/scripts/
user: www
#2.nfs
nfs_dir: /ans-data/web/
#3.backup
backup_dir: /ans-backup/nfsbackup/
#4.web
web_upload: /app/ans/web/upload/
“`
#### 4 facts变量
facts变量相当于是ansible内置变量,存放被管理机器的基本信息
运行剧本的时候会运行:**Gathering Facts** task任务
**Gathering Facts** :ansible收集被管理端基本信息.cpu,内存,磁盘,网络,系统
我们在剧本中使用facts变量,未来如果不用可以关闭gather facts功能,加速剧本运行效果
“`sh
查看某个主机facts变量
[root@m01 /server/scripts/playbook]# ansible 172.16.1.61 -m setup
“`
常用facts变量
“`sh
ansible_hostname #主机名
ansible_memtotal_mb #内存大小
ansible_processor_vcpus #cpu总数
ansible_default_ipv4.address #默认网卡ip
ansible_distribution #系统版本
debian
ansible_distribution_version #系统版本
ansible_date_time.date #年月日
“`
##### 1 案例08-输出常用基本信息到屏幕,系统基本信息写入到/etc/motd中
“`sh
vim 08.test_facts_vars.yml
—
– hosts: all
tasks:
– name: 01.调试变量
debug:
msg: |
“主机名:{{ ansible_hostname }}”
“内存:{{ ansible_memtotal_mb }}”
“cpu:{{ ansible_processor_vcpus }}”
– name: 02.保存到文件中
lineinfile:
path: /etc/motd
state: present
backup: true
create: true
line: |
主机名:{{ ansible_hostname }}
内存:{{ ansible_memtotal_mb }}
cpu核数:{{ ansible_processor_vcpus }}
系统:{{ ansible_distribution }}
系统版本:{{ ansible_distribution_version }}
[root@m01 /server/scripts/playbook]# ansible all -m shell -a “cat /etc/motd”
“`
debug模块用于在剧本中输出与调试,一般用于输出变量内容.
|表示下面的内容是多行,用于输出或写入多行到文件中,注意索引对齐
debug:
msg:
相当于 echo,msg后面也要加引号
##### 2 案例09-ans-facts变量和判断
麒麟系统安装cowsay
ubt系统安装cmatrix
“`yaml
– hosts: all
tasks:
– name: 麒麟系统安装cowsay
yum:
name: cowsay
state: present
when: ansible_distribution is match(“Kylin”)
– name: ubt系统安装cmatrix
apt:
name: cmatrix
state: present
when: ansible_distribution is match(“Ubuntu”)
– name: 输出
debug:
msg:”{{ ansible_distribution }}”
“`
\1. 通过facts变量获取系统的基本信息
\2. 通过facts变量获取信息并进行判断
\3. 如果不需要可以进行关闭,加速剧本的运行( gather_facts: false )
\4. 修改ansible.cfg #gathering = implicit去掉注释,永久关闭,后期剧本中需要开启gather_facts: true
“`sh
yes or no 还是True or false
在使用上是一致的,表示的是真(开启,启动),假(关闭).
布尔类型的数据:真,假.
真:yes,True,true
假:no,False,false
根据每个模块选项的说明去使用即可
“`
##### 3 关闭facts变量,加速剧本运行
默认开启,不使用的时候临时关闭: gather_facts: false hosts: tasks之间
彻底关闭,使用的时候在剧本中临时开启 gather_facts: true
“`sh
cat>/etc/ansible/ansible.cfg<