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

how to use with_items ,when and tags in suitable #33

Closed
x2y2 opened this issue Sep 24, 2019 · 7 comments
Closed

how to use with_items ,when and tags in suitable #33

x2y2 opened this issue Sep 24, 2019 · 7 comments

Comments

@x2y2
Copy link

x2y2 commented Sep 24, 2019

suitable is very good, but i don't know how to use it with when , with_items , and tags。

@href
Copy link
Contributor

href commented Sep 25, 2019

Suitable doesn't support these directives, since they basically exist to add logic to Ansible playbooks (as I understand it). However, with Suitable you have Python at your disposal, so these
constructs don't make much sense I think.

For example, from the docs, this statement:

 - name: "shut down Debian flavored systems"
    command: /sbin/shutdown -t now
    when: ansible_facts['os_family'] == "Debian"

Would be written as follows with ansible:

from suitable import Api
HOST = 'example.org'
api = Api(HOST)

# retrieve the facts
facts = api.setup().ansible_facts(HOST)

# conditional shutdown
if facts['ansible_os_family'] == 'Debian':
    api.command('/sbin/shutdown -t now')

@x2y2
Copy link
Author

x2y2 commented Sep 26, 2019

Suitable doesn't support these directives, since they basically exist to add logic to Ansible playbooks (as I understand it). However, with Suitable you have Python at your disposal, so these
constructs don't make much sense I think.

For example, from the docs, this statement:

 - name: "shut down Debian flavored systems"
    command: /sbin/shutdown -t now
    when: ansible_facts['os_family'] == "Debian"

Would be written as follows with ansible:

from suitable import Api
HOST = 'example.org'
api = Api(HOST)

# retrieve the facts
facts = api.setup().ansible_facts(HOST)

# conditional shutdown
if facts['ansible_os_family'] == 'Debian':
    api.command('/sbin/shutdown -t now')

Thank you for your feedback, Would you like to make Suitable support Ansible playbooks in the future?

@href
Copy link
Contributor

href commented Sep 26, 2019

Probably not. If you have a good use case though, please see #21 and add a comment there. Maybe it makes sense to add support for it.

@jself
Copy link

jself commented Oct 22, 2019

I'm curious how this works for multiple hosts, for example right now I'm doing:

def setup_cockroach():
    for host in hostnames:
        api = suitable.Api(host)
        if not api.stat(path="/usr/local/bin/cockroach")['contacted'][host]['stat']['exists']:
            api.shell('wget -qO- https://binaries.cockroachdb.com/cockroach-v19.1.5.linux-amd64.tgz | tar  xvz')
            api.shell('cp -i cockroach-v19.1.5.linux-amd64/cockroach /usr/local/bin')
setup_cockroach()

But that loses the benefit of doing them all in parallel. With Ansible, you could set a register on the stat and check when: "{{register}}" to only run it on the hosts that you need to

@href
Copy link
Contributor

href commented Oct 23, 2019

You can still do it in parallel, just that you need to do it a bit differently, to get maximum parallelisation. With your approach you will indeed run serially through all hosts.

This is how I would approach it (note that this is completely untested code):

def setup_cockroach():
    # by default only 5 hosts are contacted in parallel ('serial' strategy),
    # use the 'free' strategy to run it parallel on *all* hosts
    api = suitable.Api(hostnames, strategy='free')

    # this is run in parallel on all hosts
    response = api.stat(path='/usr/local/bin/cockroach')

    # next, gather the subset of hosts to use as installation targets
    targets = []

    for host in hostnames:
        if not response.stat(host)['exists']:
            targets.append(host)

    # install in parallel on a subset of hosts
    api = suitable.Api(targets, strategy='free')
    api.shell('wget -qO- https://binaries.cockroachdb.com/cockroach-v19.1.5.linux-amd64.tgz | tar  xvz')
    api.shell('cp cockroach-v19.1.5.linux-amd64/cockroach /usr/local/bin')

You probably don't want to use cp -i btw, since that might result in an interactive prompt, which you cannot interact with.

If you install mitogen, you might also get some additional speed boost. See
https://suitable.readthedocs.io/en/latest/#mitogen-support

@jself
Copy link

jself commented Oct 23, 2019

That's awesome, thank you. Loving Suitable, with a Jupyter notebook it's pretty much exactly what I wanted for an interactive configuration management tool

@href
Copy link
Contributor

href commented Oct 23, 2019

Thanks 😊

@Daverball Daverball closed this as not planned Won't fix, can't repro, duplicate, stale Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants