Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 333
sherri Avatar asked Mar 07 '23 15:03

sherri


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
like image 172
techraf Avatar answered Mar 10 '23 13:03

techraf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!