dharpatel
dharpatel

Reputation: 467

how to use variable from .yml file to .py file

I have below two files one is .py file and other is .yml file as shown in image. In .py file I am using all variable that define in .yml file. Now I am looking for a solution about How I can pass this .yml file or how I can call or use the variables from .yml file to .py file. I have also marked my question in image to help to understand my question exactly.

Any suggestion will be very helpful.

Thanks

-> Below is abcd.py file

# abcd.py

import pexpect
import os
import yaml
from yaml import load
from pexpect import pxssh
import pdb


with open('/home/asdf/Desktop/test.yml', 'rb') as f:
 var=yaml.load(f.read())

def example(gw_user,gw_password,gw_host):
  child = pexpect.spawn("ssh"+" "+gw_user+"@"+gw_host,timeout=30)
  child.expect('password')
  child.sendline(gw_password)
  #child.expect(self.gw_prompt)
  print 'connection established'
  child.expect('$')
  child.sendline('cd /usr/local/lib/python2.7/dist-packages/ostinato/')
  child.expect('ostinato')
  child.sendline('python example.py')
  print 'establishing connectivity with ostinato'
  child.expect('127.0.0.1')
  child.sendline('10.0.0.3')
  child.expect('Tx')
  child.sendline('1')
  child.expect('Rx')
  child.sendline('1')
  child.expect('$')
  child.sendline('exit')
  child.interact()
  #return self.gw_user
 #pdb.set_trace()
answer=example(var[Username],var[Userpassword],var[Hostname])
print (answer)

-> Below is test.yml file

---
- Username:
      - xyz
- Userpassword:
      - ubuntu 
- Hostname:
      - 10.0.0.3

Also attached below screenshot for better understanding of my question.

enter image description here

Upvotes: 3

Views: 7652

Answers (1)

mhawke
mhawke

Reputation: 87134

Because of the structure of the YAML document, yaml.load() returns a list of dictionaries:

with open('test.yml') as f:
    var = yaml.load(f)

>>> var
[{'Username': ['xyz']}, {'Userpassword': ['ubuntu']}, {'Hostname': ['10.0.0.3']}]

This is not the most usable data structure; not only is there a list of single key dictionaries, but the values themselves are lists that must be indexed to get to their contents. A single dictionary would be more convenient so, if you are able to, you could change the YAML to:

---
Username: xyz
Userpassword: ubuntu 
Hostname: 10.0.0.3

which represents a single dictionary:

>>> var
{'Username': 'xyz', 'Userpassword': 'ubuntu', 'Hostname': '10.0.0.3'}

Now it's easy to pass these values to your function:

answer = example(var['Username'], var['Userpassword'], var['Hostname'])

If you can't change the YAML file, then you can first make a single dictionary out of the data, and then use that to call the function as above:

with open('test.yml') as f:
    var = yaml.load(f)
    var = {k:v[0] for d in var for k,v in d.items()}
    answer = example(var['Username'], var['Userpassword'], var['Hostname'])

Here the line var = {k:v[0] for d in var for k,v in d.items()} is a dictionary comprehension that converts the list of single-key dictionaries into a single multi-key dictionary.

Upvotes: 5

Related Questions