ansible高级使用知识点
ansible功能重用和高级特性
Handlers
·Handlers只有在其所在的任务被执行时,才会被运行;如果一个任务中定义了notify调用Handlers,但是由于条件判断等原因,该任务未被执行,那么Handlers同样不会被执行。
·Handlers只会在Play的末尾运行一次;如果想在一个Playbook的中间运行Handlers,则需要使用meta模块来实现,例如:-meta:flush_handlers。
·如果一个Play在运行到调用Handlers的语句之前失败了,那么这个Handlers将不会被执行。我们可以使用mega模块的--force-handlers选项来强制执行Handlers,即使是Handlers所在的Play中途运行失败也能执行。
handlers:
- name: restart apache
service: name=apache2 state=restarted
notify: restart memcached - name: restart memcached
service: name=memcached state=restarted
Facts
在运行任何一个Playbook之前,Ansible默认会先抓取Playbook中所指定的所有主机的系统信息,这些信息我们称之为Facts。
ansible munin -m setup
设置gather_facts:no来暂时让Ansible在执行Playbook任务之前跳过收集远程主机Facts信息这一步,这样可以为任务节省几秒钟的时间
交互式提示
---
- hosts: all
vars_prompt:- name: share_user
prompt: "What is your network username?" - name: share_pass
prompt: "What is your network password?"
private: yes
- name: share_user
Block
---
- hosts: web
tasks:Install and configure Apache on RedHat/CentOS hosts.
- block:
- yum: name=httpd state=present
- template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
- service: name=httpd state=started enabled=yes
when: ansible_os_family == 'RedHat'
sudo: yes
Install and configure Apache on Debian/Ubuntu hosts.
- block:
- apt: name=apache2 state=present
- template: src=httpd.conf.j2 dest=/etc/apache2/apache2.conf
- service: name=apache2 state=started enabled=yes
when: ansible_os_family == 'Debian'
sudo: yes
- block:
块功能非常适合于多个任务共用同一套任务参数的情况
control node
控制端版本
ansible-core 2.11 and Ansible 4.0.0
make Python 3.8 a soft dependency
ansible-core 2.12 and Ansible 5.0.0
will require Python 3.8 or newer
Starting with ansible-core 2.11, the project will only be packaged for Python 3.8 and newer.
版本
- 从2.10版本开始
community package called ansible
minimalist language and runtime called ansible-core (called ansible-base in version 2.10)
Includes
- include: ./static_git_pull.yml
Includes引用方式的好处不言而喻:简洁,干净,解耦,复用度高,易于维护
- include: ./static_git_pull.yml
when
动态引用
Roles
相对Includes功能,Roles更适合于大项目Playbook的编排架构
Roles不仅支持Tasks的集合,同时包括vars_files、tasks、handlers、meta、templates
Roles主要依赖于目录的命名和摆放,默认tasks/main.yml是所有任务的入口
每个目录下均由main.yml定义该功能的任务集
引用role的方式
- hosts: all
roles:- role_name
·roles/x/tasks/main.yml:主函数,包括在其中的所有任务将被执行。
·roles/x/handlers/main.yml:所有包括其中的handlers将被执行。
·roles/x/vars/main.yml:所有包括在其中的变量将在roles中生效。
·roles/x/meta/main.yml:roles所有依赖将被正常登入。
·roles/x/{files,templates,tasks}/(dir depends on task):所有文件、模板都可存放在这里,放在这里最大的好处是不用指定绝对路径。
Jinja2
- 变量的提取使用{{variable}},{%statement execution%}括起来的内容为Jinja2命令执行语句
- bind-address=0.0.0.0:{{ PORT | default(3306) }}
Galaxy
- collections
- Galaxy provides pre-packaged units of work such as roles, and new in Galaxy 3.2, collections You can find roles for provisioning infrastructure, deploying applications, and all of the tasks you do everyday. The collection format provides a comprehensive package of automation that may include multiple playbooks, roles, modules, and plugins.
XMind: ZEN - Trial Version
分类:
devops
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
2013-04-06 几个Web Charts框架
2011-04-06 .net人员用Java 之Java EE
2010-04-06 arcgis开发笔记【系统介绍】