如何将ansible事实变量的值存储在另一个变量中

Huz*_*efa 1 linux ansible ansible-playbook

我想将 ansible 事实变量的值存储在其他变量中,并且还想检查事实变量是否为空

下面是我的代码,我可以在其中打印 ansible_local 事实变量的值,但我希望将该值存储在另一个变量(例如 xyz)中。并且还想检查 ansible_local.sj.inventory.as_tag 变量值是否为空

- name: -> Apply common configuration to {{ target }} nodes
  hosts: "{{ target }}"
  gather_facts: True

  user: root

  pre_tasks:
   - setup:
      filter: ansible_local

 tasks:
   - action: debug msg="{{ ansible_local.sj.inventory.as_tag }}"
Run Code Online (Sandbox Code Playgroud)

样本操作数:

TASK: [debug msg="{{ansible_local.sj.inventory.as_tag}}"]   
    ***********
Monday 09 May 2016  09:48:49 -0700 (0:00:01.375)       0:00:02.785  
  ************
ok: [abcserver] => {
  "msg": "abcd-123"
Run Code Online (Sandbox Code Playgroud)

小智 5

您可以使用set_fact 模块

这是一个例子......

- name: -> Apply common configuration to {{ target }} nodes
  hosts: "{{ target }}"
  gather_facts: True
  user: root

  pre_tasks:
   - setup:
      filter: ansible_local
   - set_fact:
       tag: "{{ ansible_local.sj.inventory.as_tag }}"
   - debug:
       var: tag
Run Code Online (Sandbox Code Playgroud)