3 ms·
_Technically_ there's a Python API on it: https://docs.ansible.com/ansible/latest/dev_guide/developing_api.html https://docs.ansible.com/ansible/latest/dev_guid
by geerlingguy 7y ago
_Technically_ there's a Python API on it: https://docs.ansible.com/ansible/latest/dev_guide/developing_api.html https://docs.ansible.com/ansible/latest/dev_guide/developing...
There's also a utility to help with the running of Ansible playbooks via Python (https://github.com/ansible/ansible-runner https://github.com/ansible/ansible-runner).
However, I've said this many times before—if your playbooks start mixing Python into tasks, you're either doing something wrong (it could be architected better), or it's time to write a custom Ansible module so the Python goes into a `.py` file and not into YAML.
99% of my playbooks are strict YAML with maybe a few Jinja filters, which make everything very readable. But I've also seen a ton of YAML that makes me want to barf.
- BiteCode_dev 7y agoKeyword being "friendly". See, if hello world is: - name: Hello Ansible hosts: ansibleclient01.local tasks: - name: Copy '/tmp/testfile.txt' into 'hello world'. copy: content: hello worldn dest: /tmp/testfile.txt Then it would become: from icandream import playbook pb = playbook("Hello Ansible", hosts=["ansibleclient01.local"]) with db.define_task(Copy '/tmp/testfile.txt' into 'hello world') as task: task.copy(content="hello worldn", dest="/tmp/testfile.txt) Instant benefits: - Python interpretter is very good at telling you error messages. - You get an exception system, stack trace and a debugger for free - The entire python tooling ecosystem, including full monthy IDE support, linters and formaters, is at your disposal - Any more complex example is going to be much cleaner than the yaml version, exponentially. And no need to live in the "yaml/python module" dichotomy to keep PB clean. - No need to reinvent the wheel. Python has elegant import, namespaces, ways to pass data around, conditionals, etc., - You can import Python libs. Event if it's just for declarative stuff like pendulum, appdirs.. The only cons are: - idempotence needs to be enforced culturally. It's already the case for people writing the module behind the scene, and a good API + some warning tricks would be enough IMO. - can't read the files outside of Python. But nobody does it anyway, and we can always let the Python files output the yaml representation if needed. Now I don't hold my breath because it's a lot of work and the ansible community usually don't like the idea at all, and I'm not willing to do it. Fair is fair.