Last active
January 16, 2020 08:54
-
-
Save AbhishekPednekar84/ca03404b69260bdab1e981ed3f6f3af7 to your computer and use it in GitHub Desktop.
pyyaml - using anchors across two YAML documents in the same file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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 | |
... | |
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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