ansible 环境部署
ansible环境准备
角色 | IP |
---|---|
ansible-server | 192.168.5.3 |
node01 | 192.168.5.4 |
node02 | 192.168.5.5 |
node03 | 192.168.5.6 |
node04 | 192.168.5.7 |
- 配置主机hosts问文件
注 :批量操作所有主机
[root@ansible-server ~]# vim /etc/hosts +
192.168.5.3 ansible-server
192.168.5.4 node01
192.168.5.5 node02
192.168.5.6 node03
192.168.5.7 node04
- 配置免密钥互信
[root@ansible-server ~]# su - student
[student@ansible-server ~]$ ssh-keygen -f ~/.ssh/id_rsa -P '' -q
[student@ansible-server ~]$ ssh-copy-id student@node01
[student@ansible-server ~]$ ssh-copy-id student@node02
[student@ansible-server ~]$ ssh-copy-id student@node03
[student@ansible-server ~]$ ssh-copy-id student@node04
ansible 安装
注:此次环境使用centos-8.2操作系统
#安装ansible 2.8.0
[root@ansible-server ~]# dnf install ansible-2.8.0-1.el8ae.noarch.rpm sshpass-1.06-3.el8ae.x86_64.rpm -y
#查看anisble软件是否安装成功
[root@ansible-server ~]# yum list installed platform-python
Installed Packages
platform-python.x86_64 3.6.8-23.el8 @anaconda
#查看ansible安装版本
[root@ansible-server ~]# ansible --version
ansible 2.8.0
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, Apr 16 2020, 01:36:27) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
编辑ansible配置文件
dnf 方式安装ansible,默认会生成ansible配置文件
本次案例,我们自定义编辑配置文件,完成ansible的基本配置
- 编辑ansible配置文件
#编辑ansible配置文件
[root@ansible-server ~]# su - student
[student@ansible-server ~]$ mkdir ansible/
[student@ansible-server ~]$ cd ansible/
[student@ansible-server ansible]$ vim ansible.cfg
[defaults]
inventory = ./inventory
remote_user = user
ask_pass = false
roles_path = roles
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
#编辑清单文件
[student@ansible-server ansible]$ vim inventory
[dev]
node01
[test]
node02
[prod]
node03
node04
[balancers]
node02
[webserver:children]
prod
#创建校色文件目录
[student@ansible-server ansible]$ mkdir roles/
测试连通性
[student@ansible-server ansible]$ ansible all -m ping -o
node02 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"ping": "pong"}
node04 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"ping": "pong"}
node03 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"ping": "pong"}
node01 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"ping": "pong"}
[student@ansible-server ansible]$ ansible localhost -m ping -o
localhost | SUCCESS => {"changed": false,"ping": "pong"}
总结
提示:ansible 管理端支持三种方式连接被管理端:
- local
- ssh
- zeromq
注:ansible 默认有localhost 连接,这种连接不包含在all和ungrouped组中,采用local方式连接