Ansible笔记
2024-07-29 00:09:41 0 举报
AI智能生成
Ansible笔记是一种用于自动化IT任务的开源软件,它支持配置管理、应用部署、服务编排等多种功能。通过Ansible,你可以将日常任务自动化,提高工作效率。其核心内容包括编写Playbook,使用模块,理解Ansible的工作流程等。Ansible采用YAML格式的文件进行配置,易于阅读和维护。此外,Ansible还支持多种操作系统,如Linux、Windows等,并且提供了丰富的第三方模块库,可以满足各种定制化的需求。
作者其他创作
大纲/内容
简介
简称:Ans
基于Python
自动化批量管理工具
适用于在多台机器上执行重复指令,批量部署、批量分发、批量更新等
常用文档地址:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/index.html#plugins-in-ansible-builtin
环境准备
1.管理端与被管理端配置好SSH密钥认证
目的:管理端可以免密登录被管理端
可以写个shell脚本生成ssh密钥对,然后通过ssh-copy-id分发到从机
#!/bin/bash
#author: lainxxu
#desc 便利的批量创建和分发公钥
echo '----------------------------------------'
echo '1. 创建 key'
echo '----------------------------------------'
ssh-keygen -f ~/.ssh/id_rsa -P ''
echo '----------------------------------------'
echo '2. 分发 pub key'
echo '----------------------------------------'
# 从文件读取IP地址
while IFS=' \t' read -r ip user pass; do
echo "配置 $ip with user $user"
sshpass -p"$pass" ssh-copy-id -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no $user@$ip
done < ip_addresses.txt
#author: lainxxu
#desc 便利的批量创建和分发公钥
echo '----------------------------------------'
echo '1. 创建 key'
echo '----------------------------------------'
ssh-keygen -f ~/.ssh/id_rsa -P ''
echo '----------------------------------------'
echo '2. 分发 pub key'
echo '----------------------------------------'
# 从文件读取IP地址
while IFS=' \t' read -r ip user pass; do
echo "配置 $ip with user $user"
sshpass -p"$pass" ssh-copy-id -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no $user@$ip
done < ip_addresses.txt
最好再写个批量检查从机是否ready的脚步
#!/bin/bash
#author: lainxxu
#desc 批量登录到远程主机
echo '----------------------------------------'
echo '批量执行命令'
echo '----------------------------------------'
# 从文件读取IP地址
while IFS=' \t' read -r ip; do
ssh -p 36000 root@$ip $@
done < ip_addresses.txt
#author: lainxxu
#desc 批量登录到远程主机
echo '----------------------------------------'
echo '批量执行命令'
echo '----------------------------------------'
# 从文件读取IP地址
while IFS=' \t' read -r ip; do
ssh -p 36000 root@$ip $@
done < ip_addresses.txt
2.yum install -y ansible
3.设置主机清单
支持分组管理
[lain]
127.0.0.1
[lain01]
127.0.0.2
127.0.0.1
[lain01]
127.0.0.2
通过修改/etc/ansible/ansible.cfg可以自定义受控端端口号
[defaults]
remote_port = 2222
remote_port = 2222
4.使用ansible测试受控端是否ready
ansible lain -m ping
Ans基础(ad-hoc,即命令行运行模块功能)
分组功能:Inventory文件(通常指hosts)
组
[lain]
127.0.0.1
[lain01]
127.0.0.2
127.0.0.1
[lain01]
127.0.0.2
子组
[total:children]
lain
lain01
lain
lain01
格式
ansible+分组名+'-m'+模块名+'-a'+动作名
-m: module
-a: action
ansible all -m ping
模块 -m
ping
command
仅支持简单名,不支持特殊符号
默认命令为command,可不指定
shell
支持更多的特殊符号和执行脚本(脚本需要在受控端存在,所以执行脚本使用script模块)
script
支持传输脚本到受控端并执行脚本
file:管理文件或目录
path:路径、必填
src:一般用来在创建软连接时指定源文件
state:状态/模式
state=directory:创建目录
ansible all -m file -a 'path=/lain state=directory'
state=file(默认)更新文件,如果文件不存在也不会创建
ansible all -m file -a 'path=/lain/file.txt state=file'
state=link:创建软连接
ansible all -m file -a 'src=/lain/file.txt path=/tmp/file.txt.soft state=link'
state=touch:创建文件
ansible all -m file -a 'path=/lain/file.txt state=touch'
state=absent:删除目录、文件、软连接
...
示例:创建文件并指定权限
ansible all -m file -a 'path=/lain/file.txt -owner=root group=root mode=0755 state=touch'
copy:类似scp,远程传输模块
src:源文件
dest:目标
backup:backup=yes 会在覆盖前备份文件
owner
group
mode
systemd:服务管理
name:指定服务名
enabled:控制服务开启自启动 enabled=yes、no
state:表示服务开关重启
state=started
state=stopped
state=reloaded 重加载配置文件:sshd、nfs、、nginx等
state=restarted
daemon-reload
关闭防火墙:ansible all -m systemd -a 'name=firewalld enabled=no state=stopped'
yum软件包管理
yum_repository
添加nginx的yum源:ansible all -m yum_repository -a 'name=nginx baseurl="" gpgcheck=no enabled=yes'
yum
安装一个软件:ansible all -m yum -a 'name=lrzsz state=installed'
get_url(类似wget功能)
下载文件到目录(目录不存在不会创建)
ansible all -m get_url -a 'url="https://xxx.com/test/t.txt" dest="/data/home/"'
mount:文件系统挂载,nfs挂载
fstype:指定文件系统类型,如nfs
src: 源地址:如nfs服务地址eg 172.16.1.31/data
path:挂载点,要把源挂载到哪里
state
absent:卸载(umount)并修改挂载fstab,清理配置
unmounted: 卸载,不修改/etc/fstab
present: 仅修改/etc/fstab不挂载
mounted: 挂载(用mount命令)并修改/etc/fstab,永久挂载
remounted: 重新挂载
cron定时任务模块
name:定时任务名字,对应注释内容
时间:分、时、日、月、周
job:执行的命令
state
present (默认)添加定时任务
absent:删除
lineinfile:用于确保一个特定的行存在于文件中,
或者替换一个文件中符合特定正则表达式的行。
这个模块常用于修改配置文件或其他文本文件。
它提供了一种方便的方式来确保文件中包含
(或不包含)特定的内容,而不需要替换整个文件
或者替换一个文件中符合特定正则表达式的行。
这个模块常用于修改配置文件或其他文本文件。
它提供了一种方便的方式来确保文件中包含
(或不包含)特定的内容,而不需要替换整个文件
....还有很多
动作 -a
实际执行的命令或各个模块下的选项,如command的可以执行简单shell:hostname
*剧本playbook(基于文件部署、分发)
简介
通过yml文件编排一系列操作进行统一管理,
类似与pipline或github的actions这样的任务编排功能。
提前编排好配置,一个命令执行批量重复或复杂操作
类似与pipline或github的actions这样的任务编排功能。
提前编排好配置,一个命令执行批量重复或复杂操作
playbook vs shell脚本?
playbook可以并行批量部署、批量分发、批量管理
shell更偏向某一台机器的各种操作、是一台一台执行
playbook效率更高,但是shell也不是不能实现playbook功能
管理的设备较多时 我们有playbook 为什么还去使用shell...循环呢?
shell更偏向某一台机器的各种操作、是一台一台执行
playbook效率更高,但是shell也不是不能实现playbook功能
管理的设备较多时 我们有playbook 为什么还去使用shell...循环呢?
基本格式(yml):
- hosts: all
vars:
filename: test.txt
tasks:
- name: touch file
shell: touch /data/{{filename}}
- hosts: all
vars:
filename: test.txt
tasks:
- name: touch file
shell: touch /data/{{filename}}
执行:ansible-playbook -i hosts test.yml
检查 不执行:ansible-playbook -i hosts -C test.yml
-i指定主机文件
-C检查真正执行时都有哪些操作,实际不执行
--syntax-check检查文件的格式是否有问题
--skip-tags跳过指定的tags
--tags只执行指定的tags
-C检查真正执行时都有哪些操作,实际不执行
--syntax-check检查文件的格式是否有问题
--skip-tags跳过指定的tags
--tags只执行指定的tags
include:可以将一个比较大的剧本文件拆分成多个,并且关联起来
- hosts:nfs
tasks:
- include_task: a.yml
- hosts: web
tasks:
- include_task: b.yml
tasks:
- include_task: a.yml
- hosts: web
tasks:
- include_task: b.yml
实战
在一组主机上部署nfs服务端,并在另一组主机上挂载
https://blog.csdn.net/Tianyi_0801i/article/details/140757543
ip_addresses.txt内容
172.16.1.51 root 123456
172.16.1.52 root 123456
172.16.1.56 root 123456
172.16.1.51 root 123456
172.16.1.52 root 123456
172.16.1.56 root 123456
0 条评论
下一页
为你推荐
查看更多