基于条件测试实现角色调用

实例:指定 memcached 的运行内存大小为总大小的 1/4,并只有centos 7版本系统才执行操作

```

[root@localhost ~]# mkdir -pv /etc/ansible/roles/memcached/tasks


[root@localhost ~]# vim /etc/ansible/roles/memcached/tasks/main.yml

-------------------------------------------------------------------------------------------------------

- name: install memcached package

  yum: name=memcached state=present

- name: install conf file

  template: src=memcached.j2 dest=/etc/sysconfig/memcached

  notify: restarted memcached

  tags: restmemcached

- name: start memcached

  service: name=memcached state=started enabled=true


[root@localhost ~]# vim /etc/sysconfig/memcached

-------------------------------------------------------------------------------------------------------

PORT="11211"

USER="memcached"

MAXCONN="1024"

CACHESIZE="64"

OPTIONS=""


[root@localhost ~]# cd /etc/ansible/roles/memcached/


[root@localhost memcached]# mkdir templates


[root@localhost memcached]# cp /etc/sysconfig/memcached ./templates/


[root@localhost memcached]# vim templates/memcached

-------------------------------------------------------------------------------------------------------

PORT="11211"

USER="memcached"

MAXCONN="1024"

CACHESIZE="{{ ansible_memtotal_mb//4 }}"

OPTIONS=""


[root@localhost memcached]# cd templates/


[root@localhost templates]# mv memcached memcached.j2


[root@localhost templates]# cd ..


[root@localhost memcached]# mkdir 


[root@localhost templates]# vim memcached/handlers/main.yml

-------------------------------------------------------------------------------------------------------

- name: restarted memcached

  service: name=memcached state=restarted


[root@localhost templates]# cd 


[root@localhost ~]# cd ansible/


[root@localhost ansible]# vim memcached.yml 

-------------------------------------------------------------------------------------------------------

- hosts: dbservers

  remote_user: root

  roles:

  - { role: memcached,username: nginx,when: ansible_distribution_major_version == '7' }


[root@localhost ansible]# ansible-playbook --check memcached.yml

PLAY [dbservers] ************************************************************** 


GATHERING FACTS *************************************************************** 

ok: [172.18.77.84]


TASK: [memcached | install memcached package] ********************************* 

ok: [172.18.77.84]


TASK: [memcached | install conf file] ***************************************** 

changed: [172.18.77.84]


TASK: [memcached | start memcached] ******************************************* 

ok: [172.18.77.84]


NOTIFIED: [memcached | restarted memcached] *********************************** 

changed: [172.18.77.84]


PLAY RECAP ******************************************************************** 

172.18.77.84               : ok=5    changed=2    unreachable=0    failed=0   


[root@localhost ansible]# ansible-playbook memcached.yml


[root@localhost ansible]# cat /etc/sysconfig/memcached 

PORT="11211"

USER="memcached"

MAXCONN="1024"

CACHESIZE="244"

OPTIONS=""


```