Ansible 是一款功能强大的 IT 自动化工具,通过无代理机制实现高效的配置管理、应用部署、任务执行以及多节点间的 IT 编排。其主要特点包括:
在安装时,需根据使用环境选择适配方案:
CentOS 7 在生产环境中依然应用广泛,使用 yum
进行安装:
yum install epel-release -y
yum install ansible -y
CentOS 9 为官方维护版本,推荐使用 dnf
进行安装:
dnf install epel-release -y
dnf install ansible -y
Ubuntu 系统上可通过 APT 包管理安装:
sudo apt update
sudo apt install ansible -y
安装完成后,可以运行下列命令确认:
ansible --version
Inventory 文件是用于定义目标节点和管理节点组的核心文件。
Inventory 文件默认位置为 /etc/ansible/hosts
,其格式如下:
[group1]
host1 ansible_host=192.168.1.10 ansible_user=root
host2 ansible_host=192.168.1.11 ansible_user=admin
[group2]
host3 ansible_host=192.168.1.12 ansible_port=2222
group1
和 group2
:节点分组。ansible_host
:目标节点的 IP 地址。ansible_user
:用于连接的用户。ansible_port
:指定目标节点的 SSH 端口,默认为 22。公共变量可以为全局或指定组设定默认值:
[all:vars]
ansible_python_interpreter=/usr/bin/python3
通过分组进行节点的根据配置:
[group1:children]
subgroup1
subgroup2
动态 Inventory 的优点在于能够根据实时环境生成节点清单,避免手动维护带来的繁琐和错误。以下为动态配置的步骤和注意事项:
以下是一个简单的 Python 脚本示例,用于生成动态 Inventory 文件:
import argparse
import json
def lists():
r = {}
h = ['192.168.119.13' + str(i) for i in range(1, 3)]
hosts = {'hosts': h}
r['mysql'] = hosts
return json.dumps(r, indent=4)
def hosts(name):
cpis = {
'ansible_ssh_port': 22,
'ansible_ssh_user': 'root',
'ansible_ssh_pass': '123456'
}
return json.dumps(cpis)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
'-l',
'--list',
help="hosts list",
action='store_true'
)
parser.add_argument(
'-H',
'--host',
help='hosts vars'
)
args = vars(parser.parse_args())
if args['list']:
print(lists())
elif args['host']:
print(hosts(args['host']))
else:
print(parser.print_help())
将上述脚本保存为 dynamic_inventory.py
,并赋予执行权限:
chmod +x dynamic_inventory.py
运行以下命令验证脚本功能:
./dynamic_inventory.py --list
./dynamic_inventory.py --host <hostname>
在 Ansible 中使用动态 Inventory:
ansible -i ./dynamic_inventory.py all -m ping
在实际生产中,可以结合工具如 JumpServer 动态生成 Inventory。以下是基于 JumpServer 数据库的 Python 示例:
import pymysql
connection = pymysql.connect(
host='jumpserver_db_host',
user='db_user',
password='db_password',
database='jumpserver_db'
)
try:
with connection.cursor() as cursor:
cursor.execute("SELECT ip, username FROM assets")
hosts = cursor.fetchall()
with open('/etc/ansible/hosts', 'w') as inventory:
inventory.write('[dynamic]\n')
for host in hosts:
inventory.write(f"{host[0]} ansible_user={host[1]}\n")
finally:
connection.close()
此脚本适用于需要与 JumpServer 或其他外部系统集成的场景。
Ad-Hoc 命令是 Ansible 最基础的操作工具,能够快速执行临时任务。以下是详细使用方法和常用模块介绍:
Ad-Hoc 命令格式如下:
ansible <pattern> -m <module> -a <arguments>
<pattern>
:目标主机模式(如 all
或指定组)。<module>
:执行任务的模块。<arguments>
:模块的参数。ansible all -m ping
ansible all -a "df -h"
ansible all -m copy -a "src=/local/file dest=/remote/path"
ansible all -m file -a "path=/remote/file state=absent"
ansible all -m yum -a "name=httpd state=present"
ansible all -m service -a "name=httpd state=started"
通过 Ad-Hoc 命令与模块的灵活组合,Ansible 能够快速响应多样化的管理需求。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。