任务内部的ansible任务

时间:2019-08-12 11:29:53

标签: json ansible ansible-facts

我正在寻找在另一个任务中运行任务的方法

  - name: Get object facts
    checkpoint_policy_pkg_facts:
    register: results

  - name: "set fact for policy pkg selection"
    set_fact:
      pkges: "{{ results  | json_query(query) }}"
    vars:
       query: "ansible_facts.checkpoint_access_layers.packages[].name"

  - name: Get object facts
    checkpoint_policy_pkg_facts:
      name: "{{ item }}"
    register: results
    with_items: "{{ pkges }}"

  - name: "set fact for policy pkg selection"
    set_fact:
      pkgname: "{{ results  | json_query(query) }}"
    vars:
       query: "results[?ansible_facts.checkpoint_access_layers.\"installation-targets-revision\"[?\"cluster-members-revision\"[?\"target-name\"=='{{ item }}']]].item | [0]"
    with_items: "{{ targets }}"
输出如下
           "PolicyPKG1",
            "PolicyPKG2",
            "Standard"

我想使用以下任务将pkgname插入另一个任务,因为我的目标项目很大,所以我想使用以下任务将相应的pkgname插入servicenow

  - name: Create a change request
    snow_record:
      state: present
      table: u_device
      username: admin
      password: 
      instance: dev778899
      data:
        u_name:  "{{ pkgname }}"

1 个答案:

答案 0 :(得分:0)

您可以将pkgname注册为变量,以后再使用pkgname.stdout_lines作为变量,

 - name: "set fact for policy pkg selection"
   register: pkgname
   vars:
   query: "results[?ansible_facts.checkpoint_access_layers.\"installation-targets-revision\"[?\"cluster-members-revision\"[?\"target-name\"=='{{ item }}']]].item | [0]"
   with_items: "{{ targets }}"

接下来:

 - name: Create a change request
   snow_record:
     state: present
     table: u_device
     username: admin
     password: 
     instance: dev778899
     data:
       u_name:  "{{ item }}"
   with_items: "{{ pkgname.stdout_lines }}"