因为工作的缘故接触并积极推动 Ansible 在企业级生产环境的落地,独立承担并实现了《基于 ansible 的主机自动化配置管理》项目,此前也先后接触过 Puppet 和 SaltStack,本文不会讨论开源或者自主研发方案的优劣,重点是和大伙儿分享自己在 ansible 技术领域积累的一些项目实战经验,如果大家遇到任何问题也欢迎通过留言或者其他方式进行互动,我尽力做到有效回复。
Ansible is Simple IT Automation
2018 年 05 月 15 日 - 初稿
阅读原文 - https://wsgzao.github.io/post/ansible/
扩展阅读
ansible - https://docs.ansible.com/
Ansible 相关的书籍在逐步增多,由于 Ansible 版本迭代更新频率高但学习成本低,个人建议书为辅,官方文档为主
Ansible is an IT automation tool. It can configure systems, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero downtime rolling updates.
Ansible ’ s main goals are simplicity and ease-of-use. It also has a strong focus on security and reliability, featuring a minimum of moving parts, usage of OpenSSH for transport (with other transports and pull modes as alternatives), and a language that is designed around auditability by humans – even those not familiar with the program.
We believe simplicity is relevant to all sizes of environments, so we design for busy users of all types: developers, sysadmins, release engineers, IT managers, and everyone in between. Ansible is appropriate for managing all environments, from small setups with a handful of instances to enterprise environments with many thousands of instances.
Ansible manages machines in an agent-less manner. There is never a question of how to upgrade remote daemons or the problem of not being able to manage systems because daemons are uninstalled. Because OpenSSH is one of the most peer-reviewed open source components, security exposure is greatly reduced. Ansible is decentralized – it relies on your existing OS credentials to control access to remote machines. If needed, Ansible can easily connect with Kerberos, LDAP, and other centralized authentication management systems.
This documentation covers the current released version of Ansible and also some development version features. For recent features, we note in each section the version of Ansible where the feature was added.
Ansible releases a new major release of Ansible approximately every two months. The core application evolves somewhat conservatively, valuing simplicity in language design and setup. However, the community around new modules and plugins being developed and contributed moves very quickly, adding many new modules in each release.
Ansible Lightbulb 是 Ansible 官方推荐的入门教材,普通用户大概只需要 10-20 分钟时间即可入门
The Ansible Lightbulb project is an effort to provide a content toolkit and educational reference for effectively communicating and teaching Ansible topics.
Ansible Lightbulb - https://github.com/ansible/lightbulb
Ansible Documentation 是 Ansible 官方文档,我的建议还是对英文不要害怕,多动手查多敲命令去理解
Ansible Documentation - http://docs.ansible.com/ansible/latest/index.html
第一个是 ansible 官方开源项目,其他都是和 ansible 相关的运维平台开源项目,推荐学习和参考
Ansible - https://github.com/ansible/ansible
Jumpserver - http://www.jumpserver.org/
OpsManage - https://github.com/welliamcao/OpsManage
adminset - https://github.com/guohongze/adminset
以下内容来自于《基于 ansible 的主机自动化配置管理》项目,基于 ansible 目前可以满足生产环境所有基线要求,相信对大家有一定的参考价值
因为生产环境为内外网物理隔离,所有的安装部署都是离线进行的
# Install Packages
yum install gcc zlib zlib-devel openssl-devel -y
# Install Python
tar xf Python-2.7.14.tgz
cd Python-2.7.14
./configure
make
make install
cd ..
# renew python env
exit
# ImportError: No module named six.moves
tar xf six-1.11.0.tar.gz
cd six-1.11.0
python setup.py install
cd ..
# ImportError: No module named packaging.version
tar xf packaging-17.1.tar.gz
cd packaging-17.1
python setup.py install
cd ..
# ImportError: No module named pyparsing
tar xf pyparsing-2.2.0.tar.gz
cd pyparsing-2.2.0
python setup.py install
cd ..
# ImportError: No module named appdirs
tar xf appdirs-1.4.3.tar.gz
cd appdirs-1.4.3
python setup.py install
cd ..
# Install Setuptools
unzip setuptools-38.5.2.zip
cd setuptools-38.5.2
python setup.py install
cd ..
# Install pip
tar xf pip-9.0.1.tar.gz
cd pip-9.0.1
python setup.py install
cd ..
# pip 离线下载
# pip download -d DIR -r requirements.txt
pip download -d ~/ansible/ ansible
# pip 离线安装
# pip install --no-index --find-links=DIR -r requirements.txt
pip install --no-index --find-links=pip-ansible-2.3.3/ -r requirements.txt
pip install --no-index --find-links=pip-ansible-2.5.0/ -r requirements.txt -U
# pip 离线安装 pipenv
pip install --no-index --find-links=pip-pipenv/ pipenv
# 使用 pipenv 创建虚拟环境
mkdir win_ansible
cd win_ansible
pipenv shell
pip install --no-index --find-links=pip-ansible-2.5.2/ -r requirements.txt
ansible.cfg 不影响执行结果但合理的配置会有效提升效率
# 配置文件路径(优先级)
./ansible.cfg
/etc/ansible/ansible.cfg
# 配置文件内容
[defaults]
#inventory = /etc/ansible/hosts
#log_path = /var/log/ansible.log
forks = 100 # 设置并发数
host_key_checking = False # 不检查 SSH 主机登录的密钥
display_skipped_hosts = False # 不显示已跳过的主机
retry_files_enabled = False # 不创建任务失败后的重试文件
# 按照 1d 设置 setup 缓存,优化执行效率
gathering = smart
fact_caching_timeout = 86400
fact_caching = jsonfile
fact_caching_connection = cachedir
服务端
客户端
核心用法
# 检测 ansible 是否可以正常访问主机
ansible-playbook -i hosts playbooks/ping.yml -v
# 配置好 inventory,执行以下命令创建用户并建立信任关系
ansible-playbook -i hosts playbooks/user/default.yml -v
# 配置时间同步 /进程服务 /基线文件
ansible-playbook -i hosts playbooks/baseline/cfgset.yml -v
ansible-playbook -i hosts playbooks/baseline/cfgset.yml -v --tags="repo"
ansible-playbook -i hosts playbooks/baseline/cfgset.yml -v --skip-tags="ntp,repo"
# 更新系统软件包和补丁包
ansible-playbook -i hosts playbooks/baseline/pakset.yml -v
# 修改用户密码
ansible-playbook -i hosts_changepw playbooks/user/changepw.yml -v -e "@userpass.json"
# 备份配置,支持自定义日期命名,默认为"%Y%m%d"
ansible-playbook -i hosts backup/backup.yml -v
# 恢复配置,支持按日期目录全局或者局部主机恢复
ansible-playbook -i hosts backup/restore.yml -v -e "var_backup_date=20180305"
服务端
客户端
核心用法
# 检测 ansible 是否可以正常访问主机
ansible-playbook -i hosts win_playbooks/ping.yml -v
# 配置好 inventory,执行以下命令创建用户并建立信任关系
ansible-playbook -i hosts win_playbooks/user/default.yml -v
# 配置时间同步 /进程服务 /基线文件
ansible-playbook -i hosts win_playbooks/baseline/cfgset.yml -v
ansible-playbook -i hosts win_playbooks/baseline/cfgset.yml -v --tags="wsus"
ansible-playbook -i hosts win_playbooks/baseline/cfgset.yml -v --skip-tags="ntp,wsus"
# 更新系统软件包和补丁包
ansible-playbook -i hosts win_playbooks/baseline/pakset.yml -v
# 修改用户密码
ansible-playbook -i win_hosts_changepw win_playbooks/user/changepw.yml -v -e "@userpass.json"
# 备份配置,支持自定义日期命名,默认为"%Y%m%d"
ansible-playbook -i win_hosts win_backup/backup.yml -v
# 恢复配置,支持按日期目录全局或者局部主机恢复
ansible-playbook -i win_hosts win_backup/restore.yml -v -e "var_backup_date=20180305"
很抱歉我暂时不能分享全部信息,但是这并不妨碍技术上的交流,我会逐步分享有价值的可公开代码
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.