Skip to content

Instantly share code, notes, and snippets.

@AbhishekPednekar84
Last active January 16, 2020 08:54
Show Gist options
  • Save AbhishekPednekar84/ca03404b69260bdab1e981ed3f6f3af7 to your computer and use it in GitHub Desktop.
Save AbhishekPednekar84/ca03404b69260bdab1e981ed3f6f3af7 to your computer and use it in GitHub Desktop.
pyyaml - using anchors across two YAML documents in the same file
---
host: phl-42
datacenter:
location: &BLR Bangalore
cabinet: !!str 13
unit: 3
roles:
- webserver
- wp_database
---
host: phl-43
datacenter:
location: *BLR
cabinet: !!str 13
unit: 4
roles:
- webserver
- wp_database
...
---
import yaml
def my_compose_document(self):
self.get_event()
node = self.compose_node(None, None)
self.get_event()
return node
yaml.composer.Composer.compose_document = my_compose_document
with open("host.yml", "r") as f:
docs = yaml.load_all(f, Loader=yaml.FullLoader)
for doc in docs:
if doc:
for k, v in doc.items():
print(f"{k}: {v}")
else:
break
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment