ansible入门指南 - 安装与简单使用

发布时间 2023-08-18 16:03:08作者: Chinor

ansible 是用来自动化管理远程操作系统的工具.

ansible的三要素: 控制节点 , 被控节点, 资产清单

安装

ansible 可以通过pip直接安装

python3 -m pip install ansible --user

快速入门

创建inventory文件, 把需要管理的机器登记在该文件中, 此处假装有两台被控机器, 划分在group1组中

echo -e '[group1]\n127.0.0.1\nlocalhost' > inventory

配置免密登录主机, 根据提示输入密码

ssh-copy-id chino@127.0.0.1
ssh-copy-id chino@localhost

检查inventory的所有主机. 此处的all表示选中所有主机, 如果inventory文件中, 被控机器划分在不同的组中, 也可以使用组名 / 主机IP地址 替代all

ansible -i inventory all --list-hosts
# 看到以下输出表示配置成功
#  hosts (3):
#    127.0.0.1
#    localhost

检查group1中的主机是否在线, inventory即我们上面新建的主机清单文件

ansible -i inventory group1 -m ping
# 没问题的话将会输出以下结果
127.0.0.1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
localhost | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

-m ping指调用使用ansible的ping模块, 后面的文章会介绍模块的用法