I'm looking into YAML and must admit, I'm struggling a bit. I have a program which reads a JSON config file (and works just fine) but I am refactoring it to work with a YAML config file, for one, my own education and two, a more human friendly config file for users.
In short, I want to have two key value pairs, eg
log-file: /var/log/file
api-key: /api/key
which are only defined once. I then wish to define a host, and be able to have multiple hosts in my configuration file.
Here is an example of a host.
fqdn: firewall.example.com.
project_id: fluffy-penguin-242411
managed_zone: example
domain: example.com
ttl: 60
interval: 600
Is there anyone here who can help me define a YAML file which I can easily work with in Python?
I've managed to get basic configurations into my code, but not a configuration which matches with what I want to do.
Ultimately, I want to be able to have a list or a dictionary of hosts which I can iterate through.
Cheers,
C
Update
I think I might be on the right track with something like this
api-key: './ddns-api-key.json'
log-path: './ddns.log'
hosts:
- fqdn: 'firewall.example.com.'
project_id: 'fluffy-penguin-242411'
managed_zone: 'example'
domain: 'example.com'
ttl: 60
interval: 600
You can't do exactly that, but you can do something similar by using a named key/value pair object in a list, like so:
api-key: './ddns-api-key.json'
log-path: './ddns.log'
hosts:
- host1:
fqdn: 'firewall.example.com.'
project_id: 'fluffy-penguin-242411'
managed_zone: 'example'
domain: 'example.com'
ttl: 60
interval: 600
- host2:
fqdn: 'stuff.example.org.'
project_id: 'fluffy-bear-213461'
managed_zone: 'example'
domain: 'example.org'
ttl: 70
interval: 700
You'll just have to use a unique name for each list entry - I used host1
, host2
, etc. in this example.
I believe that the following which I came up with, and with the help of connectyourcharger from the above post, works:
api-key: './ddns-api-key.yaml'
logfile: './ddns.log'
hosts:
- host: 'firewall.example.com.'
project_id: 'fluffy-penguin-242411'
managed_zone: 'example'
domain: 'example.com'
ttl: 60
interval: 600
- host: 'www.example-two.com.'
project_id: 'fluffy-penguin-242411'
managed_zone: 'example-two'
domain: 'example-two.com'
ttl: 60
interval: 600
When I access this using the following python, I am able to loop through the YAML hosts and retrieve all the configuration items.
import yaml
# Read YAML file
with open("configuration.yaml", 'r') as stream:
try:
yaml_data = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
print(yaml_data['api-key']) # prints ./ddns-api-key.yaml
for host in yaml_data['hosts']:
print(host['host']) # prints firewall.example.com and www.exmaple-two.com
print(host['ttl']) # prints 60 twice
print(host['domain']) # prints example.com and example-two.com
Cheers, C
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.