Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pybook: Python syntax to write playbooks #10966

Closed
wants to merge 1 commit into from

Conversation

muravjov
Copy link

@muravjov muravjov commented May 9, 2015

Now Ansible suggests to write playbooks in YAML and JSON.

This patch adds a possibility to write playbooks in Python language for greater flexibility. A Python playbook use a simple API to generate YAML like structures, like so:

In YAML:


---

- hosts: web
  vars:
    build_cbo: false

  tasks:
  - include: web-tasks.yml

In Python:

#!python

with mapping:
    append("hosts", "web")
    with mapping("vars"):
        append("build_cbo", False)

    with sequence("tasks"):
        with mapping:
            append("include", "web-tasks.py")

Python API consists of <with mapping> for starting dictionaries, <with sequence> for starting lists, <append> for appending whole Python objects and <append_yaml>/<populate_yaml> for mixing Python and YAML notation.

This patch also includes tests in test/unit/TestPybook.py .

@bcoca
Copy link
Member

bcoca commented May 11, 2015

We only suggest writing playbooks in YAML and don't explicitly support JSON, it just happens to be a subset of YAML. We do not want multiple ways to write a play, specially not as a programming language.

The aim is to keep ansible auditable and simple, having more than one way to write playbooks takes us in the opposite direction. Most of ansible's effort is hiding code behind the plugins, this again is counter productive.

We are going to pass on this feature.

@bcoca bcoca closed this May 11, 2015
@muravjov
Copy link
Author

Well, I proposed such an answer. And I understand your intentions, ok.

The other way, keeping playbooks auditable and simple is not that simple. For example, if you have to write many tasks with "become" like

- pip: virtualenv=/opt/catbo-web/env 
  become: yes
  become_user: catbo

then you have a lot of repetitive code, fail.
And I suppose to use this experimental possibilty (Python playbooks) to create a better DSL above Ansible to avoid such problems.

Earlier I worked with Salt and used to think to make a better deploy system with it. But now I found Ansible, which is much more better and simpler! Thanks for your work.

@bcoca
Copy link
Member

bcoca commented May 11, 2015

currently you can set become options per play, role, task include, task, soon in v2 you also get task blocks which I think will bring most of what you wanted to the standard syntax.

@muravjov
Copy link
Author

Thanks for info (task blocks).

But don't you think that you are inventing now one more programming language using YAML notation, do you? By the way, with Python syntax I can make task block myself on the fly :)

@bcoca
Copy link
Member

bcoca commented May 11, 2015

We try very hard to not be a programming language, while implementing enough elements of it to make it usable (fine line I admit). The original author also wrote Func (https://fedorahosted.org/func/) which is very much what you describe, but went in a different direction with Ansible precisely because of that experience.

I for one think it makes a much better tool (I've written plenty of CM in perl/bash/ksh/sh/python) and do sometimes miss the programming aspect. but I think the tradeoff for simplicity is a big win here.

The YAML playbooks are intended to be descriptive and not imperative, i think this is the major difference with most scripting languages. (yes i'm ignoring all lisp like things, on purpose)

@muravjov
Copy link
Author

Well, my proposed code generate the very same YAML structures, too (Python structures to be precise).

Anyway, I'll try to keep my patch in actual state. Maybe it'll be worth it.

@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants