Ansible安装配置管理
Ansible优势和应用场景
Ansible:开源部署工具
开发语言:Python
特点:ssh协议通讯,全平台,无需编译,模块化部署管理
作用:推送playbook进行远程节点快速部署
优势:
轻量级无客户端(Agentless)
开源免费,学习成本低,快速上手
使用playbook作用核心配置架构,统一脚本格式,批量化部署
完善的模块化扩展,支持目前主流的开发场景
强大的稳定性和兼容性
Ansible安装配置
Ansible的两种安装模式(cenOS7)
1.yum包管理安装
代码语言:javascript复制# yum –y install ansible
代码语言:javascript复制
2.Git源码码安装(推荐)
Ansible2.5 Python3.6安装步骤:
1)预先安装py3.6
代码语言:javascript复制# wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz
#指定目录编译
# tar xvf Python-3.6.5.tar.xz
#cd /Python-3.6.5
# ./configure --prefix=/usr/local --with-ensurepip=install --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
代码语言:javascript复制
遇到错误:configure: error: no acceptable C compiler found in $PATH
解决:yum install -y gcc
# make && make altinstall
遇到错误:make: *** [altinstall] Error 1
解决:yum install zlib-devel bzip2-devel sqlite sqlite-devel openssl-devel
https://stackoverflow.com/questions/40355611/python-3-4-installation-error-1-oserror-errno-22-invalid-argument
2)安装virtualenv
代码语言:javascript复制#查看pip路径
# which pip3.6
/usr/local/bin/pip3.6
#给usr/local/bin/pip3.6做软连接
# ln -s /usr/local/bin/pip3.6 /usr/local/bin/pip
# pip install virtualenv
代码语言:javascript复制
3)创建ansible账号并安装py3.6版本和virtualenv实例
代码语言:javascript复制# useradd deploy
# su - deploy
# virtualenv -p /usr/local/bin/python3.6 .py3-a2.5-env
4)git源代码安装Ansible2.5
代码语言:javascript复制#查看deploy用户是否安装git
# su - deploy
$ which git
/usr/bin/git
#使用root用户安装git依赖包
# yum -y install git nss curl
#deploy git安装完成后(使用root)
# cd /home/deploy/.py3-a.2.5-env
# git clone https://github.com/ansible/ansible.git
5)加载py3.6 virtualenv环境
代码语言:javascript复制# source /home/deploy/.py3-a2.5-env/bin/activate
6)安装Ansible依赖包
代码语言:javascript复制#安装paramiko pyYAML jinja2依赖包
# pip install paramiko pyYAML jinja2
#进入ansible目录下将ansible源代码切换到2.5版本
# cd .py3-a2.5-env/ansible
# git checkout stable-2.5
7)在py3.6虚拟环境下加载ansible2.5
代码语言:javascript复制# source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q
8)验证ansible2.5
代码语言:javascript复制# ansible --version
代码语言:javascript复制
到此ansible2.5版本已经成功的在py3.6虚拟环境下完成安装;
CentOS6.7下Ansible部署:http://www.showerlee.com/archives/1649
Ansible中文权威指南:http://www.ansible.com.cn/index.html
https://blog.csdn.net/wating_jx/article/details/71084592
Ansible常用模块操作和编写规范
playbook框架与格式
Test playbook 文件结构清单:
inventory/ ---->server详细清单目录
testenv/ ---->具体清单与变量声明文件
roles/ ---->roles任务列表
testbox/
tasks/
main.yml ---->testbox主任务文件
deploy.yml ---->playbook任务入口文件
详细目录testenv
[testservers] ---->server组列表
test.example.com ---->目标部署服务器主机名
[testservers:vars] ---->server组列表参数
server_name= test.example.com
user=root ---->目标主机key/value参数
output=/root/test.txt
主任务文件main.yml
任务入口文件deploy.yml
远程ansible主机,编写playbook框架
代码语言:javascript复制# ssh root@47.98.198.241
# su - deploy
#加载py3.6虚拟环境
# source .py3-a2.5-env/bin/activate
#同时加载ansible2.5版本
# source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q
#验证加载效果
# ansible-playbook --version
#编写playbook框架
# mkdir playbook
# cd /playbook
# mkdir inventory
# mkdir roles
# cd inventory
# vi testenv
[testservers]
report.example.com
[testservers:vars]
server_name= report.example.com
user=root
output=/root/test.txt
# cd roles
# mkdir testbox
# cd testbox
# mkdir tasks
# cd tasks
# vi main.yml
- name: print server name and user to remste testbox
shell: "echo 'Currently {{ user }} is logining {{ server_name }}' > {{ output }}"
# cd ../../..
#pwd
/home/deploy/playbook
# vi deploy.yml
- hosts: "testservers"
gather_facts: true
remote_user: root
roles:
- testbox
代码语言:javascript复制#查看树形文件夹
#切换deploy用户,创建ssl秘钥认证
# ssh-keygen -t rsa
#指定deploy用户公钥
代码语言:javascript复制# ssh-copy-id -i /home/deploy/.ssh/id_rsa root@report.example.com(test.example.com是ansible本机DNS)
# ansible-playbook -i inventory/testenv ./deploy.yml (命令手动敲) 在/home/deploy/playboo目录下执行
遇到错误:bad permissions: ignore key: /home/deploy/.ssh/id_rsa
解决:
代码语言:javascript复制chmod 755 ~/.ssh/
chmod 600 ~/.ssh/id_rsa ~/.ssh/id_rsa.pub
chmod 644 ~/.ssh/known_hosts
代码语言:javascript复制#测试目标主机
authorized_key是目标主机.ssh的秘钥