Ansible parser - python

Hello! I need to parse ansible files effectively. Is there any python library which i can use to parse .yml/.yaml files which contain ansible deployments…? Besides pyyaml or ruamel.yaml

Ansible does not expose a python API for developers for this purpose. Under the hood, it is pyyaml that parses the files. I don’t know what your end goal is, so I cannot really make any useful recommendation, other than to use pyyaml.

Thanks for the quick answer Matt. For each ansible task, i want to get the module, parameters, etc. I want to tokenize the task body for ML purposes. Additionally, is there an AST (abstract syntax tree) for Ansible…?

There isn’t really an AST for Ansible.

Ultimately, there are ways to achieve this, they just aren’t “supported”. You could use the Ansible code to do it, but you would need to understand that it could break at any point in time without notification.

Or you could write your own tooling from scratch.

I wrote a proof of concept tool recently, that used the Ansible code. You can look at it for inspiration, but again, it may stop working at any point in time.

https://gist.github.com/sivel/1f850b7f577b9dc9466293034c82b19d