每次再操作节点进行系统的一些软件包更新时,由于存在ubuntu和centos系统,所以需要对系统判断来使用包管理器,每当后续有些操作需要就要写如下内容,繁琐。。。

1
when: ansible_os_family == 'RedHat'

正好最近浏览到ansible可以将任务合成为一个任务组,对组进行判断即可满足我的需求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
---
- hosts: all
tasks:
#Install and configure Apache on RHEL/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'
become: 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'
become: yes