#211 fix standard-inventory-rpm
Merged 5 years ago by astepano. Opened 5 years ago by bgoncalv.
bgoncalv/standard-test-roles fix-subject-standard-inventory-rpm  into  master

@@ -38,7 +38,7 @@ 

      parser = argparse.ArgumentParser(description="Inventory for local RPM installed host")

      parser.add_argument("--list", action="store_true", help="Verbose output")

      parser.add_argument('--host', help="Get host variables")

-     parser.add_argument("subjects", nargs="*", default=shlex.split(os.environ.get("TEST_SUBJECTS", "")))

+     parser.add_argument("subjects", nargs="*", default=os.environ.get("TEST_SUBJECTS", ""))

      opts = parser.parse_args()

  

      try:
@@ -56,22 +56,13 @@ 

  

  

  def getlist(subjects):

-     repos = []

-     rpms = []

      hosts = []

      variables = {}

  

-     for subject in subjects:

-         if subject.endswith(".rpm"):

-             rpms.append(subject)

-         elif isrepo(subject):

-             repos.append(subject)

- 

-     if repos or rpms:

-         host_vars = gethost(repos, rpms)

-         if host_vars:

-             hosts.append("rpms")

-             variables["rpms"] = vars

+     host_vars = gethost(subjects)

+     if host_vars:

+         hosts.append("rpms")

+         variables["rpms"] = host_vars

  

      if not hosts:

          return EMPTY_INVENTORY
@@ -80,7 +71,20 @@ 

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

  

  

- def gethost(repos, rpms):

+ def gethost(subjects):

+     subjects = shlex.split(subjects)

+     repos = []

+     rpms = []

+ 

+     for subject in subjects:

+         if subject.endswith(".rpm"):

+             rpms.append(subject)

+         elif isrepo(subject):

+             repos.append(subject)

+ 

+     if not repos and not rpms:

+         return EMPTY_INVENTORY

+ 

      # The variables

      variables = {

          "ansible_connection": "local"

Solve dynamic inventory output:

ANSIBLE_INVENTORY=$(test -e inventory && echo inventory || echo /usr/share/ansible/inventory) TEST_SUBJECTS=local ansible-inventory --list

[WARNING]: * Failed to parse /usr/share/ansible/inventory/standard-inventory-rpm with script plugin: Invalid data from file, expected dictionary and got: None
[WARNING]: * Failed to parse /usr/share/ansible/inventory/standard-inventory-rpm with yaml plugin: Syntax Error while loading YAML. mapping values are not allowed in this context The error appears to have
been in '/usr/share/ansible/inventory/standard-inventory-rpm': line 37, column 15, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: def main(argv):
^ here
[WARNING]: * Failed to parse /usr/share/ansible/inventory/standard-inventory-rpm with ini plugin: /usr/share/ansible/inventory/standard-inventory-rpm:27: Expected key=value host variable assignment, got:
argparse
[WARNING]: Unable to parse /usr/share/ansible/inventory/standard-inventory-rpm as an inventory source

Maybe rewrite as:
else:
return EMPTY_INVETORY

?

rebased onto a28913d84cc51e82be32ec8431b52d1e2b9ebb87

5 years ago

thanks for the suggestion, I've updated it.

Hmm, how can I specify more rpms? This does not work:

[root@qeos-41 tests]# ansible-playbook -e subjects="file-5.31-11.fc27.x86_64.rpm file-5.31-12.fc27.x86_64.rpm" tests.yml

@bgoncalv I cannot reproduce the problem on master, do you have a reproducer @bgoncalv ?

rebased onto fd1ae938dae08786dbbcae3ff50204b86d766a79

5 years ago

@bgoncalv I cannot reproduce the problem on master, do you have a reproducer @bgoncalv ?

I reproduce it running: ANSIBLE_INVENTORY=$(test -e inventory && echo inventory || echo /usr/share/ansible/inventory) TEST_SUBJECTS=local ansible-inventory --list

with standard-test-roles-2.13-1.fc29.noarch and ansible-2.5.3-1.fc29.noarch.rpm.

rebased onto 35031bc4cc43fe0585e1389fd0c922d3197e9084

5 years ago

It is not clear what is TEST_SUBJECTS.

  1. package name for yum command
  2. package path for yum command

If we want to use package name than we use:

yum install zsh-5.0.2-28.el7

if we want to use package path then we use path in install command:

yum install /uri/or/path/to/file/zsh-5.0.2-28.el7.x86_64.rpm

It seems that this inventory script supports ONLY path/uri to .rpm file.

This PR makes a lot of copies hosts "rpms":

# TEST_SUBJECTS="zsh-5.0.2-28.el7.x86_64.rpm bash-5.0.2-28.el7.x86_64.rpm" ./standard-inventory-rpm --list
...
{
    "subjects": {
        "hosts": [
            "rpms",
            "rpms"
        ],
        "vars": {}
    },
    "_meta": {
        "hostvars": {
            "rpms": {
                "ansible_connection": "local"
            }
        }
    },
    "localhost": {
        "hosts": [
            "rpms",
            "rpms"
        ],
        "vars": {}
    }
}#                                 

rebased onto 26ab4c0

5 years ago

Thanks I've updated the PR to fix the multiple copies of "rpms".

Note the script never accepted 'package name for yum command'. It excepted hte file to end with .rpm.
Sure, if you want the script can be updated to support it.

@bgoncalv nice, I like this approach.
ACK to merge it.
If we want to support 'package name for yum command' than it should be done in different PR.
Thank you.

Commit f920351 fixes this pull-request

Pull-Request has been merged by astepano

5 years ago

Pull-Request has been merged by astepano

5 years ago