From d2f8a189fc455d4417ff456f2320c53f9b247892 Mon Sep 17 00:00:00 2001 From: Rich Megginson Date: Jul 14 2021 22:22:10 +0000 Subject: [PATCH 1/3] add support for hostalias in TEST_SUBJECTS `TEST_SUBJECTS` and arguments on the command line can be specified like `/path/to/image.qcow2:hostalias`. --- diff --git a/README.md b/README.md index ad87a9f..ba5f6b0 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,82 @@ is missing. Let's look at an example: ansible_python_interpreter: "{% if vm_python_interpreter != '' %}{{ vm_python_interpreter }}{% else %}/usr/bin/python2{% endif %}" ``` +## Running with `ansible-navigator` + +Ansible has a new way to run playbooks, using `ansible-navigator` and execution +environments (containers). You will have to use the `LOCK_ON_FILE` method above +to start the VMs and create the inventory. There is a new feature required when +using `standard-inventory-qcow2`. +* `TEST_SUBJECTS` and arguments on the command line can be specified like + `/path/to/image.qcow2:hostalias`. + +`ansible-navigator` expects that the hostname looks like a regular IP address or +hostname, not a path. Adding `:hostalias` to the image path allows you to +specify the hostname alias for that image path. + +Example: +``` +cd /path/to/role/tests +touch running-test +LOCK_ON_FILE=`pwd`/running-test /usr/share/ansible/inventory/standard-inventory-qcow2 ~/.cache/libvirt/centos-8.qcow2:sut +``` +This will create the inventory in `/tmp/inventory_xxxx/inventory` like this: +```yaml +all: + children: + localhost: + hosts: &id001 + sut: + ansible_host: 127.0.0.3 + ansible_port: '5041' + ansible_python_interpreter: /usr/libexec/platform-python + ansible_ssh_common_args: -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no + ansible_ssh_pass: foobar + ansible_ssh_private_key_file: /tmp/inventory-xxxx/identity + ansible_user: root + subjects: + hosts: *id001 +``` +The hostname reported by Ansible will be `sut` and the JSON output of the script will look like this: +```json +{ + "localhost": { + "hosts": [ + "sut" + ], + "vars": {} + }, + "subjects": { + "hosts": [ + "sut" + ], + "vars": {} + }, + "_meta": { + "hostvars": { + "sut": { + "ansible_port": "5041", + "ansible_host": "127.0.0.3", + "ansible_user": "root", + "ansible_ssh_pass": "foobar", + "ansible_ssh_private_key_file": "/tmp/inventory-xxxx/identity", + "ansible_ssh_common_args": "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no", + "ansible_python_interpreter": "/usr/libexec/platform-python" + } + } + } +} +``` +Then you can run `ansible-navigator` like this: +``` +ansible-navigator run tests_default.yml --ee true \ + --eei quay.io/ansible/ansible-runner:stable-2.11-devel \ + --container-option=--net=host -i /tmp/inventory-xxxx/inventory +``` +NOTE: `--container-option=--net=host` is required because otherwise the +container will attempt to reach `127.0.0.3` inside the container rather than the +VM running on the host. If your `ansible-navigator` does not support +`--container-option` see https://github.com/ansible/ansible-navigator/pull/511 [1]: https://fedoraproject.org/wiki/CI/Metadata [2]: http://fmf.readthedocs.io/ diff --git a/inventory/standard-inventory-qcow2 b/inventory/standard-inventory-qcow2 index f401539..ab50d26 100755 --- a/inventory/standard-inventory-qcow2 +++ b/inventory/standard-inventory-qcow2 @@ -206,14 +206,26 @@ def get_artifact_path(path=""): return os.path.join(artifacts, path) + +def split_image_hostalias(image_hostalias): + ary = image_hostalias.split(":") + image = ary[0] + if len(ary) > 1: + hostalias = ary[1] + else: + hostalias = image + return (image, hostalias) + + def inv_list(subjects): hosts = [] variables = {} for subject in subjects: + image, hostalias = split_image_hostalias(subject) host_vars = inv_host(subject) if host_vars: - hosts.append(subject) - variables[subject] = host_vars + hosts.append(hostalias) + variables[hostalias] = host_vars if not hosts: return EMPTY_INVENTORY return {"localhost": {"hosts": hosts, "vars": {}}, @@ -447,7 +459,8 @@ def start_qemu(image, cloudinit, portrange=(2222, 5555)): return qemu_proc, port, log_guest -def inv_host(image): +def inv_host(image_hostalias): + image, hostalias = split_image_hostalias(image_hostalias) if not image.endswith((".qcow2", ".qcow2c")): logger.info("Return empty inventory for image: %s.", image) return EMPTY_INVENTORY @@ -502,7 +515,7 @@ def inv_host(image): } # Write out a handy inventory file, for our use and for debugging inventory = os.path.join(directory, "inventory") - write_debug_inventory(inventory, {image: variables}) + write_debug_inventory(inventory, {hostalias: variables}) # Wait for ssh to come up ping = [ ansible_bin, @@ -564,7 +577,7 @@ def inv_host(image): return None variables["ansible_python_interpreter"] = ansible_python_interpreter # Update inventory file - write_debug_inventory(inventory, {image: variables}) + write_debug_inventory(inventory, {hostalias: variables}) return variables # Daemonize and watch the processes os.chdir("/") From 808202e79c5f6d2a43d3fc55ced8578aacd3baba Mon Sep 17 00:00:00 2001 From: Rich Megginson Date: Jul 14 2021 22:33:46 +0000 Subject: [PATCH 2/3] container-options --- diff --git a/README.md b/README.md index ba5f6b0..c01b174 100644 --- a/README.md +++ b/README.md @@ -215,12 +215,12 @@ Then you can run `ansible-navigator` like this: ``` ansible-navigator run tests_default.yml --ee true \ --eei quay.io/ansible/ansible-runner:stable-2.11-devel \ - --container-option=--net=host -i /tmp/inventory-xxxx/inventory + --container-options=--net=host -i /tmp/inventory-xxxx/inventory ``` -NOTE: `--container-option=--net=host` is required because otherwise the +NOTE: `--container-options=--net=host` is required because otherwise the container will attempt to reach `127.0.0.3` inside the container rather than the VM running on the host. If your `ansible-navigator` does not support -`--container-option` see https://github.com/ansible/ansible-navigator/pull/511 +`--container-options` see https://github.com/ansible/ansible-navigator/pull/511 [1]: https://fedoraproject.org/wiki/CI/Metadata [2]: http://fmf.readthedocs.io/ From f92ed06fbfad585a481ed924d48455b127e6e7cc Mon Sep 17 00:00:00 2001 From: Rich Megginson Date: Jul 15 2021 14:05:01 +0000 Subject: [PATCH 3/3] fixed --- diff --git a/README.md b/README.md index c01b174..d633ee4 100644 --- a/README.md +++ b/README.md @@ -150,9 +150,8 @@ is missing. Let's look at an example: Ansible has a new way to run playbooks, using `ansible-navigator` and execution environments (containers). You will have to use the `LOCK_ON_FILE` method above to start the VMs and create the inventory. There is a new feature required when -using `standard-inventory-qcow2`. -* `TEST_SUBJECTS` and arguments on the command line can be specified like - `/path/to/image.qcow2:hostalias`. +using `standard-inventory-qcow2` - `TEST_SUBJECTS` and arguments on the command +line can be specified like `/path/to/image.qcow2:hostalias`. `ansible-navigator` expects that the hostname looks like a regular IP address or hostname, not a path. Adding `:hostalias` to the image path allows you to