0

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?

jmunsch
  • 22,771
  • 11
  • 93
  • 114

2 Answers2

1
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

Community
  • 1
  • 1
M. Urban
  • 11
  • 1
  • Thanks, I decided to not use `global`, and went with using the `env` variable instead. Thanks for the heads up. I hadn't ever used a global before, and was at the point where I would have used whatever worked at the cost of good practice. – jmunsch Dec 16 '16 at 19:38
0

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.
jmunsch
  • 22,771
  • 11
  • 93
  • 114