【教程】Ansible 环境部署

操作系统:红帽 Linux8

环境


控制机:192.168.88.100

主机01:192.168.88.110

主机02:192.168.88.120

主机03:192.168.88.130

流程


为受控机添加 remote_user 与 sudo 提权

复制代码
# 为 node1/node2/node3 准备 remote_user 远程专用用户 Root(普通用户)
# 以 node1 示例:
[root@node1 ~]# useradd Root && echo "123" | passwd --stdin Root
[root@node1 ~]# vim /etc/sudoers
...
Root  ALL  NOPASSWD: ALL
...

为控制机部署 ansible 环境

复制代码
# 进入控制机
[root@control ~]# mkdir -p /home/Root/ansible
[root@control ~]# mkdir -p /home/Root/ansible/roles /home/Root/ansible/collections

# 创建 ansible.cfg
[root@control ~]# vim /home/Root/ansible/ansible.cfg
[defaults]
inventory=
roles_path=
collections_paths=

remote_user=Root

[privilege_escalation]
become=true
become_method=sudo
become_user=root
become_ask_pass=false

# 创建 Inventory
[root@control ~]# vim /home/Root/ansible/inventory
[nodes]
node1
node2
node3

# 配置 hosts
[root@control ~]# vim /etc/hosts
...
node1 192.168.88.110
node2 192.168.88.120
node3 192.168.88.130

或在 Inventory 文件中使用 node1 ansible_host=192.168.88.100 格式

为控制机远程增加 ssh 免密操作

复制代码
# 创建密钥
[root@control ~]# ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa  
...默认回车

# 为 node1/node2/node3  部署密钥
# 以 node1 为例
[root@control ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub Root@node1
...依次对 Root@node2 Root@node3 执行

测试

复制代码
[root@control ansible]# ansible all -m ping
 node2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
...显示如上结果
相关推荐
cly12 天前
Ansible自动化(十五):加解密详解
运维·自动化·ansible
cly12 天前
Ansible自动化(十二):Jinja2过滤器
运维·自动化·ansible
cly12 天前
Ansible自动化(十三):调试与优化
数据库·自动化·ansible
cly12 天前
Ansible自动化(十四):Roles(角色)
服务器·自动化·ansible
cly12 天前
Ansible自动化(十一):Jinja2模板
网络·自动化·ansible
cly12 天前
Ansible自动化(八):条件语句
运维·自动化·ansible
China_Yanhy2 天前
Ansible 工业级项目标准化架构指南 (V1.0)
架构·ansible
cly12 天前
Ansible自动化(九):循环语句
windows·自动化·ansible
cly12 天前
Ansible自动化(十):配置文件管理模块(lineinfile / blockinfile)
运维·自动化·ansible
tyatyatya3 天前
Ansible自动化配置,从入门到实战
运维·自动化·ansible