Inline commenting in YAML files - yaml

I'm using the following linter to check if my yml is valid: http://www.yamllint.com/
I tried putting inline comments, but they are removed. I read the spec here http://www.yaml.org/spec/1.2/spec.html#id2780069
Am I correct that the following is actually valid and that the linting website is wrong by removing the comments?
cache:
paths:
- node_modules/ # some comment here

Your source is correct. If you want to run such a check with preservation of the comments, or reformat preserving the comments, then use a small Python program based on ruamel.yaml, which can preserve your comments on round-trip and normalize the indentation (disclaimer: I am author of ruamel.yaml):
import sys
from ruamel.yaml import YAML
from ruamel.yaml.util import load_yaml_guess_indent
with open(sys.argv[1]) as fp:
data, ind, offset = load_yaml_guess_indent(fp)
yaml = YAML()
yaml.indent(mapping=ind, sequence=ind, offset=offset)
yaml.dump(data, sys.stdout)
just provide the input file as parameter on the commandline.
This has the advantage over all web based checkers that your possible sensitive data doesn't get "published".
It also has the advantage over yamllint.com and some other sites, that it supports YAML 1.2. yamllint.com only supports YAML 1.1, what you can see if you try it with an explicit YAML document directive:
%YAML 1.2
---
a: 0o7
...
That site throws an error that that version is not supported. Which is better than what e.g. http://yaml-online-parser.appspot.com/ does (directive ignored, parsed as if YAML 1.1, with the octal integer scalar as if it where a string scalar) or the half-fledged implementation of YAML at http://beautifytools.com/yaml-validator.php (Error: unable to parse))

Related

How to import Markdown with Sphinx and myst-parser into Readthedocs?

In my docs/source/conf.py I have this code:
extensions = [
"sphinx.ext.autodoc",
"myst_parser",
]
source_suffix = ['.rst', '.md']
All my files are in Markdown, not in reStructuredText.
On ReadTheDocs, the initial build was successful technically, only until the modules page was "not found" and the index page is completely blank.
My .readthedocs.yaml:
# .readthedocs.yaml
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
# Required
version: 2
# Build documentation in the docs/ directory with Sphinx
sphinx:
configuration: docs/source/conf.py
# Optionally build your docs in additional formats such as PDF
formats: all
# Optionally set the version of Python and requirements required to build your docs
python:
version: "3.8"
install:
- method: pip
path: .
extra_requirements:
- docs
My setup.cfg contains this:
[options.extras_require]
docs =
sphinx
myst-parser
Of course, doing make clean html in docs/ works completely fine. Not sure why ReadTheDocs cannot properly link to the rest of the.md files from index.md.
I solved it. I checked the build logs, turns out I have some syntax errors (I did list[str] not List[str] with from typing import List)

How load a yaml file inside an ansible custom module

I have an ansible custom module, that have a configuration file in YAML format.
Now the question is how should I load that YAML file inside the module?
NOTE as I understand I can't simply use something like PyYAML since ansible will run my module on the node that it is configuring and maybe that system does not have PyYAML installed.
NOTE Also ansible itself have ansible.parsing.utils.yaml.from_yaml it is not usable by the modules.
So funny as it may sound, I don't know how to load a YAML file in custom ansible module. Please help
It's a great question. It does sound funny and you'd expect a simple answer but as far as i can see these are the facts.
The latest development branch of ansible has /lib/ansible/module_utils/common/yaml.py which can be used by modules because it is under module_utils. see here
If you look at the source code all it's doing is import yaml as _yaml, which you could do yourself inside your custom module. My understanding is this is using PyYAML, which is documented here. (someone correct me if I"m wrong! I don't fully understand the comment in that file stating "preferring the YAML compiled C extensions...")
Anyway, if your target machine does not have PyYAML you can always add a task to ensure its there. e.g.
- name: Install PyYAML python package
pip:
name: pyyaml
and then use it in your own module with:
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
# ...
data = load(stream, Loader=Loader)
# ...
output = dump(data, Dumper=Dumper)

EYAML syntax validator?

I had an issue with an eyaml file used to store password for DB connection and it seems that I missed a "[".
I want to know if there is a command or script to check eyaml syntax
One thing you can do, if you have python installed somewhere, is install ruamel.yaml (disclaimer: I am the author of that package) and run the following:
python check.py your_eyaml_file
with check.py being:
import sys
from ruamel.yaml import YAML
yaml = YAML()
yaml.load(sys.argv[1])
This will do a safe load of your YAML file and will throw an error if your file doesn't conform to the YAML specification.
There are also online parsers when you can run such checks, but I would not want to use them with sensitive information (encrypted or not).

Get hiera value in puppet manifest with nested keys

I'm new to using hiera with puppet (and somewhat new to puppet).
I've written this bit of yaml:
---
web_mysql_server:
mysql_database: "my_production"
and then I try to use it in a puppet manifest thus:
database => hiera('web_mysql_server::mysql_database'),
And this does not work, generating the error
Error: Could not find data item web_mysql_server::mysql_database
in any Hiera data file and no default supplied at
/vagrant/puppet/modules/web_mysql_server/manifests/init.pp:33
on node railstest.vm
(where I've added some '\n''s for readability here).
I suspect (hope!) this is a simple syntax error that I'm not getting. Anyone see what I'm doing wrong?
Based in hiera documentation, the proper syntax for accessing nested keys is
hiera('web_mysql_server.mysql_database')
This syntax for qualified keys has not been around always, the documentation says it's from Hiera 2.0.
I know we use puppet 3.8.3 and qualified keys are not supported yet in our setup. We work around this limitation with this approach:
$mysql_configuration = hiera('web_mysql_server')
# ...
database => $mysql_configuration['mysql_database']

Pylint: "locally defined disables" still give warnings. How to suppress them?

I work with a software framework which has a couple of classes with method names containing capital letters (due to C++ wrappers). This is of course not PEP8 and pylint shows the corresponding error C0103. I also added C0111 to the list to ignore the missing docstrings for some methods, like this:
def Configure(self): # pylint: disable=C0103,C0111
It works, however now I get warnings because of the local disablings:
Class: I0011 -> locally disabling C0103
Class: I0011 -> locally disabling C0111
How should I suppress them?
OK, so obviously one has to ignore the ignore-warning explicitly. One can do this in the pylint config file: if you don't have one, simply generate a standard configuration via
pylint --generate-rcfile > pylint.rc
and uncomment the line with disable=... and add I0011 to the list. This suppresses all warnings regarding "locally defined disablings".
The other method is to add the following line to the beginning of a file (or block, whatever), if you don't want to suppress the warning globally:
#pylint: disable=I0011

Resources