Ansible流程控制
条件语句(判断)
在各大编程语言中,流程控制、条件判断都是必不可少的,在使用Ansible的过程中,条件判断使用频率高。条件判断通俗点讲就是当满足什么条件时就执行哪些语句
ansible获取主机名
ansible_hostname:只显示第一个‘.’前面的名字
[root@m01 ~]
"ansible_hostname": "locahost"
ansible_fqdn:获取完整的主机名
[root@m01 ~]
"ansible_fqdn": "locahost.local"
条件判断语法
tasks:
- name: "shut down Debian flavored systems"
command: /sbin/shutdown -t now
when: ansible_facts['os_family'] == "Debian"
tasks:
- name: "shut down Ubuntu flavored systems"
command: apt-get install apache2
when: ansible_os_family == "Ubuntu"
案例
通过判断系统对软件包进行安装
- hosts: rsync
tasks:
- name: "shut down RedHat flavored systems"
command: yum install -y unzip
when: ansible_facts['os_family'] == "RedHat"
通过判断主机来安装服务和推送文件
- hosts: all
tasks:
- name: 安装rsync和nfs服务
yum:
name:
- rsync
- nfs-utils
when: ansible_hostname == 'backup' or ansible_hostname == 'nfs'
- name: 推送rsync文件
template:
src: /root/wordpress_ansible/rsync/rsyncd.conf
dest: /etc/
when: ansible_hostname == 'backup'
判断是否安装nginx包
- hosts: web_group
tasks:
- name: 查看nginx目录
shell: "ls -l /etc/nginx"
register: ng
- name: 判断是否安装nginx
shell: "cd /opt && rpm-Uvh *.rpm"
when: ng.rc != 0
- hosts: all
tasks:
- name: 查看nginx目录
shell: "ls -l /etc/nginx"
when: ansible_hostname is match 'web*'
playbook循环语句
列表循环
- hosts: all
tasks:
- name: 重新启动nginx和php
service:
name: "{{ item }}"
state: restarted
with_items:
- nginx
- php-fpm
when: ansible_hostname is match 'web*'
字典循环
- hosts: all
tasks:
- name: 重新启动nginx和php
service:
name: "{{ item }}"
state: restarted
with_items:
- nginx
- php-fpm
when: ansible_hostname is match 'web*'
- name: nginx and php Profile
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
with_items:
- {src: "/root/wordpress_ansible/nginx_php/nginx.conf",dest: "/etc/nginx/"}
- {src: "/root/wordpress_ansible/nginx_php/blog.jl.com.conf",dest: "/etc/nginx/conf.d"}
when: ansible_hostname is match 'web*'
playbook handlers(触发器)
handlers用来执行某些条件下的任务,比如当配置文件发生变化时,通过notify触发handlers去重启服务。
在saltstack中,只需要Watch,配置文件即可
实践案例
- hosts: web_group
tasks:
- name: nginx Profile
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
with_items:
- {src: "/root/wordpress_ansible/nginx_php/nginx.conf",dest: "/etc/nginx/"}
- {src: "/root/wordpress_ansible/nginx_php/blog.jl.com.conf",dest: "/etc/nginx/conf.d/"}
notify: restart nginx
- name: start nginx
service:
name: nginx
state: started
enabled: True
handlers:
- name: restart nginx
service:
name: nginx
state: restarted
handler注意事项
1.无论有多少个task调用相同的handlers,只会在所有tasks执行完成后,才会触发一次handlers
2.handlers只有在其所在的任务被执行时才会被运行,如果它所在的任务因为其他原因没有被执行,那么handlers同样也不会执行
3.handlers只会在每一个play的末尾运行一次,如果想要在playbook中间执行handlers,那么需要使用meta模块来实现。语法:meta: flush_handlers
4.如果一个play在运行到handlers的语句之前失败了,那么这个handlers将不会被执行,那么可以使用meta模块的--force-handlers选项来强制执行handlres,即使handlers所在play中途运行失败也能执行
5.不能使用handlers替代tasks
ansible任务标签
在默认情况下,ansible在执行一个palybook时会执行剧本中定义的所有任务,ansible标签(tag)功能可以给单独任务打上标签,然后利用这些标签来指定运行playbook中的个别任务,或者不执行指定的任务
打标签的方式
- name: Install rsync
yum:
name: nfs-utils,rsync
state: present
when: ansible_hostname != 'db01'
tags: install_rsync
- name: Install rsync
yum:
name: nfs-utils,rsync
state: present
when: ansible_hostname != 'db01'
tags:
- install_rsync
- create www_user
- name: cerate user
user:
name: "{{ user_group }}"
uid: "{{ id }}"
group: "{{ id }}"
shell: /sbin/nologin
create_home: no
tags: install_rsync
- name: Install rsync
yum:
name: nfs-utils,rsync
state: present
when: ansible_hostname != 'db01'
tags: install_rsync
标签的使用
-t:执行指定的tag标签任务
--skip-tags:执行--skip-tags之外的标签任务
使用:
ansible-playbook xxx.yml -t 'xxx'
ansible-playbook xxx.yml --skip-tags 'xxx'
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?