运维开发网

ansible的tags

运维开发网 https://www.qedev.com 2020-09-05 12:08 出处:51CTO 作者:菜机运维
给tasks打tags可以给tasks中的每个任务打tags以列表的方式书写,可以打多个:['tag1','tag2','tag3']也可以给整个tasks打大标签,作为多个任务的共有tags示例如下hosts:PCremote_user:jacktasks:name:copyhoststags:['hosts']copy:src:/

给tasks打tags

可以给tasks中的每个任务打tags以列表的方式书写,可以打多个:['tag1','tag2','tag3']

也可以给整个tasks打大标签,作为多个任务的共有tags

示例如下


  • hosts: PC

    remote_user: jack

    tasks:

    • name: copy hosts

      tags: ['hosts']

      copy:

      src: /etc/hosts

      dest: /home/jack/

      backup: yes

  • hosts: PD

    remote_user: root

    tasks:

    • name: add user

      tags: ['user']

      user:

      name: jiao

      uid: 1500

      password: $6$iWcHDljL6NvS45z9$Y/WMWbLhvjSOgl.NOM2sEpBbN6A4ghQiUFwImiyzmVq2JhVp.b3taieZFR4fx3npQY3Dk6OkkUuwnfGqw/DvA.

    • name: add cron

      tags: ['cron']

      cron:

      name: cron for jiao

      user: jiao

      minute: '0'

      hour: '15'

      job: echo my love

      notify: hd1

    • meta: flush_handlers
    • name: touch file

      tags: ['file']

      file:

      path: /home/jiao/test

      state: touch

      owner: jiao

      group: jiao

      mode: 0777

  • hosts: PB

    remote_user: root

    tasks:

    • name: install apache

      tags: ['apache']

      yum:

      name: httpd

      state: installed

      notify: hd2

    handlers:

    • name: reload crond

      listen: hd1

      service:

      name: crond

      state: reloaded

    • name: restart httpd

      listen: hd2

      service:

      name: httpd

      state: restarted

查看tags: ansible-playbook --list-tags test9.yml

执行某个tags:ansible-playbook --tags apache test9.yml

执行多个tags:ansible-playbook --tags user,file test9.yml

跳过已执行的tags:ansible-playbook --skip-tags cron,file,user,apache test9.yml

执行未打tags的任务:ansible-playbook --tags untagged test9.yml

执行所有打tags的任务:ansible-playbook --tags tagged test9.yml

ansible还预置了5个特殊tag,这5个特殊tag分别为

always任务就总是会被执行,除非你使用'--skip-tags'选项明确指定不执行对应的任务

never(2.5版本中新加入的特殊tag)

tagged 打过标签的任务

untagged 未打过标签的任务

all 所有任务

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号