如何指定判断条件:
setup模块中显示被管理主机系统的详细信息
- hosts: 192.168.1.43
remote_user: root
tasks:
- name: Check File
file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch
when: (ansible_hostname == "nfs") or (ansible_hostname == "backup")
- name: install httpd
yum: name=httpd state=installed
when: (ansible_distribution == "CentOS")
- name: install httpd2
yum: name=httpd2 state=installed
when: (ansible_distribution == "Ubuntu")
手动调试获取内置变量方法:
ansible 192.168.1.43 -m setup -a "filter=ansible_hostname"
常见主机信息:
ansible_all_ipv4_addresses: 仅显示ipv4的信息。
ansible_devices: 仅显示磁盘设备信息。
ansible_distribution: 显示是什么系统,例:centos,suse等。
ansible_distribution_major_version: 显示是系统主版本。
ansible_distribution_version: 仅显示系统版本。
ansible_machine: 显示系统类型,例:32位,还是64位。
ansible_eth0: 仅显示eth0的信息。
ansible_hostname: 仅显示主机名。
ansible_kernel: 仅显示内核版本。
ansible_lvm: 显示lvm相关信息。
ansible_memtotal_mb: 显示系统总内存。
ansible_memfree_mb: 显示可用系统内存。
ansible_memory_mb: 详细显示内存情况。
ansible_swaptotal_mb: 显示总的swap内存。
ansible_swapfree_mb: 显示swap内存的可用内存。
ansible_mounts: 显示系统磁盘挂载情况。
ansible_processor: 显示cpu个数(具体显示每个cpu的型号)。
ansible_processor_vcpus: 显示cpu个数(只显示总的个数)。
实践编写单条件判断:
- hosts: rsync
tasks:
- name: create server password file
copy: content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600
when: (ansible_hostname == "backup")
- name: create client password file
copy: content='oldboy123' dest=/etc/rsync.password mode=600
when: (ansible_hostname != "backup")
实践编写多条件判断:
- name: 1. Ubuntu Add Postgresql repo to Ubuntu repo
shell:
cmd: echo "deb https://mirrors.tuna.tsinghua.edu.cn/postgresql/repos/apt/ $(lsb_release -cs)"-pgdg main | sudo tee /etc/apt/sources.list.d/pgdg.list
when:
ansible_distribution == 'Ubuntu'
- name: 1. CentOS Add Postgresql repo to CentOS repo
yum:
name: https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
when:
ansible_distribution == 'CentOS' and ansible_distribution_major_version == '7'
when语法实践
1 when控制语句
when 判断在用于控制在满足when所指定的条件的情况下才执行响应的动作。
使用场景:
比如: web 节点都需要配置 nginx 仓库,但其他节点并不需要,此时就会用到when 判断。
比如: Centos 与 Ubuntu 都需要安装 Apache ,而 Centos 系统软件包为httpd ,而 Ubuntu 系统软件包为 httpd2 ,那么此时就需要判断主机系统,然后为不同的主机系统安装不同的软件包。
1.1 根据不同操作系统安装相同的软件
需求:为所有主机安装 Apache 软件,若系统为 CentOS :安装 httpd,若系统为 Ubuntu :安装 httpd2。
ansible_distribution变量可以获取到主机的发行版本。
playbook编写如下:
[root@xuzhichao playbook]# cat when1.yml
- hosts: webs
remote_user: root
tasks:
- name: CentOS Install httpd
yum:
name: httpd
state: present
when: ansible_distribution == "CentOS" <==判断版本语句,此处变量不需要{{ }}引用。
- name: Ubuntu Install httpd
yum:
name: httpd2
state: present
when: ansible_distribution == "Ubuntu"
主机hosts文件如下:
[root@xuzhichao playbook]# tail /etc/ansible/hosts
[webs]
192.168.20.22
192.168.20.23
执行结果:
[root@xuzhichao playbook]# ansible-playbook when1.yml
PLAY [webs] ***************************************************************************************************************************************************
TASK [CentOS Install httpd] ***********************************************************************************************************************************
ok: [192.168.20.22]
ok: [192.168.20.23]
TASK [Ubuntu Install httpd] ***********************************************************************************************************************************
skipping: [192.168.20.22] <==跳过执行此任务
skipping: [192.168.20.23]
PLAY RECAP ****************************************************************************************************************************************************
192.168.20.22 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
192.168.20.23 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
1.2 为不同centos版本安装httpd软件
centos6和centos7中的httpd服务,版本不同,配置文件也不相同,此时可以将模板根据centos的版本发送到指定的目标主机。
ansible_distribution_major_version变量用于判断centos主版本号。
playbook文件如下:
---
- hosts: web
remote_user: root
tasks:
- name: install package
yum:
name: httpd
- name: copy template centos6
template:
src: httpd_6.conf.j2
dest: /etc/httpd/conf/httpd.conf
notify: restart service
when: ansible_distribution_major_version == "6" <==判断版本语句,此处变量不需要{{ }}引用。
- name: copy template centos7
template:
src: httpd_7.conf.j2
dest: /etc/httpd/conf/httpd.conf
notify: restart service
when: ansible_distribution_major_version == "7" <==判断版本语句
- name: start service
service:
name: httpd
state: started
handlers:
- name: restart service
service:
name: httpd
state: restarted
1.3 为特定的主机添加Nginx仓库
为特定的主机添加 Nginx 仓库:主机名为 web 则添加 Nginx 仓库,主机名不为 web 则不做任何处理。
编写playbook文件:
[root@xuzhichao playbook]# cat when2.yml
- hosts: all
remote_user: root
tasks:
- name: Add Nginx Yum Repository
yum_repository:
name: nginx
description: Nginx Repository
baseurl: http://nginx.org/packages/centos/7/$basearch/
gpgcheck: no
when: (ansible_hostname is match("web*")) or (ansible_hostname is match("nginx*")
#when也可以使用and与or方式进行多项匹配。
运行playbook:
[root@xuzhichao playbook]# ansible-playbook when2.yml
查看运行效果:
[root@nginx03 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx]
baseurl = http://nginx.org/packages/centos/7/$basearch/
gpgcheck = 0
name = Nginx Repository
1.4 判断服务是否正常运行
判断 nginx 服务是否处于运行状态,已运行:则重启服务;未运行:则不做处理。
playbook文件如下:
[root@xuzhichao playbook]# cat when3.yml
- hosts: NginxWebs
remote_user: root
tasks:
- name: Check Nginx Status
shell:
cmd: systemctl is-active nginx
ignore_errors: yes
register: check_nginx
- name: Print Check_nginx
debug:
var:
"check_nginx" <==#通过debug的var输出该变量的所有内容
- name: Nginx Restart
service:
name: nginx
state: restarted
when: check_nginx.rc == 0 <==.rc是check_nginx变量中的执行结果,见下面的执行过程
执行playbook:
#其中192.168.20.22主机nginx检测失败,没有重启服务;
#192.168.20.23主机nginx检测成功,重启服务;
[root@xuzhichao playbook]# ansible-playbook when3.yml
PLAY [NginxWebs] **********************************************************************************************************************************************
TASK [Check Nginx Status] *************************************************************************************************************************************
fatal: [192.168.20.22]: FAILED! => {"changed": true, "cmd": "systemctl is-active nginx", "delta": "0:00:00.007774", "end": "2021-08-04 18:33:05.331828", "msg": "non-zero return code", "rc": 3, "start": "2021-08-04 18:33:05.324054", "stderr": "", "stderr_lines": [], "stdout": "unknown", "stdout_lines": ["unknown"]}
...ignoring
changed: [192.168.20.23]
TASK [Print Check_nginx] **************************************************************************************************************************************
ok: [192.168.20.22] => {
"check_nginx": {
"changed": true,
"cmd": "systemctl is-active nginx",
"delta": "0:00:00.007774",
"end": "2021-08-04 18:33:05.331828",
"failed": true,
"msg": "non-zero return code",
"rc": 3, <==.rc是check_nginx变量中的执行结果状态
"start": "2021-08-04 18:33:05.324054",
"stderr": "",
"stderr_lines": [],
"stdout": "unknown",
"stdout_lines": [
"unknown" <==执行结果。
]
}
}
ok: [192.168.20.23] => {
"check_nginx": {
"changed": true,
"cmd": "systemctl is-active nginx",
"delta": "0:00:00.007241",
"end": "2021-08-04 18:33:05.331485",
"failed": false,
"rc": 0, <==.rc是check_nginx变量中的执行结果状态,0表示执行正常
"start": "2021-08-04 18:33:05.324244",
"stderr": "",
"stderr_lines": [],
"stdout": "active",
"stdout_lines": [
"active" <==执行结果。
]
}
}
TASK [Nginx Restart] ******************************************************************************************************************************************
skipping: [192.168.20.22]
changed: [192.168.20.23]
PLAY RECAP ****************************************************************************************************************************************************
192.168.20.22 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=1
192.168.20.23 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
1.5 判断主机是否安装nginx 如果为否 则安装nginx服务
- name: Ubuntu Check Nginx Exist
shell:
cmd: dpkg -l nginx|grep ii|grep nginx|wc -l
register: Ubuntu_Check_Nginx_Exist
when:
ansible_distribution == 'Ubuntu'
- name:
debug:
msg:
"{{ Ubuntu_Check_Nginx_Exist }}"
- name: Ubuntu Install Nginx
apt:
name: nginx
state: present
when:
Ubuntu_Check_Nginx_Exist.stdout == '0'