简体   繁体   中英

Using register in playbook for multiple clients

Hello Everyone.

First task in my playbook will be executed in server. Second task will be executed in clients. ##

First task : generate token numbers for all clients listed in inventory

- hosts: Server
  vars:
    clients:
      - clientA
      - ClientB
  tasks:
   - name: generate ticket on server and save it as a variable
     shell: /path/to/bin ticket {{ clients }} 
     register: ticket

Second task: Make clients to use generated token specific to them.

(Example: ClientA should take ticket {{ hostvars['server']['ticket'][0]['stdout'] }}

output example for one client: "stdout": "9338e126e8dd454820870b3ba19f5344334c8b1d" ##

Note: below play is for one client
- hosts: ClientA tasks: shell: /path/to/bin --key /path/to/store-key/ticket.key --ticket {{ hostvars['server']['ticket']['stdout'] }}

Above plays works completely fine with one client but no idea to write play for multiple clients (in loop)

Need inputs to write shell value for below play (for multiple clients) ##

 - hosts: "{{ clients }}" vars: clients: - clientA - ClientB tasks: shell: /path/to/bin --key /path/to/store-key/ticket.key --ticket !!!!!!!!Please your input here !!!!!!!!!

How can we achieve it?

##

One of possible solutions is to

  1. Add to hosts in clients group index of the host
clients:
  hosts:
    clientA:
      uid: 0
      <etc>
    clientB:
      uid: 1
      <etc>
  1. Add loop to server part (see below)
  2. Address the client's token by its uid as array index in ticket variable
- hosts: serverA
  tasks:
   - name: generate ticket on server and save it as a variable
     shell: /path/to/bin ticket {{ item }} 
     register: ticket
     with_items:
      - "{{ groups['clients'] }}"

- hosts: clients
  tasks:
   - name: checkticket
     shell: /path/to/bin --key /path/to/store-key/ticket.key --ticket {{ hostvars['serverA']['ticket']['results'][uid]['stdout'] }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM