#207 avoid trying to bring up wrong inventory when processing dynamic inventory
Merged 5 years ago by astepano. Opened 5 years ago by bgoncalv.
bgoncalv/standard-test-roles fix-check-subjects  into  master

@@ -66,12 +66,10 @@ 

      hosts = []

      variables = {}

      for subject in subjects:

-         if subject.startswith("docker:"):

-             image = subject[7:]

-             name, host_vars = inv_host(image, docker_extra_args)

-             if host_vars:

-                 hosts.append(name)

-                 variables[name] = host_vars

+         name, host_vars = inv_host(subject, docker_extra_args)

+         if host_vars:

+             hosts.append(name)

+             variables[name] = host_vars

      if not hosts:

          return EMPTY_INVENTORY

      return {"localhost": {"hosts": hosts, "vars": {}},
@@ -79,7 +77,11 @@ 

              "_meta": {"hostvars": variables}}

  

  

- def inv_host(image, docker_extra_args):

+ def inv_host(subject, docker_extra_args):

+     if not subject.startswith("docker:"):

+         return None, EMPTY_INVENTORY

+ 

+     image = subject[7:]

      null = open(os.devnull, 'w')

  

      try:

@@ -99,11 +99,10 @@ 

      hosts = []

      variables = {}

      for subject in subjects:

-         if subject.endswith((".qcow2", ".qcow2c")):

-             host_vars = inv_host(subject)

-             if host_vars:

-                 hosts.append(subject)

-                 variables[subject] = host_vars

+         host_vars = inv_host(subject)

+         if host_vars:

+             hosts.append(subject)

+             variables[subject] = host_vars

      if not hosts:

          return EMPTY_INVENTORY

      return {"localhost": {"hosts": hosts, "vars": {}},
@@ -155,6 +154,9 @@ 

  

  

  def inv_host(image):

+     if not image.endswith((".qcow2", ".qcow2c")):

+         return EMPTY_INVENTORY

+ 

      null = open(os.devnull, 'w')

  

      try:

Fix messages like:
Launching virtual machine for a33bc36b3d2133714cffba130a08db0433f71249029fe148cbe0056230f32d46

# ANSIBLE_INVENTORY=$(test -e inventory && echo inventory || echo /usr/share/ansible/inventory) TEST_SUBJECTS=docker:docker.io/fedora:rawhide ansible-inventory --list
Launching Docker container for docker.io/fedora:rawhide
Redirecting to /bin/systemctl start docker.service
a33bc36b3d2133714cffba130a08db0433f71249029fe148cbe0056230f32d46
[ERROR]:

Launching virtual machine for a33bc36b3d2133714cffba130a08db0433f71249029fe148cbe0056230f32d46
standard-inventory-qcow2: qemu failed to launch qcow2 image: a33bc36b3d2133714cffba130a08db0433f71249029fe148cbe0056230f32d46
{
    "_meta": {
        "hostvars": {
            "a33bc36b3d2133714cffba130a08db0433f71249029fe148cbe0056230f32d46": {
                "ansible_connection": "docker", 
                "ansible_python_interpreter": "/usr/bin/python3"
            }
        }
    }, 
    "all": {
        "children": [
            "localhost", 
            "subjects", 
            "ungrouped"
        ]
    }, 
    "localhost": {
        "hosts": [
            "a33bc36b3d2133714cffba130a08db0433f71249029fe148cbe0056230f32d46"
        ]
    }, 
    "subjects": {
        "hosts": [
            "a33bc36b3d2133714cffba130a08db0433f71249029fe148cbe0056230f32d46"
        ]
    }, 
    "ungrouped": {}
}
# ANSIBLE_INVENTORY=$(test -e inventory && echo inventory || echo /usr/share/ansible/inventory) TEST_SUBJECTS=local ansible-inventory --list
[ERROR]:

Launching virtual machine for local
standard-inventory-qcow2: qemu failed to launch qcow2 image: local
{
    "_meta": {
        "hostvars": {
            "local": {
                "ansible_connection": "local"
            }
        }
    }, 
    "all": {
        "children": [
            "localhost", 
            "subjects", 
            "ungrouped"
        ]
    }, 
    "localhost": {
        "hosts": [
            "local"
        ]
    }, 
    "subjects": {
        "hosts": [
            "local"
        ]
    }, 
    "ungrouped": {}
}

rebased onto 183e5c0

5 years ago

Changes are about moving chk code to more common place.
Seems good, thank you.

@bgoncalv standard-inventory-local doesn't have this issue, right?

@astepano no, it already handles it:

if host == "local":
    return {"ansible_connection": "local"}
return EMPTY_INVENTORY

Commit 8bc781b fixes this pull-request

Pull-Request has been merged by astepano

5 years ago

Pull-Request has been merged by astepano

5 years ago