Skip to content

Commit

Permalink
simplify python code
Browse files Browse the repository at this point in the history
  • Loading branch information
sbbroot committed Jan 5, 2022
1 parent 549a9cf commit cc8e896
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 37 deletions.
43 changes: 8 additions & 35 deletions ansible/playbooks/filter_plugins/ini.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Dict
import configparser


Expand All @@ -7,53 +8,25 @@ class FilterModule(object):
def filters(self):
return {
'from_ini': self.from_ini,
'get_ini_value': self.get_ini_value
}

def from_ini(self, content: str) -> str:
def from_ini(self, content: str, default_section_name: str = '__none__') -> Dict:
"""
Parse `content` in ini format which was obtained from a decoded file.
:param content: to be parsed
:return: properly parsed ini content which can be further processed
:param default_section_name: fields without section will be available under this key
:return: properly parsed ini content
"""
return content.replace('\\n', '\n').replace('\\', '')
fixed_content = content.replace('\\n', '\n').replace('\\', '')

def get_ini_value(self, content: str, field: str, section: str, accept_empty_field: bool = True) -> str:
"""
Search for `field` in `content` under `section`
Example:
get_ini_value(content, 'some_field', 'some_section')
Where content is:
```
[some_section]
some_filed = 150
```
will return `150`
:param content: where to search for section/field
:param field: for which value will be searched for
:param section: under which `field` should be search for
:param accept_empty_field: if set to False will raise error on empty field or when section/field not found,
empty string will be returned otherwise
:return: value of `section/field`
"""
config = configparser.ConfigParser()

try:
config.read_string(content)
config.read_string(fixed_content)
except configparser.MissingSectionHeaderError:
# content might be missing default header, add it and try to parse it once more
config = configparser.ConfigParser()
config.read_string(f'[DEFAULT]\n{content}')
config.read_string(f'[{default_section_name}]\n{fixed_content}')

if not accept_empty_field:
return config[section][field]

try:
return config[section][field]
except Exception:
return ''
return {section: dict(config.items(section)) for section in config.sections()}
2 changes: 1 addition & 1 deletion ansible/playbooks/roles/grafana/tasks/password_change.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

- name: Fetch the password
set_fact:
current_admin_password: "{{ grafana_config_file_content | get_ini_value(field='admin_password', section='security') }}"
current_admin_password: "{{ grafana_config_file_content['security']['admin_password'] }}"

# Grafana admin password change is only available through the grafana-cli
- name: Change admin password using grafana-cli
Expand Down
2 changes: 1 addition & 1 deletion ansible/playbooks/roles/grafana/tasks/upgrade/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@
path: "{{ upgrade_defaults.state_file_path }}"
state: absent

- name: Run password change
- name: Change password
include_tasks: password_change.yml

0 comments on commit cc8e896

Please sign in to comment.