Home > OS >  Looking to see if a key from one set of imported variables matches another so it's value can be
Looking to see if a key from one set of imported variables matches another so it's value can be

Time:08-19

I have roughly formatted yml files with key/value pairs in them. I then imported the values of both of these files successfully into a running playbook using the include_vars module.

Now, I want to be able to compare the value of the key/value pair from file/list 1, to all of the keys of file/list 2. Then finally when there is a match, print and preferably save/register the value of the matching key from file/list 2.

Essentially I am comparing a machine name to an IP list to try to grab the IP the machine needs out of that list. The name is "dynamic" and is different each time the playbook is run, as file/list 1 is always dynamically populated on each run.

Examples:

file/list 1 contents

machine_serial: m60
s_iteration: a
site_name: dud
t_number: '001'

file/list 2 contents

m51: 10.2.5.201
m52: 10.2.5.202
m53: 10.2.5.203
m54: 10.2.5.204
m55: 10.2.5.205
m56: 10.2.5.206
m57: 10.2.5.207
m58: 10.2.5.208
m59: 10.2.5.209
m60: 10.2.5.210
m61: 10.2.5.211

In a nutshell, I want to be able to get the file/list 1 ct_machine_serial key who's value is currently: m60 to be able to find it's key match in file/list 2, and then print and/or preferably register it's value of 10.2.5.210.

What I've tried so far: Playbook:

 - name: IP gleaning comparison.  
   hosts: localhost
   remote_user: ansible
   become: yes
   become_method: sudo
   vars:
     ansible_ssh_pipelining: yes

   tasks:

     - name: Try to do a variable import of the file1 file.
       include_vars:
         file: ~/active_ct-scanner-vars.yml
         name: ctfile1_vars
       become: no

     - name: Try to do an import of file2 file for lookup comparison to get an IP match.
       include_vars:
         file: ~/machine-ip-conversion.yml
         name: ip_vars
       become: no

     - name: Best, but failing attempt to get the value of the match-up IP.  
       debug:
         msg: "{{ item }}"
       when: ctfile1_vars.machine_serial == ip_vars
       with_items:
         - "{{ ip_vars }}"

Every task except the final one works perfectly.
My failed output final task:

TASK [Best, but failing attempt to get the value of the match-up IP.] ***********************************************************************************
skipping: [localhost] => (item={'m51': '10.200.5.201', 'm52': '10.200.5.202', 'm53': '10.200.5.203', 'm54': '10.200.5.204', 'm55': '10.200.5.205', 'm56': '10.200.5.206', 'm57': '10.200.5.207', 'm58': '10.200.5.208', 'm59': '10.200.5.209', 'm60': '10.200.5.210', 'm61': '10.200.5.211'})
skipping: [localhost]

What I hoped for hasn't happened, it simply skips the task, and doesn't iterate over the list like I was hoping, so there must be a problem somewhere. Hopefully there is an easy solution to this I just missed. What could be the correct answer?

CodePudding user response:

Given the files
shell> cat active_ct-scanner-vars.yml
machine_serial: m60
s_iteration: a
site_name: dud
t_number: '001'
shell> cat machine-ip-conversion.yml
m58: 10.2.5.208
m59: 10.2.5.209
m60: 10.2.5.210
m61: 10.2.5.211

Read the files

    - include_vars:
        file: active_ct-scanner-vars.yml
        name: ctfile1_vars
    - include_vars:
        file: machine-ip-conversion.yml
        name: ip_vars

Q: "Compare the machine name to an IP list and grab the IP."

A: Both variables ip_vars and ctfile1_vars are dictionaries. Use ctfile1_vars.machine_serial as index in ip_vars

match_up_IP: "{{ ip_vars[ctfile1_vars.machine_serial] }}"

gives

match_up_IP: 10.2.5.210

Example of a complete playbook for testing

- hosts: localhost
  gather_facts: false
  vars:
    match_up_IP: "{{ ip_vars[ctfile1_vars.machine_serial] }}"
  tasks:
    - include_vars:
        file: active_ct-scanner-vars.yml
        name: ctfile1_vars
    - include_vars:
        file: machine-ip-conversion.yml
        name: ip_vars
    - debug:
        var: match_up_IP
  • Related