pypyr.steps.filewriteyaml permalink

create yaml file from any context object permalink

Format & write a payload to a yaml file on disk. This is useful for generating yaml files from your pipeline such as when you want to create configuration files dynamically on the fly.

filewriteyaml works like this:

- name: pypyr.steps.filewriteyaml
  comment: write context payload out to yaml
  in:
  fileWriteYaml:
    path: /path/to/output.yaml # destination file
    payload: # (optional) payload to write to path
      key1: value1 # output yaml will have
      key2: value2 # key1 and key2 as string.
      key3: 124 # output int
      key4: false # output bool

This will generate the following yaml to /path/to/output.yaml:

key1: value1
key2: value2
key3: 123
key4: false

Note that the correct data types will serialize to the generated output yaml. More complicated hierarchical nested structures will also just work.

If you do not specify payload, pypyr will write the entire context to the output file in yaml format. Be careful if you have sensitive values like passwords or private keys!

format yaml with token replacements permalink

All inputs support substitutions. This means you can specify another context item to be the path and/or the payload, for example:

arbkey: arbvalue
writehere: /path/to/output.yaml
writeme:
  this: yaml content
  will: be written to
  thepath: with substitutions like this {arbkey}.
fileWriteYaml:
  path: '{writehere}'
  payload: '{writeme}'

Substitution processing runs on the output. In this example, in the output yaml file created at /path/to/output.yaml, the {arbkey} expression in the last line will substitute like this:

this: yaml content
will: be written to
thepath: with substitutions like this arbvalue.

As always in pypyr, you can construct a value by embedding & combining substitution expressions in other strings:

- name: pypyr.steps.set
  comment: set some arb values in context
  in:
    set:
      arbkey: 123
      arbstr: in the middle
      filename: my-file

- name: pypyr.steps.filewriteyaml
  comment: write toml file from substitution expressions
  in:
    fileWriteYaml:
      path: out/{filename}.yaml
      payload:
        my_table:
          my_number: '{arbkey}'
          my_string: begin {arbstr} end

This will to output file ./out/my-file.yaml:

my_table:
  my_number: 123
  my_string: begin in the middle end

See a worked filewriteyaml example.

encoding permalink

By default the file will write in the platform’s default encoding. This is utf-8 for most systems, but be aware on Windows it’s still cp1252.

You can use the encoding input explicitly to set the encoding:

- name: pypyr.steps.filewriteyaml
  in:
    fileWriteYaml:
      path: out/utf8-example.yaml
      payload:
        mykey: "€ ∮ E⋅da = Q,  n → ∞, ∑ f(i) = ∏ g(i), ∀x∈ℝ: ⌈x⌉ = −⌊−x⌋, α ∧ ¬β = ¬(¬α ∨ β)"
      encoding: utf-8

See here for more details on handling text encoding in pypyr and changing the defaults.

See here for a list of available encodings.

see also

last updated on .