# Buggy code
def _get_hosts_from_group(group):
    inventory = __context__["inventory"]
    hosts = [host for host in inventory[group].get("hosts", [])]
    for child in inventory[group].get("children", []):
        if child != "ungrouped":
            hosts.extend(_get_hosts_from_group(child))
        hosts.extend(_get_hosts_from_group(child))
    return hosts


# Traceback (KeyError)
username@do-ansible:~$ salt-mk \* test.ping
[ERROR   ] An un-handled exception was caught by salt's global exception handler:
KeyError: 'ungrouped'
Traceback (most recent call last):
File "/usr/bin/salt-ssh", line 11, in <module>
load_entry_point('salt==3001', 'console_scripts', 'salt-ssh')()
File "/usr/lib/python3/dist-packages/salt/scripts.py", line 499, in salt_ssh
client.run()
File "/usr/lib/python3/dist-packages/salt/cli/ssh.py", line 25, in run
ssh = salt.client.ssh.SSH(self.config)
File "/usr/lib/python3/dist-packages/salt/client/ssh/__init__.py", line 243, in __init__
self.targets = self.roster.targets(self.opts["tgt"], self.tgt_type)
File "/usr/lib/python3/dist-packages/salt/roster/__init__.py", line 105, in targets
targets.update(self.rosters[f_str](tgt, tgt_type))
File "/usr/lib/python3/dist-packages/salt/roster/ansible.py", line 133, in targets
host for host in _get_hosts_from_group("all") if fnmatch.fnmatch(host, tgt)
File "/usr/lib/python3/dist-packages/salt/roster/ansible.py", line 146, in _get_hosts_from_group
hosts.extend(_get_hosts_from_group(child))
File "/usr/lib/python3/dist-packages/salt/roster/ansible.py", line 142, in _get_hosts_from_group
hosts = [host for host in inventory[group].get("hosts", [])]
KeyError: 'ungrouped'
Traceback (most recent call last):
File "/usr/bin/salt-ssh", line 11, in <module>
load_entry_point('salt==3001', 'console_scripts', 'salt-ssh')()
File "/usr/lib/python3/dist-packages/salt/scripts.py", line 499, in salt_ssh
client.run()
File "/usr/lib/python3/dist-packages/salt/cli/ssh.py", line 25, in run
ssh = salt.client.ssh.SSH(self.config)
File "/usr/lib/python3/dist-packages/salt/client/ssh/__init__.py", line 243, in __init__
self.targets = self.roster.targets(self.opts["tgt"], self.tgt_type)
File "/usr/lib/python3/dist-packages/salt/roster/__init__.py", line 105, in targets
targets.update(self.rosters[f_str](tgt, tgt_type))
File "/usr/lib/python3/dist-packages/salt/roster/ansible.py", line 133, in targets
host for host in _get_hosts_from_group("all") if fnmatch.fnmatch(host, tgt)
File "/usr/lib/python3/dist-packages/salt/roster/ansible.py", line 146, in _get_hosts_from_group
hosts.extend(_get_hosts_from_group(child))
File "/usr/lib/python3/dist-packages/salt/roster/ansible.py", line 142, in _get_hosts_from_group
hosts = [host for host in inventory[group].get("hosts", [])]
KeyError: 'ungrouped'
username@do-ansible:~$ sudo sed -i '146 s/^\([ ]*\)/\1#/g' /usr/lib/python3/dist-packages/salt/roster/ansible.py
username@do-ansible:~$ salt-mk \* test.ping
do-ansible:
True
do-test:
True
username@do-ansible:~$ dpkg -l|grep salt
ii  salt-common                          3001+ds-1                         all          shared libraries that salt requires for all packages
ii  salt-ssh                             3001+ds-1                         all          remote manager to administer servers via Salt SSH
username@do-ansible:~$ grep -i pret /etc/os-release
PRETTY_NAME="Ubuntu 20.04 LTS"

# Fixed code
def _get_hosts_from_group(group):
    inventory = __context__["inventory"]
    hosts = [host for host in inventory[group].get("hosts", [])]
    for child in inventory[group].get("children", []):
        if child != "ungrouped":
            hosts.extend(_get_hosts_from_group(child))
    return hosts