0

Need assistance how to filter output and run additional tasks based on the previous task in ansible.

I am writing a task which check metrics configuration is enabled/disabled. If disabled need to run enable task.

   - name: Verify insights configuration
      shell:  dsetool -h `hostname` -a username -b {{ password }} insights_config -- 
              show_config
      register: insight_output

   - name: Print status
      debug:
        msg: "{{ insight_output.stdout }}"

   - name: Filter Mode
      shell: dsetool -h `hostname` -a username -b {{ password }}  insights_config -- 
             show_config | python -m json.tool | jq '.mode' | tr -d '"'
      register: mode_status

Output of print status will be like below

 msg:
    config_refresh_interval_in_seconds: 30
    data_dir_max_size_in_mb: 1024
    metric_sampling_interval_in_seconds: 30
    mode: ENABLED_WITH_LOCAL_STORAGE
    node_system_info_report_period: PT1H

TASK [cassandra : Print mode]
msg: ENABLED_WITH_LOCAL_STORAGE

How can I accomplish running additional tasks if output shows DISABLED.

Zeitounator
  • 38,476
  • 7
  • 53
  • 66

1 Answers1

0

How can I accomplish running additional tasks if output shows DISABLED.

You may have a look into the following minimal example playbook with Conditionals.

---
- hosts: localhost
  become: false
  gather_facts: false

  vars:

    mode_status:
      mode: ENABLED_WITH_LOCAL_STORAGE

  tasks:

  - name: Show status
    debug:
      msg: "DISABLED"
    when: "'DISABLED' in mode_status.mode"

Further Q&A

... which might be interesting for future use

U880D
  • 8,601
  • 6
  • 24
  • 40