Ansible
约 3 分钟阅读
- ansibleGitHubansible/ansibleansible 是什么? · GPL-3.0, Python · 自动化 服务开通、配置管理、应用部署 工具 · 实现 Infrastructure as code - 基础设施即代码 · 支持 Linux、Windows 及 网络设备 · 包含大量的插件和三方集成笔记:Ansible 是什么?
- GPL-3.0, Python
- 自动化 服务开通、配置管理、应用部署 工具
- 实现 Infrastructure as code - 基础设施即代码
- 支持 Linux、Windows 及 网络设备
- 包含大量的插件和三方集成
- 系统要求
- 控制节点 - python linix/windows
- 管理节点 - python sftp/scp
- 注意 ⚠️
- 分组名包含
-会告警force_valid_group_names=ignore可关闭
- docker_container 模块网络有所调整 - 之后默认不会添加 default 网络 - 与 docker 保持一直
- 建议
networks_cli_compatible=yes提前与 docker 网络保持一致
- 建议
- 建议使用 yaml 写 inventory - 比 ini 的模式好管理 - 结构也更加清晰
- 相同 set_fact 不能互相依赖 #40239GitHub Issueansible/ansible#40239
- 分组名包含
- 参考
- Ansible tutorialGitHubleucos/ansible-tuto
- List all modules
- collection ansible.builtin
- ansible.cfg 可用的 ansible.cfg 配置
- 可用环境变量列表GitHub 文件ansible/ansible/lib/ansible/constants.py
- YAML Syntax
- Other Tools and Programs
- apenella/go-ansibleGitHubapenella/go-ansible
- 学习
- 界面
- ansible-semaphore/semaphore笔记semaphoreansible-semaphore/semaphore · MIT, Go, Vue打开:/notes/devops/infra/ansible/semaphor
- Go 实现
- 有 Web 界面 和 API
- ansible/awxGitHubansible/awx是什么 · 基于 Ansible 的 Web 管理平台、REST API · 提供任务执行引擎,项目管理,用户权限管理 · AWX -> AnsibleWorks · ansible/awx · 参考 · 用户手册 · 管理文档 · Ansible AWX vs Tower笔记:Ansible AWX - Ansible Tower 上游开源项目
- 可 Docker Compose 启动
- ansible-semaphore/semaphore笔记semaphoreansible-semaphore/semaphore · MIT, Go, Vue打开:/notes/devops/infra/ansible/semaphor
- 环境变量
ANSIBLE_INVENTORY- 逗号分隔的仓库源DEFAULT_HOST_LIST- 默认仓库源 -inventory配置
# ping 所有节点
ansible all -m ping
# -i 指定仓库
ansible all -m ping -i hosts
# ping 本地 - 指定解释器
ansible localhost -m ping -e 'ansible_python_interpreter=/usr/bin/python3'
# 执行命令
ansible all -a date -i hosts
# ansible_facts 内容
ansible localhost -m setup
# 节点上本地设置的内容 - 文件为 ini 格式
# /etc/ansible/facts.d/preferences.fact
ansible hostname -m setup -a "filter=ansible_local"
# docker 启动环境
docker run --rm -it \
-e TZ=Asia/Shanghai \
-v $HOME/.ansible:/root/.ansible \
-v $PWD:/host -w /host \
--name ansible wener/ansible| Command | Desc |
|---|---|
| ansible | 执行 AdHoc 命令 |
| ansible-config | 查看当前所有配置 |
| ansible-connection | 连接到远程设备 |
| ansible-console | REPL 方式执行 Ansible Task |
| ansible-doc | 文档查询 |
| ansible-galaxy | Role 和 Collection 依赖管理 |
| ansible-inventory | 查询机器清单 |
| ansible-playbook | 执行 Playbook |
| ansible-pull | 配合 cron 从 VSC 拉取 playbook 然后本地执行 - 适用于大规模周期性任务执行 |
| ansible-test | Role 和 Collection 开发的测试工具 |
| ansible-vault | Ansible 加密数据操作 |
安装
- 2.10 需要先卸载 ansible 再安装
- Installing Ansible
# macOS
CFLAGS=-Qunused-arguments CPPFLAGS=-Qunused-arguments pip install --user ansible
# 2.10
pip uninstall ansible
pip install ansibleansible.cfg
- Ansible Configuration Settings
- 查找顺序
- ANSIBLE_CONFIG
- ansible.cfg
- ~/.ansible.cfg
- /etc/ansible/ansible.cfg
# 缓存 facts
[defaults]
gathering = smart
# 缓存时间 - 秒
fact_caching_timeout = 86400
# 缓存到 redis
# pip install redis
fact_caching = redis
# 缓存到 json 文件
fact_caching = jsonfile
fact_caching_connection = /path/to/cachedir
# 兼容 docker network - 如果指定了网络不添加默认网络
networks_cli_compatible=yes
# 不校验分组名字 允许包含 `-'
force_valid_group_names=ignore- ansible_sudo_pass
变量查找路径
最佳实践
Tips
- 使用 merge hash_behaviour 可合并对象配置
关联信息
反向链接、本文链接的其他页面和外部资料。
站内链接
- semaphore笔记 · ansible-semaphore/semaphore
ansible-semaphore/semaphore · MIT, Go, Vue
参考资料
GitHub7 条
- ansible/ansiblegithub.com/ansible/ansible
- ansible/ansible/lib/ansible/constants.pygithub.com/ansible/ansible/blob/devel/lib/ansible/constants.py
- ansible/ansible#40239github.com/ansible/ansible/issues/40239
- ansible/awxgithub.com/ansible/awx
- apenella/go-ansiblegithub.com/apenella/go-ansible
- geerlingguy/ansible-for-devopsgithub.com/geerlingguy/ansible-for-devops
- leucos/ansible-tutogithub.com/leucos/ansible-tuto
其他外链12 条
- ansible-semaphore.github.io/semaphoreansible-semaphore.github.io/semaphore
- docs.ansible.com/ansible/intro_configuration.htmldocs.ansible.com/ansible/intro_configuration.html
- docs.ansible.com/ansible/latest/collections/ansible/builtin/index.htmldocs.ansible.com/ansible/latest/collections/ansible/builtin/index.html
- docs.ansible.com/ansible/latest/collections/index.htmldocs.ansible.com/ansible/latest/collections/index.html
- docs.ansible.com/ansible/latest/community/other_tools_and_programs.htmldocs.ansible.com/ansible/latest/community/other_tools_and_programs.html
- docs.ansible.com/ansible/latest/installation_guide/intro_installation.htmldocs.ansible.com/ansible/latest/installation_guide/intro_installation.html
- docs.ansible.com/ansible/latest/reference_appendices/config.htmldocs.ansible.com/ansible/latest/reference_appendices/config.html
- docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.htmldocs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
- docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.htmldocs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html
- docs.ansible.com/ansible/latest/user_guide/playbooks_variables.htmldocs.ansible.com/ansible/latest/user_guide/playbooks_variables.html
- raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfgraw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg
- spacelift.io/blog/ansible-playbooksspacelift.io/blog/ansible-playbooks