Home > Enterprise >  Ansible - set_facts Set a boolean value based on register.stdout_lines containing a string
Ansible - set_facts Set a boolean value based on register.stdout_lines containing a string

Time:02-02

How can I set a variable using set_facts to True or False based on whether the register.stdout_lines contains a specific string.

Ansible version - 2.9

This is my stdout_lines output

 {
    "msg": [
        "● confluent-server.service - Apache Kafka - broker",
        "   Loaded: loaded (/usr/lib/systemd/system/confluent-server.service; enabled; vendor preset: disabled)",
        "  Drop-In: /etc/systemd/system/confluent-server.service.d",
        "           └─override.conf",
        "   Active: active (running) since Tue 2023-01-31 20:57:00 EST; 19h ago",
        "     Docs: http://docs.confluent.io/",
        " Main PID: 6978 (java)",
        "   CGroup: /system.slice/confluent-server.service",
    ]
 }

And I want to set a variable server_running to True if the above output contains the string active (running) ( which it does in above case - otherwise that should be set to false )

I tried this - but that is not correct

- name: success for start
  set_fact:
    start_success: >-
        "{{ confluent_status.stdout_lines | join('') | search(' active (running)') }}"

I want start_success above to have true or false value

I am not yet familiar with how to process using filters in ansible output, so trying different things found on the net.

Can I define a variable to true or false as output of whether a condition is true or not ? How would I go about it?

CodePudding user response:

stdout_lines is basically a list containing an item for each stdout line.
If you only need to access the output, you could easily use stdout instead.

The following example shows how to determine the value of a variable based on a condition:

- name: success for start
  set_fact:
    start_success: >-
        {% if ' active (running)' in confluent_status.stdout %}True{% else %}False{% endif %}

It's also possible to set stdout_callback = yaml in ansible.cfg for a better formatted output.

CodePudding user response:

You can use regex_search to check the string you're looking for. Below I provide an example of a playbook.

- name: Check status
  hosts: localhost
  gather_facts: no
  vars:
    confluent_status:
      stdout_lines:  [
        "● confluent-server.service - Apache Kafka - broker",
        "   Loaded: loaded (/usr/lib/systemd/system/confluent-server.service; enabled; vendor preset: disabled)",
        "  Drop-In: /etc/systemd/system/confluent-server.service.d",
        "           └─override.conf",
        "   Active: active (running) since Tue 2023-01-31 20:57:00 EST; 19h ago",
        "     Docs: http://docs.confluent.io/",
        " Main PID: 6978 (java)",
        "   CGroup: /system.slice/confluent-server.service",
      ]

  tasks:
    - name: set_status either to True or False
      set_fact:
        set_status: "{% if (confluent_status.stdout_lines | regex_search('active \\(running\\)')) %}True{% else %}False{% endif %}"
    
    - name: output set_status variable set in the previous task
      debug:
        msg: "{{ set_status  }}"

    - name: just a debug that outputs directly the status so you can use the condition directly to any task if needed. 
      debug:
        msg: "{% if (confluent_status.stdout_lines | regex_search('active \\(running\\)')) %}True{% else %}False{% endif %}"

Gives:

PLAY [Check status] ************************************************************************************************************************************************************************

TASK [set_status either to True or False] **************************************************************************************************************************************************
ok: [localhost]

TASK [output set_status variable set in the previous task] *********************************************************************************************************************************
ok: [localhost] => {
    "msg": true
}

TASK [Check if status is True or false] ****************************************************************************************************************************************************
ok: [localhost] => {
    "msg": true
}
  • Related