How set ansible function to when modeule?

时间:2016-10-20 18:49:30

标签: ansible

I want install one of Filebit prospectors only if service redis is running on the host. For that i create default list of prospectors ( redis, aerospike, postgress ) with {{ item.id }}. But right now i want put some expression to "when:" which will install prospector for redis only if it running - how can i do it ?

- name: Configure Filebeat prospectors
  template: src=filebeat_conf.yml.j2 dest=/etc/filebeat/conf.d/{{ item.id }}.yml
  notify: restart filebeat
  with_items: prospectors
  when: { " service: " }

1 个答案:

答案 0 :(得分:0)

将你的任务分成两部分:收集事实,根据事实采取行动 在您的情况下,您可以先列出已安装的服务,然后仅配置现有服务。

例如:

- hosts: test-host
  vars:
    services:
      - name: apache2
        id: 1
      - name: nginx
        id: 2
      - name: openvpn
        id: 3
  tasks:
    - name: get list of services
      shell: "service --status-all 2>&1 | awk {'print $4'}"
      args:
        warn: false
      register: services_list

    - name: process only existing services
      debug: msg="service {{ item.name }} with id={{ item.id }} exists"
      with_items: "{{ services }}"
      when: item.name in services_list.stdout_lines