I want to write a script that imports data of a csv file with users and generates a yaml file used in ansible to create Linux users. The csv file is list with the following form:
users = [
['user1', 'passwordUser1' ['wheel', 'users']],
['user2', 'passwordUser2' ['wheel', 'users']],
...
]
The yaml file must have the following form:
users:
- name: user1
groups:
- wheel
- users
password: passwordUser1
- name: user2
groups:
- wheel
- users
password: passwordUser2
- ...
I have already found a source to export from a dictionary, but it reorders the fields alphabetically. (groups before name)
My code is the following:
import yaml
output = [{'users': [{'name': 'user1', 'groups': ['wheel', 'users'], 'password': 'user1p'}]}]
with open(r'/home/maarten/git/elnx-1920-sme-maartenbeeckmans/assignment/store_file.yaml', 'w') as file:
documents = yaml.dump(output, file)
Yaml file it generates
- users:
- groups:
- wheel
- users
name: user1
password: user1p