jmunsch
jmunsch

Reputation: 24089

How to iterate a variable in fabric ssh?

For example:

global count
count += 1
@task
def install(hosts, local_swift_config):
    env.use_ssh_config = True
    env.hosts = set_hosts(hosts)
    execute(place_count)

def place_count():
    sudo('echo {} > /home/user/some_count'.format(count))
    count += 1

It doesn't have to be a global, what is the preferred way to do this with fabric?

Upvotes: 0

Views: 77

Answers (2)

jmunsch
jmunsch

Reputation: 24089

I decided not to use global:

def counter():
    env.count += 1
    if env.count == 2:
        env.count += 4

@task
def install(hosts):
    env.count = 0

    execute(counter)
    print(env.count)

    execute(counter)
    print(env.count)

    execute(counter)
    print(env.count)

Ouput:

1
6
7

Done.

Upvotes: 0

M. Urban
M. Urban

Reputation: 11

count = 0
@task
def install(hosts, local_swift_config):
    env.use_ssh_config = True
    env.hosts = set_hosts(hosts)
    execute(place_count)

def place_count():
    sudo('echo {} > /home/user/some_count'.format(count))
    global count
    count += 1

I've had this work for simple functions in fabric. Your problem is with python globals, not fabric.

See this thread for more information on globals: Stacokverflow Python Globals

Upvotes: 1

Related Questions