1、Ansible基础
1.1 什么是Ansible
-
Ansile首次发布于2012年,作者Michael DeHaan
Michael DeHaan也是 Cobbler的作者。
于2015年被Redhat收购 -
Ansible 是一款自动化运维工具,基于Python开发
批量系统配置
批量程序部署
批量运行命令等功能
1.1 Ansible续(1)
批量修改服务器密码
批量安装软件包
批量修改配置
1.2 环境部署
主机名 IP地址 角色
主机名 | IP地址 | 角色 |
---|---|---|
Control | 192.168.4.253 | 控制节点(master) |
Node1 | 192.168.4.1 | 被控制节点(test) |
Node2 | 192.168.4.2 | 被控制节点(proxy) |
Node3 | 192.168.4.3 | 被控制节点(web1) |
Node4 | 192.168.4.4 | 被控制节点(web2) |
1.3 配置SSH秘钥
Control控制节点
修改 /etc/hosts 、配置SSH秘钥
1 )修改 /etc/hosts 、配置SSH秘钥
cat /etc/hosts
192.168.4.253
192.168.4.1 Control
192.168.4.2 node1
192.168.4.3 node2
192.168.4.4 node3
192.168.4.5 node4
2) 配置秘钥(续1)
]# ssh-keygen -f /root/.ssh/id_rsa -N ’ ’
]# for i in node1 node2 node3 node3 node4 node5
do
ssh-copy-id $i
done
1.4 软件部署
1) Control控制节点
Python 2.6版本以上
Python模块:paramiko、PyYAML、Jinja2
2 ) 部署软件(续1)**
被控制节点
- Ansible默认通过SSH协议管理机器
- 被管理主机开启SSH服务,并与允许控制主机登录
- 被管理的主机需要安装Python
2 、Ansible 基本配置
1)配置文件
- 主配置文件 ansible.cfg
参考**/etc/ansible/ansible.cfg** - ansibel配置文件查找顺序
- 首次检测ANSIBLE_CONFIG变量定义的配置文件
- 其次检查当前目录下的 ./ansbile.cfg 文件
- 再其次检查当前用户目录下 ~/ansible.cfg文件
- 最后检查/etc/ansible/ansible.cfg 文件
2)主配置文件(续1)
]# mkdir ~/ansible
]# vim ~/ansible/ansible.cfg
[defaults]
inventory = ~/ansiblehosts #主机清单配置文件
#forks = 5 #ssh并发数量
#ask_pass = True #使用秘钥还是密码远程
#host_key_checking =False #是否效验密码
## 3) iventory主机清单文件
将被管理端主机写入一个主机列表文件(主机清单)
参考 /etc/ansible/ansible.cfg
]# cat ~/ansible/hosts
[test] #定义主机组(组名任意)
node1 #组中具体主机
[proxy]
node2
[webserver]
node[3:4]
[database]
node5
[cluster:children] #嵌套组(children为关键字)
webdatabase
webserver
4)测试
测试Ansible 环境与配置是否正常
]# cd ~/ansible
ansible]# ansible all --list-hosts 查看主机列表
]# ansible node1 -m ping 调用ping模块
3、 Ansible ad-hoc 命令行
1)基本命令
命令行语法格式
Ansible ad-hoc 是一种通过命令行批量管理的方式
- 格式: ansible 主机集合 -m 模块名 -a “参数”
- 其它参数: -k 使用密码远程、-i 指定主机列表文件
[root@control ~]# cd ~/ansible
[root@control ansible]# ansible all --list-hosts #查看所有主机列表
[root@control ~]# ansible node1 -m ping #调用ping模块
2) 快速入门
模块就是脚本(多数为python脚本)
- 多数脚本都支持参数
- 默认模块为command
[root@control ansible]# ansible node1 -m command -a "uptime"
[root@control ansible]# ansible node1 -m command -a "uname -r"
[root@control ansible]# ansible node1 -m command -a "daet"
3) 快速入门(续1)
快速获得帮助
[root@control ansible]# ansible-doc -l #列出所有模块
[root@control ansible]# ansible-doc -l | grep yum #过滤模块
[root@control ansible]# ansible-doc yum #查看帮助模块
4、 Ansible 常用模块应用案例(上)
1)shell 模块
command和shell模块的区别
- command模块的命令不启动shell, 直接通过ssh执行命令
- command 不支持bash的特性,如管道和重定向等功能
- 所有需要调用shell的功能都无法使用
[root@control ansible]# ansible test -m command -a "ps | wc -l" #报错
[root@control ansible]# ansible test -m command -a "ls &" #报错
2)shell 模块(续1)
shell模块会启动shell命令
不可以使用shell模块执行交互命令 如 vim top 等
[root@control ansible]# ansible test -m shell -a "ps | wc -l" #进程数量
[root@control ansible]# ansible test -m shell -a " who" #登录信息
[root@control ansible]# ansible test -m sehll -a " touch /tmp/txt.txt"
3)shell 模块(续2)
Ansible 使用SSH远程链接被管理主机
退出后所有状态失效
[root@control ansible]# ansible test -m shell -a "cd /tmp"
#切换目录
[root@control ansible]# ansible test -m shell -a " touch my.txt"
#创建文件(没有指定路径,是在家目录创建文件)
[root@control ansible]# ansible test -m shell -a " ls /tmp/my.txt"
#查看文件失败
[root@control ansible]# ansible test -m shell -a " chdir=/tmp touch my.txt "
#使用chdir参数切换工作目录 可创建多个文件
4) shell模块(续3)
shell 模块支持判断(creates、removes)
creates 文件名: 文件存在,不执行shell命令
removes 文件名: 文件名不存在,不执行shell命令
[root@control ansible]# ansible test -m shell -a " ssh-keygen -f ~/.ssh/id_rsa -N ' ' creates=~/.ssh/id_rsa"
#如果有秘钥文件id_rsa,则不创建秘钥(skip跳过)
[root@control ansible]# ansible test -m shell -a " unzip xx.zip removes=/bin/zip"
#如果没有安装zip的软件包,则不执行解压命令(skip 跳过)
5) script 模块
script 允许在本地写脚本,拷贝到被管理端并执行脚本
脚本不是shell脚本(如python、Perl脚本即可),可以没有 -X[root@control ansible]# cat test.sh #!/bin/bash yum -y install httpd systemctl start httpd [root@control ansible]# ansible test -m script -a " ./test.sh" #test 主机组的名称