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

fix(anta.cli): Use configured anta inventory as output by default in get from-ansible #468

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions anta/cli/get/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,26 @@ def from_cvp(inventory_directory: str, cvp_ip: str, cvp_username: str, cvp_passw
@click.option("--ansible-group", "-g", help="Ansible group to filter", type=str, required=False, default="all")
@click.option(
"--ansible-inventory",
"-i",
"-ai",
default=None,
help="Path to your ansible inventory file to read",
type=click.Path(file_okay=True, dir_okay=False, exists=True, path_type=Path),
)
@click.option(
"--output",
"-o",
default="inventory-ansible.yml",
help="Path to save inventory file",
required=False,
help="Path to save inventory file. If not configured, use anta inventory file",
type=click.Path(file_okay=True, dir_okay=False, exists=False, writable=True, path_type=Path),
)
def from_ansible(ctx: click.Context, output: Path, ansible_inventory: Path, ansible_group: str) -> None:
"""Build ANTA inventory from an ansible inventory YAML file"""
logger.info(f"Building inventory from ansible file {ansible_inventory}")

# Create output directory
output = output if output is not None else ctx.obj["inventory_path"]
output.parent.mkdir(parents=True, exist_ok=True)
logger.info(f"output anta inventory is: {output}")
try:
create_inventory_from_ansible(
inventory=ansible_inventory,
Expand Down
13 changes: 6 additions & 7 deletions docs/cli/inv-from-ansible.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ Usage: anta get from-ansible [OPTIONS]
Build ANTA inventory from an ansible inventory YAML file

Options:
-g, --ansible-group TEXT Ansible group to filter
-i, --ansible-inventory FILENAME
Path to your ansible inventory file to read
-o, --output FILENAME Path to save inventory file
-d, --inventory-directory PATH Directory to save inventory file
--help Show this message and exit.
-g, --ansible-group TEXT Ansible group to filter
-ai, --ansible-inventory FILE Path to your ansible inventory file to read
-o, --output FILE Path to save inventory file. If not
configured, use anta inventory file
--help Show this message and exit.
```

The output is an inventory where the name of the container is added as a tag for each host:
The output is an inventory where the name of the container is added as a tag for each host. By default, anta cli saves output in anta inventory configured under `anta --inventory` and can be overwritten with `--output` option:

```yaml
anta_inventory:
Expand Down
18 changes: 8 additions & 10 deletions tests/data/test_inventory.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
---
anta_inventory:
hosts:
- name: dummy
host: dummy.anta.ninja
tags: ["leaf"]
- name: dummy2
host: dummy2.anta.ninja
tags: ["leaf"]
- name: dummy3
host: dummy3.anta.ninja
tags: ["spine"]
- host: 192.168.0.12
name: leaf1
- host: 192.168.0.13
name: leaf2
- host: 192.168.0.14
name: leaf3
- host: 192.168.0.15
name: leaf4
7 changes: 4 additions & 3 deletions tests/units/cli/get/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,13 @@ def mock_cvp_connect(self: CvpClient, *args: str, **kwargs: str) -> None:
"ansible_inventory, ansible_group, output, expected_exit",
[
pytest.param("ansible_inventory.yml", None, None, 0, id="no group"),
pytest.param("ansible_inventory.yml", None, "inventory.yml", 0, id="output defined"),
pytest.param("ansible_inventory.yml", "ATD_LEAFS", None, 0, id="group found"),
pytest.param("ansible_inventory.yml", "DUMMY", None, 4, id="group not found"),
pytest.param("empty_ansible_inventory.yml", None, None, 4, id="empty inventory"),
],
)
# pylint: disable-next=too-many-arguments
# pylint: disable=too-many-arguments
def test_from_ansible(
tmp_path: Path,
caplog: LogCaptureFixture,
Expand All @@ -132,7 +133,7 @@ def test_from_ansible(
else:
# Get inventory-directory default
default_dir: Path = cast(Path, from_ansible.params[2].default)
out_dir = Path() / default_dir
out_dir = Path() / default_dir if default_dir is not None else Path()

if ansible_inventory is not None:
ansible_inventory_path = DATA_DIR / ansible_inventory
Expand All @@ -150,6 +151,6 @@ def test_from_ansible(
assert result.exit_code == expected_exit
print(caplog.records)
if expected_exit != 0:
assert len(caplog.records) == 2
assert len(caplog.records) in {2, 3}
else:
assert out_dir.exists()
Loading