playbook 之 handlers

第一种类型

- hosts: 127.0.0.1
  tasks:
  - name: mkdir
    file:
      path: /tmp/testdir/1
      state: directory
      recurse: yes
      owner: test
  - name: touch
    file:
      path: /tmp/testdir/1/1.txt
      state: touch
      owner: test
      mode: 0600
    notify: link
    
  handlers:    
  - name: link
    file:
      path: /tmp/testdir/2
      state: link
      src: /tmp/testdir/1/1.txt
      owner: test
      mode: 0600

执行palybook ansible-playbook play.yml

第二种类型

使用listen通知多个handlers

- hosts: 127.0.0.1
  tasks:
  - name: mkdir
    file:
      path: /tmp/testdir/1
      state: directory
      recurse: yes
      owner: test
  - name: touch
    file:
      path: /tmp/testdir/1/1.txt
      state: touch
      owner: test
      mode: 0600
    notify: group
    
  handlers:    
  - name: link
    listen: group
    file:
      path: /tmp/testdir/2
      state: link
      src: /tmp/testdir/1/1.txt
      owner: test
      mode: 0600
  - name: creat file 2.txt
    listen: group
    file:
      path: /tmp/testdir/1/2.txt
      state: touch
      owner: test
      mode: 0600

执行palybook ansible-playbook play.yml

posted @ 2023-01-17 09:13  mingtian是吧  阅读(21)  评论(0编辑  收藏  举报