Ansible使用介绍

72课时
1.9K学过
8分

课程评价 (0)

请对课程作出评价:
0/300

学员评价

暂无精选评价
2分钟

08 When-示例1

vim testtemplate.yml

– hosts: all

remote_user: root

tasks:

– name: install package

yum: name=nginx

– name: copy template for centos7

template: src=nginx.conf7.j2 dest=/etc/nginx/nginx.conf

when: ansible_distribution_major_version == “7”

notify: restart service

– name: copy template for centos6

template: src=nginx.conf6.j2 dest=/etc/nginx/nginx.conf

when: ansible_distribution_major_version == “6”

notify: restart service

– name: start service

service: name=nginx state=started enabled=yes

handlers:

– name: restart service

service:name=nginx state=restarted

执行结果:当when语句不匹配时,将skipping直接跳过,仅执行与when语句匹配的语句内容,最终

CentOS6,7根据不同的版本号生成对应的配置并启动服务。