安装
# 1. 配置 Epel 源; ansible 存在于 epel 源中
yum install epel-release -y
# 2. 安装
yum install ansible -y
其他系统安装参考
EPEL(Extra Packages for Enterprise Linux) 是由 Fedora 社区打造,为 RHEL 及衍生发行版如 CentOS等提供高质量软件包的项目
# RedHat8 系列安装 epel 源
dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm -y
dnf repolist epel # 验证
# RedHat7 系列安装 epel源
sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
或 rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# 列出EPEL存储库中可用的包:
sudo dnf --disablerepo="*" --enablerepo="epel" list available
# 可以进一步过滤以检查EPEL存储库上是否有必需的包:
sudo dnf --disablerepo="*" --enablerepo="epel" list available | grep <package>
# 从EPEL存储库安装包,只需运行以下命令:
sudo dnf --enablerepo="epel" install <package>
# RedHat6 系列安装 epel源
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
# RedHat5 系列安装 epel源
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-5.noarch.rpm
yum install 软件包名进行安装了,nginx、htop、ncdu、vnstat等一些常用的软件都灰常简单的装上了。
yum install --downloadonly <软件包> # 不删除软件包
-c /etc/yum.conf # 表示指定yum配置文件地址
--installroot=/usr/local # 表示指定自定义的安装目录
--releasever=/
安装参考链接
CentOS/RHEL Linux安装EPEL第三方软件源
# 可用的epel 源:
1. https://mirrors.tuna.tsinghua.edu.cn/epel/
2. https://dl.fedoraproject.org/pub/epel/
3. http://centos.ustc.edu.cn/epel/
常用的配置文件
/etc/ansible/ansible.cfg # 默认配置文件
/etc/ansible/hosts # 默认 主机列表文件
# 主机 配置文件 主机 分组
25 [self]
26 66.66.66.18
27 66.66.66.88
28
29 [redhat]
30 66.66.66.88
31
32 [ubuntu]
33 66.66.66.18
# 切片
www[001:006].example.com
# 分组
选项及含义
选项 |
含义 |
-m [module] |
指定模块的名字, 默认为 command 模块 |
-k [--ask-pass] |
使用密码登录,提示输入SSH密码而不是假设基于密钥的验证 |
-a |
指定模块参数 |
-c |
指定连接远程主机的方式, 默认只能选择ssh 或者是 paramiko |
-i |
指定主机配置文件 |
-u |
指定远程登录用户名 |
--ssh-extra-args |
指定 SSH 连接额外参数 |
-f |
|
|
|
|
|
|
|
常用命令
# 显示主机列表
ansible <组名> --list-hosts
ansible all --list-hosts # 显示所有的主机列表
# 测试 ping
ansible <hostname 或 IP> -m ping # 默认基于 秘钥的方式
ansible <hostname 或 IP> -m ping -k # 基于 密码 的方式
ansible all -m ping # 对所有机器执行命令
ansible 66.66.66.88,66.66.66.18 -m ping # 指定多个机器, 逗号分割
ansible web,html -m ping # 多个分组, 用 逗号分割
# 并集
ansible 'web:html' -m ping # 多个分组, 用 冒号 分割, 一个分组 加 单引号也可以
# 交集
ansible 'web:&html' -m ping # 多个分组的交集
# 差集
ansible 'web:!html' -m ping # 多个分组的差集
Ansible 模块
一. 主机连通性测试(ping)
ansible all -m ping # 返回 'pong' 表示 连通
二. 查看模块的帮助信息(ansible-doc)
ansible-doc -s <模块名> # 显示 模块 命令
ansible-doc <模块名> # 显示 模块的详细信息
三. 命令模块(command)
选项及含义
命令 |
含义 |
chdir |
切换目录 |
creates |
文件存在 ,则不执行 |
removes |
文件存在, 则执行 |
free_form |
|
executable |
|
# ansible 默认使用的 模块, 不支持特殊字符,'<, >, ! | , ; && ||'
ansible all -m 'ls / '
ansible all a 'pwd'
ansible all -a 'chdir=/home ls'
ansible all -a 'removes=/tmp pwd'
四. 执行shell 命令(shell)
# 特殊字符需要转义
ansible all -m shell -a "tail -1 /etc/passwd | awk -F ':' '{print \$1}'"
# 设置密码
ansible all -m shell -a 'echo "shiwei" | passwd --stdin suosuo'
# 远程执行脚本, ruby, perl , python, shell
ansible -m shell -a 'bash scripts'
ansible all -m shell -a './scripts' # 注意文件 要有 可执行权限,
ansible all -m shell -a "/shiwei/pyth" # 远程执行 python 文件
#注意:
1. 文件要有可执行权限
2. 要考虑远程是 2 还是 3
3. 远程 python 文件必须要有 '#!/bin/env python'
# 若 python 文件第一行不指定 解释器, 则可改为如下:
ansible all -m shell -a "python /shiwei/pyth"
五. 文件传输到远程(copy)
命令及含义
模块命令 |
含义 |
backup |
当文件内容发生改变后,在覆盖之前把源文件备份,备份文件包含时间信息 |
dest |
目的地址 |
mode |
修改权限 |
group |
文件的属组 |
owner |
修改属主 |
src |
源地址 |
content |
上传内容 |
# 上传文件, 远程文件权限不受控
# 其中, 默认带 'x' 的权限自动删除, Linux 自带的安全策略,
ansible server -m copy -a 'src=/shiwei/shi dest=/shiwei/'
# 设定文件权限
ansible server -m copy -a 'src=/shiwei/shi dest=/shiwei/ mode=755'
# 设定文件属主
ansible server -m copy -a 'src=/shiwei/shi dest=/shiwei/ owner=shiwei'
# 上传本地目录文件, 对文件属性的修改, 将迭代作用到其子文件
ansible server -m copy -a 'src=/etc/init.d/ dest=/tmp owner=shiwei mode=755'
# 上传本地目录内所有文件
ansible server -m copy -a 'src=/etc/init.d/ dest=/tmp/ owner=shiwei mode=755'
# 上传内容到远程文件中, 默认追加,
ansible server -m copy -a 'content="先帝创业未半而中道崩殂" dest=/shiwei/shi'
# 通过文件的 md5 码 判断是否需要复制
六. 从远程拉取文件(fetch)
# 下载远程被 控节点的文件, 给每台机器创建一个文件夹,并保存本来的目录结构
ansible now -m fetch -a 'src=/var/log/cron dest=/tmp/shiwei'
七. 文件属性管理(file)
命令及含义
选项 |
含义 |
path |
远程主机文件位置 |
state |
|
|
|
|
|
inode : 硬盘上的地址
id : 内存上的地址
硬链接: 二次引用, inode 号相同
软连接: 快捷方式
当源文件变时 , 软连接和硬链接都会跟着发生变化
# 在远程机器创建文件夹
ansible all -m file -a 'path=/shiwei22 state=directory'
# 在远程机器创建文件
ansible all -m file -a 'path=/shiwei/a.txt state=touch'
# 在远程机器创建 软链接
ansible all -m file -a 'path=/tmp/f src=/etc/fstab state=link'
# 在远程机器创建 硬 链接
ansible all -m file -a 'path=/tmp/f src=/etc/fstab state=hard'
# src : 源文件
# 在远程机器 删除文件或者文件夹
ansible all -m file -a 'path=/tmp/f state=absent'
八. 定时任务(cron)
.... 待更新
九. 软件包的管理(yum)
YUM 源的配置
# 配置文件
[BaseOS]
name=BaseOS # 名字
baseurl=http://mirrors.ustc.edu.cn/centos/8/BaseOS/x86_64/os/ # rpm 源的地址, 可以写 http, https, ftp, samba, file,
gpgcheck=0 # 是否校验签名
enabled=1 # 是否开启,
gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-Testing-7 # 签名文件
# yum 安装包组
yum grouplist # 查看包组信息
yum groupinstall # 安装包组
子命令及含义
子命令 |
含义 |
disablerepo |
禁用仓库 |
enablerepo |
开启仓库 |
name |
包名 |
state |
状态, present --->安装, latest --->安装最新的, absent ---> 卸载软件 |
exclude |
排除某些包, 可使用正则 |
list |
|
disablerepo |
禁用指定的 yum 库 |
download_only |
只下载不安装 |
download_dir |
下载目录 |
常用命令
ansible now -m yum -a 'name=wget'
ansible now -m copy -a 'src=/etc/yum.repos.d/epel.repo dest=/etc/yum.repos.d/epel.repo'
ansible now -m yum -a 'name=nginx'
十. 程序服务的管理(service)
子命令 |
含义 |
name |
服务名 |
enabled |
是否开机自启动 |
state |
started, stopped, restarted, reloaded |
# 安装 nginx , 并设置开机自启动
ansible server -m service -a 'name=nginx enabled=yes'
十一. 用户管理(user)
子命令及含义
子命令 |
含义 |
group |
指定用户组名称 |
groups |
附加组 |
home |
指定用户家目录 |
remove |
删除用户的家目录 |
shell |
用户的登录 shell |
system |
创建一个系统用户 |
uid |
指定系统的用户 ID |
name |
用户名 |
password |
给用户添加密码(单引号) |
create_home |
是否创建家目录, 'yes' or 'on' |
# 新建一个用户, 指定用户ID, 家目录, 附加组, 登录shell
ansible now -m user -a 'name=alexsb1 uid=4000 home=/opt/alexsb1 groups=root shell=/sbin/nlogin'
# 删除用户, 但不删除用户的家目录
ansible now -m user -a 'name=alexsb1 state=absent'
# 删除用户, 并删除家目录
ansible now -m user -a 'name=alexsb1 state=absent remove=yes'
# 将明文密码进行hash加密,然后进行用户创建
ansible web_group -m debug -a "msg={{ 'szk' | password_hash('sha512', 'salt') }}" -i ./hosts
# 然后创建目录
ansible web_group -m user -a 'name=szk1 password=$6$salt$gaWhNcZweYlKQcLU1CqyY/UbYqIeUffVz6ESj87aMNfMX.xYBx0Z.67wzLN/hkkxmNut7SvkksPZ2Zlrse98m/ create_home=true shell=/bin/bash' -i ./hosts
十二. 组的管理(guoup)
子命令及含义
子命令 |
含义 |
gid |
组 ID |
name |
组 名字 |
system |
是否为系统组【yes】 |
state |
absent(移除远端主机组), present(默认, 创建远程主机组) |
# 添加一个系统组, 指定组 ID
ansible server -m group -a 'name=jingping system=yes gid=300'
# 删除组
ansible server -m group -a 'name=jingping state=absent'
十三. 脚本的管理(script)
# 用于将本机的脚本在被管理端的机器上运行
ansible -m script -a '/shiwei/xijing.sh'
ansible -m script -a 'removes=/root/shi.sh /root/wei.sh' # 判断 slave 机器文件是否存在, 存在则 执行
ansible -m script -a 'creates=/root/shi.sh /root/wei.sh' # 判断 slave 机器文件是否存在, 存在则 不执行
十四. 采集设备信息(setup)
.... 待更新
十五. 下载软件(get_url)
子命令及含义
选项 |
含义 |
url |
下载地址 |
dest |
指定下载的目标地址 |
mode |
指定权限 |
checksum |
校验加密算法(md5 或 sha256) |
.... 待更新
十六. 挂载(mount)
子命令以含义
选项 |
含义 |
present |
开机挂载,仅将挂载配置写入/etc/fstab |
mounted |
挂载设备,并将配置写入/etc/fstab |
unmounted |
卸载设备,不会清除/etc/fstab写入的配置 |
absent |
卸载设备,会清理/etc/fstab写入的配置 |
.... 待更新
防火墙和增强型 Linux(firewalld 和 selinux)
子命令及含义
YAML 语法规则
语法规则如下
# 1. 大小写敏感
# 2. 使用缩进表示层级关系
# 3. 缩进时不允许使用Tab键,只允许使用空格。
# 4. 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
# 5. '#' 表示注释,从这个字符一直到行尾,都会被解析器忽略。
参考链接
梭梭带你彻底搞懂YAML序列化语言
JSON、XML、TOML、CSON、YAML 大比拼
Ansible 剧本
ansible-playbook 为一个单独的命令
# 特性:
不管执行多少遍, 执行结果都是一样的。
剧本整体 是一个元素----字典, 开头第一行以 '-' 开头
选项及含义
选项 |
含义 |
-C |
检查, 预跑,干跑, |
--syntax-check |
语法检查 |
-f 【forcks】 |
用于做并发 |
-e |
额外的参数 |
-i |
指定 主机 文件 |
-t |
引用标签 |
剧本核心元素
# 一级【剧本文件】 核心
hosts # 主机列表
vars # 自定义参数列表
remote_user # 在远程主机上执行任务的用户
tasks # 任务列表
handlers # 由特定条件【notify】触发的任务列表
# 二级【任务】 核心元素
name # 任务名称
<module_nane> # ansible 模块 名称
notify # 钩子 任务 名称
tags # 打标签
with_items # 循环
when # 条件测试
剧本示例
# 简单用法 1 , 执行顺序, 从上往下执行任务
- hosts: server
tasks:
- name: creategroup
group: name=jingping
- name: createuser
user: name=wanganshi
# 定义 YAML 文件 模板的方式
- hosts:server
tasks:
- name: create{{ user }}
user: name={{ user }}
# 传参的方式
# 1. 在命令行中 使用 '-e' 参数 传参 proi
ansible-playbook -e 'user=libai' user.yml
# 2. 在 主机文件【/etc/ansible/hosts】 文件中传参
[server]
66.66.66.18 user=libai10
66.66.66.88 user=libai20
# 3. 在 主机文件【/etc/ansible/hosts】 文件中传参
[server:vars]
user=libai30
# 4. 在 yml 文件中 同时传参
- hosts:server
vars:
- user: libai50
tasks:
- name: create{{ user }}
user: name={{ user }}
# 5. 在 yml 文件中使用 Jinja2 语法
- hosts:server
tasks:
-name: sum
shell: echo 7+8|bc
register: user
- name: create{{ user.stdout }}
user: name={{ user.stdout }}
# 传参的 优先级
-e > playbooks > hosts 文件
参考文档
Github ansible 仓库
官方文档
Ansible中文权威指南
Centos7 源码安装 ansible
常见错误
报错: /usr/local/lib/python3.6/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.
from cryptography.exceptions import InvalidSignature
pip3 uninstall cryptography
pip3 install -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com cryptography==36.0.2
报错: [DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the controller starting with Ansible 2.12. Current version: 3.6.8 (default, Nov 16 2020, 16:55:22) [GCC 4.8.5
20150623 (Red Hat 4.8.5-44)]. This feature will be removed from ansible-core in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in
ansible.cfg
在 ansible.cfg 文件中添加如下:
[defaults]
deprecation_warnings=False