[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
@task(hosts=[…]) yields Context but -H yields Connection?
From: |
Daniel Middleton |
Subject: |
@task(hosts=[…]) yields Context but -H yields Connection? |
Date: |
Wed, 16 Sep 2020 15:56:10 +0100 |
Python 3.8.2, Fabric 2.5.0, Paramiko 2.7.2, Invoke 1.4.1
Hello,
I have a fabfile which needs to handle hosts passed at the command-line (using `-H`) and hosts defined in the fabfile if `-H` was not passed. Here's an example of the issue I'm facing:
```python
target_group = None
@task
def prod(ctx):
_env_handler(ctx, "prod")
def _env_handler(ctx, env_name):
global target_group
if not hasattr(ctx, 'host'):
target_group = Group("somehost1.tld", "somehost2.tld")
@task(hosts=target_group)
def test(ctx):
print(ctx)
```
If I run `fab prod test`:
```
<Context: <Config: {'run': {'asynch ...
```
If I run `fab -H 1,2 test`:
```
<Connection host=1>
<Connection host=2>
```
So, passing hosts using the `@task(hosts=[...]` decorator produces a ctx `Context` object, and using `-H` produces a ctx `Connection` object.
I know using a task (`prod(ctx)`) to wrap environment logic may be questionable...but is there a way to ensure the task (`test(ctx)`) always receives a `Connection` object...or am I fundamentally misunderstanding something?
Thanks.
Edit: I've also tried directly passing a host list `(e.g. @task(hosts=["somehost1.tld", "somehost2.tld"]))` with the same result.
- @task(hosts=[…]) yields Context but -H yields Connection?,
Daniel Middleton <=