tags 模块
可以在一个playbook中为某个或某些任务定义“标签”,在执行此playbook时通过ansible-playbook命令使用--tags选项能实现仅运行指定的tasks。
 playbook还提供了一个特殊的tags为always。作用就是当使用always作为tags的task时,无论执行哪一个tags时,定义有always的tags都会执行。
案例演示:
vim play5.yaml
- name: five play
  remote_user: root
  hosts: dbservers
  gather_facts: true
  tasks:
  - name: copy file
    copy: src=/etc/hosts dest=/opt/
    tags:
    - test
 
  - name: touch file
    file: path=/opt/myhosts state=touch
    tags:
    - only
 
 
ansible-playbook play5.yaml --tags="test"   #指定标签,只运行该标签下的任务 
Templates 模块
Jinja是基于Python的模板引擎。Template类是Jinja的一个重要组件,可以看作是一个编译过的模板文件,用来产生目标文本,传递Python的变量给模板去替换模板中的标记。
案例:通过Templates模块进行差异化处理,不同的主机上的htttpd监听不同的端口和地址
第一步:先准备一个以 .j2 为后缀的template模板文件,设置引用的变量



第二步:每个组成员都设置各自的组变量

第三步:配置yaml文件
vim play1.yaml
- name: first play
  gather_facts: false
  hosts: webservers
  remote_user: root
  tasks:
  - name: disable firewalld
    service: name=firewalld state=stopped enabled=no
  - name: disable selinux
    command: 'setenforce 0'
    ignore_errors: true
  - name: mount cdrom
    mount: src=/dev/sr0 path=/mnt fstype=iso9660 state=mounted
  - name: install httpd
    yum: name=httpd state=latest
  - name: copy config template file
    template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
    notify: "reload httpd"
  - name: start httpd
    service: name=httpd state=started enabled=yes
  handlers:
  - name: reload httpd
    service: name=httpd state=reloaded
 
ansible-playbook play1.yaml
第四步:到对应节点验证























