NANIS
NANIS

Reputation: 113

How to ask Python to update the variables for YAML?

I want to get values from python to put them in YAML:

import ruamel.yaml

file_name = 'input.yml'
from ruamel.yaml.util import load_yaml_guess_indent

config, ind, bsi = load_yaml_guess_indent(open(file_name))

instances = config['instances']
print instances
instances['hostname'] = raw_input('What is the host name>>')
instances['syslog'] = raw_input ('what is the Syslog ip address>>')
instances['prefix'] = raw_input('What is the first prefix>>')
instances['prefix'] = raw_input ('what is the second address>>')

input.yml:

---
instances:
      hostname: <name>              # update with IP
      syslog: <ip>    # update with user name
      interfaces:
         - name: GigabitEthernet0/0
           prefix: <first prefix>        
         - name: GigabitEthernet0/1
           prefix: <second prefix>

host and syslog part is working but I could not make prefix working.

Upvotes: 0

Views: 151

Answers (1)

Anthon
Anthon

Reputation: 76842

This:

instances['prefix'] = raw_input('What is the first prefix>>')
instances['prefix'] = raw_input ('what is the second address>>')

is not working (and if it would, you would update the same thing twice). You need to take into account that prefix is a key on an element of sequence (Python list) that is the value for the key interfaces:

instances['interfaces'][0]['prefix'] = raw_input('What is the first prefix>>')
instances['interfaces'][1]['prefix'] = raw_input ('what is the second address>>')

Upvotes: 1

Related Questions