{"id":16956991,"url":"https://github.com/jgraichen/salt-template","last_synced_at":"2025-05-07T23:39:19.700Z","repository":{"id":43418188,"uuid":"341294678","full_name":"jgraichen/salt-template","owner":"jgraichen","description":"Shared salt templates and functions to serialize data and text","archived":false,"fork":false,"pushed_at":"2025-01-13T04:27:09.000Z","size":207,"stargazers_count":2,"open_issues_count":1,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-01-18T21:38:17.430Z","etag":null,"topics":["salt","saltstack","template","templates"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jgraichen.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-22T18:17:38.000Z","updated_at":"2025-01-13T04:27:12.000Z","dependencies_parsed_at":"2023-12-23T09:29:56.878Z","dependency_job_id":"5104c445-fcba-49e4-83de-6e65c79d6b01","html_url":"https://github.com/jgraichen/salt-template","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgraichen%2Fsalt-template","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgraichen%2Fsalt-template/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgraichen%2Fsalt-template/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgraichen%2Fsalt-template/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jgraichen","download_url":"https://codeload.github.com/jgraichen/salt-template/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235547089,"owners_count":19007621,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["salt","saltstack","template","templates"],"created_at":"2024-10-13T22:16:34.881Z","updated_at":"2025-01-25T06:41:05.427Z","avatar_url":"https://github.com/jgraichen.png","language":"Python","readme":"# salt-template\n\nThis repository provides salt template files and module functions to serialize data and text into configuration files.\n\n## Examples\n\n```yaml\n/etc/default/application:\n  file.managed:\n    - template: py\n    - source: salt://_templates/env.py\n    - context:\n        # Default config data to merge with pillar data\n        default:\n          LOG_LEVEL: debug\n\n        # Load data from one or multiple pillar keys and merge\n        source:\n          - env:default\n          - env:application\n```\n\n```yaml\n/etc/systemd/system/application.service:\n  file.managed:\n    - template: py\n    - source: salt://_templates/systemd.py\n    - context:\n        default:\n          Unit:\n            Description: My Demo Application\n          Service:\n            EnvironmentFile: -/etc/default/application\n            ExecStart: /usr/local/bin/application -l $LOG_LEVEL\n```\n\n```yaml\n/rails/config/database.yaml:\n  file.managed:\n    - template: py\n    - source: salt://_templates/yaml2.py\n    - context:\n        source: myapp:rails:database\n        root: production\n```\n\n## Installation\n\nThe recommended installation uses salt `GitFS` to include this repository into your state tree:\n\n```yaml\ngitfs_remotes:\n  - https://github.com/jgraichen/salt-template.git:\n      - base: v1.2.0\n```\n\nIt is recommended to check out a specific revision to avoid getting unexpected updates or changes.\n\n## Templates\n\nThe repository ships a set of Python templates to serialize different kind of files. The can be used via e.g. `file.managed`. Templates accept configuration via the `context` option.\n\nAll templates accept a `source` and a `default` option. Some templates have more options to tweak things specific to them. They all use [`template.managed`](#templatemanaged) to render the final output, see [here](#templatemanaged) for details and more options.\n\n\u003cdl\u003e\n\u003cdt\u003e\n\n`source` (string, list, optional)\n\n\u003cdd\u003e\n\nA comma-separated string or a list of pillar keys to load data from. The pillar values will be recursively merged, lists will be concatenated.\n\n```yaml\n/etc/salt/minion.d/minion.conf:\n  file.managed:\n    - template: py\n    - source: salt://_templates/yaml2.py\n    - context:\n        source:\n          - salt:common\n          - salt:minion\n```\n\n\u003cdt\u003e\n\n`default` (optional)\n\n\u003cdd\u003e\n\nSome default data to use with the template. If an additional `source` is specified, it will be merged into the default data.\n\n```yaml\n/etc/salt/minion.d/minion.conf:\n  file.managed:\n    - template: py\n    - source: salt://_templates/yaml2.py\n    - context:\n        default:\n          log_level: INFO\n        source: salt:minion\n```\n\nUsing `default` without `source` only renders the given default data.\n\n\u003c/dl\u003e\n\n### Environment\n\nRenders a single-level dictionary into an environment file.\n\n```yaml\n/etc/default/application:\n  file.managed:\n    - template: py\n    - source: salt://_templates/env.py\n    - context:\n        default:\n          KEY: 1\n          LONG_VALUE: |\n            Long string\n            on multiple lines\n```\n\n```env\n# This file is managed by salt. Changes will be overwritten.\n\nKEY=1\nLONG_VALUE='Long string\non multiple lines'\n```\n\n### Sysctl\n\nRenders a sysctl-like configuration with additional list support.\n\n```yaml\n/etc/rabbitmq/rabbitmq.conf:\n  file.managed:\n    - template: py\n    - source: salt://_templates/sysctl.py\n    - context:\n        default:\n          cluster_formation.peer_discovery_backend: classic_config\n          cluster_formation.classic_config.nodes:\n            - rabbit@hostname1.example.org\n            - rabbit@hostname2.example.org\n```\n\n```\n# This file is managed by salt. Changes will be overwritten.\n\ncluster_formation.peer_discovery_backend = classic_config\ncluster_formation.classic_config.nodes.1 = rabbit@hostname1.example.org\ncluster_formation.classic_config.nodes.2 = rabbit@hostname2.example.org\n```\n\n_Note:_ List index starts with `1`.\n\n### Systemd\n\nRenders a file using a [`systemd.syntax`](https://www.freedesktop.org/software/systemd/man/systemd.syntax.html) approximation.\n\n```yaml\n/etc/systemd/system/application.service.d/override.conf:\n  file.managed:\n    - template: py\n    - source: salt://_templates/systemd.py\n    - context:\n        default:\n          Unit:\n            After: consul.service\n          Service:\n            Environment: KEY=1\n            ExecStart: [Null, /usr/local/bin/application]\n```\n\n```systemd\n# This file is managed by salt. Changes will be overwritten.\n\n[Unit]\nAfter=consul.service\n\n[Service]\nEnvironment=KEY=1\nExecStart=\nExecStart=/usr/local/bin/application\n```\n\n#### Additional arguments\n\n\u003cdl\u003e\n\u003cdt\u003e\n\n`section` (string, optional)\n\n\u003cdd\u003e\n\nRender the given data as a flat dictionary into the given section.\n\n```yaml\n/etc/systemd/resolved.conf.d/override.conf:\n  file.managed:\n    - template: py\n    - source: salt://_templates/systemd.py\n    - context:\n        default:\n          DNS: 127.0.0.1\n          Domains: ~consul\n        section: Resolve\n```\n\n```systemd\n# This file is managed by salt. Changes will be overwritten.\n\n[Resolve]\nDNS=127.0.0.1\nDomains=~consul\n```\n\n\u003c/dl\u003e\n\n### Text\n\nRenders a list of text blobs into a combined file.\n\n```yaml\n/etc/application/config:\n  file.managed:\n    - template: py\n    - source: salt://_templates/text.py\n    - context:\n        default: |\n          First blob.\n        source:\n          - pillar:key:one\n          - pillar:key:two\n```\n\n```text\n# This file is managed by salt. Changes will be overwritten.\n\nFirst blob.\n\n\n# pillar:key:one\n\nBlob from first pillar key.\n\n\n# pillar:key:two\n\nBlob from second pillar key.\n```\n\n_Note:_ The text template recognizes `comment_prefix` from [`template.managed`](#templatemanaged) and uses this to prefix source comments.\n\n### Yaml2\n\nRenders into a YAML document.\n\n```yaml\n/etc/application/config.yaml:\n  file.managed:\n    - template: py\n    - source: salt://_templates/yaml2.py\n    - context:\n        default:\n          database:\n            host: 127.0.0.1\n            port: 1234\n        source: pillar:key\n```\n\n```yaml\n# This file is managed by salt. Changes will be overwritten.\n\ndatabase:\n  host: 127.0.0.1\n  port: 1234\n  name: from_pillar\n```\n\n#### Additional arguments\n\n\u003cdl\u003e\n\u003cdt\u003e\n\n`root` (string, optional)\n\n\u003cdd\u003e\n\nA colon-separated string to recursively nest the data into the given path. Useful if applications expected the configuration in a specific path, but you do not want to have that in the source pillar.\n\n```yaml\n/rails/config/database.yaml:\n  file.managed:\n    - template: py\n    - source: salt://_templates/yaml2.py\n    - context:\n        source: myapp:rails:database\n        root: production\n```\n\n```yaml\n# This file is managed by salt. Changes will be overwritten.\n\nproduction:\n  adapter: postgresql\n  hostname: 127.0.0.1\n```\n\n\u003c/dl\u003e\n\n### config (ini/properties)\n\nRenders into an ini/properties file using Pythons `configparser` module.\n\n```yaml\n/etc/application/config.ini:\n  file.managed:\n    - template: py\n    - source: salt://_templates/config.py\n    - context:\n        default:\n          DEFAULT:\n            enabled: \"yes\"\n          DATABASE:\n            host: 127.0.0.1\n            port: 1234\n        source: pillar:key\n```\n\n```ini\n# This file is managed by salt. Changes will be overwritten.\n\n[DEFAULT]\nenabled = yes\n\n[DATABASE]\nhost = 127.0.0.1\nport = 1234\n```\n\n#### Additional arguments\n\n\u003cdl\u003e\n\u003cdt\u003e\n\n`section` (string, optional)\n\n\u003cdd\u003e\n\nA section name to scope all data into. Useful when the target file only\nneeds a single section.\n\n```yaml\n/etc/application/config.ini:\n  file.managed:\n    - template: py\n    - source: salt://_templates/config.py\n    - context:\n        source: app:config\n        section: [config]\n```\n\n```ini\n# This file is managed by salt. Changes will be overwritten.\n\n[config]\ndatabase = postgresql\nhostname = 127.0.0.1\n```\n\n\u003c/dl\u003e\n\n## Execution modules\n\n### `template.managed`\n\nThis execution module takes a string or a list of lines and renders this into a consistent text. It will add preamble and ensure there is a final newline.\n\nThe preamble text is loaded via [`config.get`](https://docs.saltproject.io/en/latest/ref/modules/all/salt.modules.config.html#salt.modules.config.get) using the `template_managed` key. Therefore the preamble can be specified everywhere including the salt master configuration. This allows to easily set custom message specific to a salt master, e.g.:\n\n```yaml\n# /etc/salt/master\ntemplate_managed: \u003e\n  This file is part of the salt.example.org\n  collective. Resistance is futile.\n```\n\nAll provided templates here use `template.managed` to render the final output. Options from the template are passed through to the module function.\n\n#### Arguments\n\n\u003cdl\u003e\n\u003cdt\u003e\n\n`text` (string or list, required)\n\n\u003cdd\u003e\n\nA text string or a list of lines.\n\n\u003cdt\u003e\n\n`preamble` (boolean, default: `True`)\n\n\u003cdd\u003e\n\nSet to `False` to not add a preamble.\n\n\u003cdt\u003e\n\n`comment_prefix` (string, default: `\"#\"`)\n\n\u003cdd\u003e\n\nThe string to prepend each line from preamble and comment with. If set to `False`, no preamble or comment will be added.\n\n\u003cdt\u003e\n\n`comment` (string, optional)\n\n\u003cdd\u003e\n\nAn additional comment to be added in front of the text.\n\n\u003c/dl\u003e\n\n#### Example: Using it in your own template\n\n```py\n#!py\n\ndef run():\n    # generate complex config file\n    config = \"Very complex config!\"\n\n    return __salt__[\"template.managed\"](config, comment_prefix=\"//\")\n```\n\n```text\n// This file is managed by salt. Changes will be overwritten.\n\nVery complex config!\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjgraichen%2Fsalt-template","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjgraichen%2Fsalt-template","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjgraichen%2Fsalt-template/lists"}