Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

ansible for loop json witj filter of some records

I am new to ansible , i have below response which is coming from rest uri

[{??
"id": 1,
"clientId": "client1",
"name": "client first"
},
{
"id": 2,
"clientId": "client2",
"name": "client first"
},
{
{
"id": 3,
"clientId": "client3",
"name": "client three"
}
}
]

I want to loop over the above json response and wants exclude some of the client Id like "clientId": "client1" then add those clientIds like client2, client3 in list client_id_to_be_create Below is my code snippet

- name: initialize container variables
  set_fact:
    client_to_be_excluded: [client1]
    client_id_to_be_create: [] // only append those client in this list

 - name: Create new client mapper for all clients
  block:
    - name: get clients
      local_action:
        module: uri
        url: "{{ host_url }}/clients"
        method: GET
        headers: "{{ host_headers }}"
        status_code: 200
        validate_certs: "{{ validate_certs }}"
      register: get_clients
    
    - name: "Find clients"
      loop: "{{ get_clients.json }}"
      when: not  get_clients.clientId in client_to_be_excluded"
      loop_control:
        label: "{{ get_clients.clientId }}"
question from:https://stackoverflow.com/questions/65840953/ansible-for-loop-json-witj-filter-of-some-records

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...