Ansible基于Python开发,集合了众多优秀运维工具的优点,实现了批量运行命令、部署程序、配置系统等功能。默认通过SSH协议进行远程命令执行或下发配置,无需任何客户端代理软件,从而使得自动化环境部署变得简单,同时支持多台主机并行管理,使得管理主机更加便携。
一、部署
1、配置yum源
# wget http://mirrors.aliyun.com/repo/Centos-7.repo
# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
2、安装软件
# yum install epel-release -y
# yum repolist
# yum install ansible -y
# yum install tree -y
3、查看配置文件结构
# tree /etc/ansible/
4、配置主机清单
# vim /etc/ansible/hosts
[webserver] # 主机分类组名
192.168.11.196 # 被管服务器
[test]
name1 ansible_ssh_host=192.168.1.111 ansible_ssh_user="root" ansible_ssh_pass="1234" ansible_ssh_port=22
# ansible webserver --list-hosts
5、生成秘钥对
# ssh-keygen -t rsa
# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.11.196
或者
# ansible webserver -m shell -a 'ls' -k # 交互输入远程机器密码,会保存在/root/.ssh/known_hosts中
6、列出所有安装模块
# ansible-doc -l (q退出)
7、列出user模块描述信息和操作动作
# ansible-doc -s user
8、command模块命令格式
# ansible 主机分类组名/all -m(指定模块) 模块名 -a(命令参数) 'uptime'
# ansible 192.168.11.196 -m command(默认模块) -a 'date'