Skip to content

Commit e7bdc9b

Browse files
committed
feat: skip selected hosts with new _meta option
1 parent 5a30a5f commit e7bdc9b

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

examples/hosts.yml

+13-1
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,27 @@ group1: # host_group
77
_auth_type: IdentityAgent
88
_auth_path: ~/.1password/agent.sock
99
_aliases: [ "base", "another-alias" ]
10+
_skip: true
1011

1112
host2:
1213
ansible_host: 127.0.0.2
1314
ansible_user: manager
1415
ansible_password:
16+
_meta:
17+
_skip: true
1518

1619
group2:
1720
hosts:
18-
another_host1:
21+
host3:
1922
ansible_host: 172.19.0.1
2023
ansible_user: postgres
2124
ansible_password:
25+
26+
host4:
27+
ansible_host: 172.99.99.99
28+
ansible_user: postgres
29+
_meta:
30+
_auth_type: IdentityAgent
31+
_auth_path: ~/.1password/agent.sock
32+
_aliases: [ "base", "another-alias" ]
33+
_skip: false

sshgen/generators/model.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ def convert(parsed_hosts: CommentedMap) -> list[HostModel]:
2323
auth_type = meta.get('_auth_type')
2424
auth_path = meta.get('_auth_path')
2525
aliases = meta.get('_aliases')
26+
skip = meta.get('_skip')
2627

27-
if any(isinstance(field, str | list) for field in (auth_type, auth_path, aliases)):
28-
model.meta_fields = MetaFieldsModel(auth_type, auth_path, aliases)
28+
if any(isinstance(field, str | list | bool) for field in (auth_type, auth_path, aliases, skip)):
29+
model.meta_fields = MetaFieldsModel(auth_type, auth_path, aliases, skip)
2930

3031
host_models.append(model)
3132

sshgen/generators/sshconfig.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python
2+
import copy
23
from pathlib import Path
34

45
from sshgen.logger import init_logger
@@ -10,15 +11,20 @@
1011

1112
class SSHConfig:
1213
def __init__(self, models: list[HostModel], output_file: Path | None = None):
13-
self.models = models
14+
self.raw_models = models
1415
self.template_path = FileUtils.as_package_file('templates/ssh_config.template')
1516
self.ssh_template = self._open_template()
1617
self.output_file = output_file
1718

1819
def generate(self) -> None:
19-
configs: list[str] = [self._process(model) for model in self.models]
20+
filtered_models: list[HostModel] = self._filter_models()
21+
configs: list[str] = [self._process(m) for m in filtered_models]
2022
self._save_config(configs)
2123

24+
def _filter_models(self) -> list[HostModel]:
25+
temp_models = copy.deepcopy(self.raw_models)
26+
return [model for model in temp_models if not (model.meta_fields and model.meta_fields.skip)]
27+
2228
def _process(self, model: HostModel) -> str:
2329
temp = self.ssh_template.replace('{{ host }}', model.host) \
2430
.replace('{{ hostname }}', model.ansible_host) \

sshgen/models/metafields.py

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ class MetaFieldsModel:
77
auth_type: str | None = None
88
auth_path: str | None = None
99
aliases: list[str] | None = None
10+
skip: bool | None = None

0 commit comments

Comments
 (0)