200-901 Question 23
Select 2A DevOps engineer is tasked with updating a network automation script to dynamically add or remove devices from a network inventory stored in a custom YAML file. The script must ensure the changes are reflected correctly without overwriting existing data. Which Python modules and methods would be most appropriate for this task?
- A
Use the
yamlmodule to parse and modify the YAML file. - B
Use the
jsonmodule to convert the YAML file into JSON for easier manipulation. - C
Use the
open()function in write ('w') mode to rewrite the entire YAML file after making changes. - D
Use the
append()method to directly add new devices to the YAML file. - E
Use the
ruamel.yamllibrary for preserving the structure and comments of the YAML file.
Show answer and explanation
Correct answers: A, E
Explanation
To dynamically add or remove devices from a YAML-based inventory file, it is important to use libraries and methods tailored for YAML file manipulation. The yaml module allows for parsing and modifying YAML content, while the ruamel.yaml library goes a step further by preserving the file's structure, formatting, and comments, ensuring that updates do not disrupt the integrity of the file. Overwriting the file or using inappropriate methods, such as handling YAML as JSON, can lead to data loss or corruption.
- A. Correct.
Correct: The
yamlmodule provides tools to parse and modify YAML files directly, making it a suitable choice for reading and updating the network inventory. - B. Incorrect.
Incorrect: While the
jsonmodule is useful for handling JSON data, YAML is a different format and requires a dedicated library for proper handling. - C. Incorrect.
Incorrect: Using
open()in write ('w') mode will overwrite the entire file, which does not align with the requirement to preserve existing data. - D. Incorrect.
Incorrect: The
append()method does not directly apply to file manipulation in Python. It is commonly used for lists, not for appending data directly to files. - E. Correct.
Correct: The
ruamel.yamllibrary is specifically designed for YAML file manipulation and is capable of preserving the structure, formatting, and comments, making it ideal for dynamically adding or removing devices.