Ansible - apply filter on output and then register as a variable

Viewed 8397

I am registering the output of an operation and then applying the filter to display the value .But i also want to register that displayed value as a variable. I am not able to register that as a variable. Does anyone knows the solution to this ?

Here is my playbook

    ---
    - name: Filtering output to register in a variable
      hosts: localhost
      gather_facts: no
      tasks:
        - name: register filtered output to a  variable 
          uri:
            url: https://example.com/api/id
            method: GET 
            user: administrator
            password: password
            force_basic_auth: yes 
            validate_certs: no
          register: restdata
        - name: Display the output
          debug: msg="{{ restdata.json.parameter[1] }}"

I was wondering. Wouldn't it be simpler. If we filter the output first and then register it as a variable? does anyone know how to do that ?

1 Answers

You cannot register a variable, but you can set a fact (which in most use cases, including yours, will be equivalent to a variable):

    - set_fact:
        the_output: "{{ restdata.json.parameter[1] }}"
    - name: Display the output
      debug:
        var: the_output
Related