Ansible - 在输出上应用过滤器,然后注册为变量

she*_*rri 3 python jquery json jinja2 ansible

我正在注册操作的输出,然后应用过滤器来显示值。但我也想将该显示的值注册为变量。我无法将其注册为变量。有谁知道这个问题的解决方案?

这是我的剧本

---
- 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] }}"
Run Code Online (Sandbox Code Playgroud)

我想知道。会不会更简单。如果我们先过滤输出然后将其注册为变量?有谁知道这是怎么做到的吗 ?

tec*_*raf 6

你不能注册一个变量,但你可以设置一个事实(在大多数用例中,包括你的,这将等同于一个变量):

- set_fact:
    the_output: "{{ restdata.json.parameter[1] }}"
- name: Display the output
  debug:
    var: the_output
Run Code Online (Sandbox Code Playgroud)