ansible条件使用
1、条件使用最简单的方式
ansible中使用条件最简单的方式如下所示:
[root@ansibleserver kel]# cat conditions.yml --- - hosts: servers tasks: - name: "shutdown centos system" command: /sbin/reboot when: ansible_lsb.id == "CentOS" |
在以上例子中,主要是根据收集的fact变量来判断哪些是centos服务器,从而将对应的centos服务器进行重启操作,执行结果如下:
[root@ansibleserver kel]# ansible-playbook -i hosts conditions.yml SSH password:
PLAY [servers] ****************************************************************
GATHERING FACTS *************************************************************** ok: [192.168.1.164] ok: [192.168.1.172] ok: [192.168.1.171] ok: [192.168.1.163]
TASK: [shutdown centos system] ************************************************ skipping: [192.168.1.163] skipping: [192.168.1.164] changed: [192.168.1.172] changed: [192.168.1.171]
PLAY RECAP ******************************************************************** 192.168.1.163 : ok=1 changed=0 unreachable=0 failed=0 192.168.1.164 : ok&# |