# Ansible parser - python

**URL:** <https://forum.ansible.com/t/ansible-parser-python/32562>\
**Category:** Ansible Developer\
**Created:** [March 30, 2020, 2:09pm UTC](https://forum.ansible.com/t/ansible-parser-python/32562 "2020-03-30T14:09:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nemania\_Borovits](https://avatars.discourse-cdn.com/v4/letter/n/aca169/32.png) [@Nemania\_Borovits](https://forum.ansible.com/u/Nemania_Borovits)\
**Post date:** [March 30, 2020, 2:09pm UTC](https://forum.ansible.com/t/ansible-parser-python/32562/1 "2020-03-30T14:09:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [March 30, 2020, 3:07pm UTC](https://forum.ansible.com/t/ansible-parser-python/32562/2 "2020-03-30T15:07:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Nemania\_Borovits](https://avatars.discourse-cdn.com/v4/letter/n/aca169/32.png) [@Nemania\_Borovits](https://forum.ansible.com/u/Nemania_Borovits)\
**Post date:** [March 30, 2020, 3:48pm UTC](https://forum.ansible.com/t/ansible-parser-python/32562/3 "2020-03-30T15:48:55Z")

</div>

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…?

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [March 30, 2020, 4:02pm UTC](https://forum.ansible.com/t/ansible-parser-python/32562/4 "2020-03-30T16:02:47Z")

</div>

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](https://gist.github.com/sivel/1f850b7f577b9dc9466293034c82b19d)
