inventory文件通常用于定义要管理主机的认证信息,例如ssh登录用户名、密码以及key相关信息。如何配置inventory文件

主机
1.支持主机名通配以及正则表达式,例如web[1:3].98yz.cn
2.支持基于非标准的ssh端口,例如:web1.98yz.cn:6666
3.支持指定变量,可对个别主机的特殊配置,如登陆用户,密码等

主机组
1.支持嵌套组,例如[websevers:children],那么在sunrise模块下面的组都会被game所包含
2.支持指定变量,例如[websevers:vars]在下面指定变量

示例

[root@linux-node1 ~]# cat /etc/ansible/hosts 
[sunrise]
192.168.6.241
192.168.6.242
192.168.6.243

添加三台主机至websevers[low版]

[websevers]
linux-node2.98yz.cn
linux-node3.98yz.cn
linux-node4.98yz.cn

添加三台主机至websevers[low改良版]

[websevers]
linux-node[2:4].98yz.cn

添加三台主机至websevers[密码版]

[websevers]
linux-node2.98yz.cn ansible_ssh_pass='hm123$%^'
linux-node3.98yz.cn ansible_ssh_pass='hm123$%^'
linux-node4.98yz.cn ansible_ssh_pass='hm123$%^'

添加三台主机至websevers[密码改良版]

[websevers]
linux-node[2:4].98yz.cn ansible_ssh_pass='hm123$%^'

添加三台主机至websevers[密码拆分版]

[websevers]
linux-node2.98yz.cn
linux-node3.98yz.cn
linux-node4.98yz.cn
[websevers:vars]
ansible_ssh_pass='hm123$%^'

定义多组,多组汇总整合

[apache]
linux-node2.98yz.cn
linux-node3.98yz.cn
linux-node4.98yz.cn
[apache:vars]
ansible_ssh_pass='hm123$%^'

[nginx]
192.168.6.241
192.168.6.242
192.168.6.243
[nginx:vars]
ansible_ssh_pass='hm123$%^'

websevers组包括两个子组[apache,nginx]

[websevers:children]
apache
nginx

ansible nginx --list-hosts
ansible apache --list-hosts
ansible websevers --list-hosts

Ansible内置变量

参数 用途 例子
ansible_ssh_host 定义hosts ssh地址 ansible_ssh_host=192.168.6.240
ansible_ssh_port 定义hosts ssh端口 ansible_ssh_port=52113
ansible_ssh_user 定义hosts ssh认证用户 ansible_ssh_user=user
ansible_ssh_pass 定义hosts ssh认证密码 ansible_ssh_pass=pass
ansible_sudo 定义hosts sudo用户 ansible_sudo=www
ansible_sudo_pass 定义hosts sudo密码 ansible_sudo_pass=pass
ansible_sudo_exe 定义hosts sudo路径 ansible_sudo_exe=/usr/bin/sudo
ansible_connection 定义hosts 连接方式 ansible_connection=local
ansible_ssh_private_key_file 定义hosts 私钥 ansible_ssh_private_key_file=/root/key
ansible_ssh_shell_type 定义hosts shell类型 ansible_ssh_shell_type=bash
ansible_python_interpreter 定义hosts 任务执行python路径 ansible_python_interpreter=/usr/bin/python2.7
ansible_*_interpreter 定义hosts 其他语言解析路径 ansible_*_interpreter=/usr/bin/ruby

ansible的帮助工具

ansible—doc  模块名        查看该模块的帮助文档
ansible-doc  模块名 -s     列出模块所有选项
ansible-doc -l             列出所有模块

按照架构搭环境

实战环境:

主机名 ip 角色 业务
node01 192.168.6.240 管理端 linux-node1
node02 192.168.6.241 受控端 backup
node03 192.168.6.242 受控端 nfs
node04 192.168.6.243 受控端 web01

实现从管理机node1到其他机器的秘钥认证

1 ansible借助公钥批量管理
利用非交换工具实现批量分发公钥与批量管理服务器

[root@linux-node1 ~]# yum install sshpass -y
[root@linux-node1 ~]# ssh-keygen -t dsa  -f ~/.ssh/id_dsa  -P ""
[root@linux-node1 ~]# sshpass -phm123$%^ ssh-copy-id -i .ssh/id_dsa.pub -o StrictHostKeyChecking=no -p52113 root@192.168.6.241
[root@linux-node1 ~]# sshpass -phm123$%^ ssh-copy-id -i .ssh/id_dsa.pub -o StrictHostKeyChecking=no -p52113 root@192.168.6.242
[root@linux-node1 ~]# sshpass -phm123$%^ ssh-copy-id -i .ssh/id_dsa.pub -o StrictHostKeyChecking=no -p52113 root@192.168.6.243
[root@linux-node1 ~]# sshpass -phm123$%^ ssh-copy-id -i .ssh/id_dsa.pub -o StrictHostKeyChecking=no -p52113 root@192.168.6.244

2 安装ansible

一定要先部署epel源,然后安装且不用起服务

[root@linux-node1 ~]# yum install ansible -y

检查ansible版本

[root@linux-node1 ~]# ansible --version
ansible 2.8.1

3 配置ansible主机清单

[root@linux-node1 ~]# cat /etc/ansible/hosts 
[backup]
192.168.6.241

[nfs]
192.168.6.242

[web]
192.168.6.243

[sunrise:children]
backup
nfs
web

4 测试配置清单

[root@linux-node1 ~]# ansible sunrise -m ping

[root@linux-node1 ~]# ansible nfs -m shell -a "w"
文档更新时间: 2019-07-23 09:36   作者:李延召