ansible: using with_items with notify handler

后端 未结 4 1811
再見小時候
再見小時候 2020-12-09 18:35

I want to pass a variable to a notification handler, but can\'t find anywhere be it here on SO, the docs or the issues in the github repo, how to do it. What I\'m doing is d

4条回答
  •  甜味超标
    2020-12-09 19:10

    Variables in Ansible are global so there is no reason to pass a variable to handler. If you are trying to make a handler parameterized in a way that you are trying to use a variable in the name of a handler you won't be able to do that in Ansible.

    What you can do is create a handler that loops over a list of services easily enough, here is a working example that can be tested locally:

    - hosts: localhost
      tasks:
      - file:  >
          path=/tmp/{{ item }}
          state=directory
        register: files_created
        with_items:
          - one
          - two
        notify: some_handler
    
      handlers:
        - name: "some_handler"
          shell: "echo {{ item }} has changed!"
          when: item.changed
          with_items: files_created.results
    

提交回复
热议问题