Fix print_hostnames of inventory.py (#8554)

When trying to run print_hostnames of inventory.py, it outputs the following
error:

 $ CONFIG_FILE=./test-hosts.yaml python3 ./inventory.py print_hostnames
 Traceback (most recent call last):
   File "./inventory.py", line 472, in <module>
     sys.exit(main())
   File "./inventory.py", line 467, in main
     KubesprayInventory(argv, CONFIG_FILE)
   File "./inventory.py", line 92, in __init__
     self.parse_command(changed_hosts[0], changed_hosts[1:])
   File "./inventory.py", line 415, in parse_command
     self.print_hostnames()
   File "./inventory.py", line 455, in print_hostnames
     print(' '.join(self.yaml_config['all']['hosts'].keys()))
 KeyError: 'all'

because it is missed to load a hosts config file before printing hostnames.
This fixes the issue.
pull/8559/head
Kenichi Omichi 2022-02-17 13:57:03 -08:00 committed by GitHub
parent 97c667f67c
commit cc45e365ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 0 deletions

View File

@ -83,11 +83,15 @@ class KubesprayInventory(object):
self.config_file = config_file self.config_file = config_file
self.yaml_config = {} self.yaml_config = {}
loadPreviousConfig = False loadPreviousConfig = False
printHostnames = False
# See whether there are any commands to process # See whether there are any commands to process
if changed_hosts and changed_hosts[0] in AVAILABLE_COMMANDS: if changed_hosts and changed_hosts[0] in AVAILABLE_COMMANDS:
if changed_hosts[0] == "add": if changed_hosts[0] == "add":
loadPreviousConfig = True loadPreviousConfig = True
changed_hosts = changed_hosts[1:] changed_hosts = changed_hosts[1:]
elif changed_hosts[0] == "print_hostnames":
loadPreviousConfig = True
printHostnames = True
else: else:
self.parse_command(changed_hosts[0], changed_hosts[1:]) self.parse_command(changed_hosts[0], changed_hosts[1:])
sys.exit(0) sys.exit(0)
@ -105,6 +109,10 @@ class KubesprayInventory(object):
print(e) print(e)
sys.exit(1) sys.exit(1)
if printHostnames:
self.print_hostnames()
sys.exit(0)
self.ensure_required_groups(ROLES) self.ensure_required_groups(ROLES)
if changed_hosts: if changed_hosts: