Nosmoht / ansible-module-foreman

Ansible module to manage configuration of Foreman
49 stars 32 forks source link

foreman_os_default_template: "AttributeError: 'list' object has no attribute 'get'" #27

Closed stintel closed 2 years ago

stintel commented 8 years ago

The foreman_os_default_template module doesn't seem to work.

Example playbook:


---
- name: Configure Foreman master via API
  hosts: localhost
  connection: local
  tasks:
    - name: add operating system default templates
      foreman_os_default_template:
        operatingsystem: Ubuntu
        config_template: Preseed default
        template_kind: provision
        state: present
        foreman_host: "{{ foreman_host }}"
        foreman_port: "{{ foreman_port }}"
        foreman_user: "{{ foreman_user }}"
        foreman_pass: "{{ foreman_pass }}"

Results in this error:

An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/home/stijn/.ansible/tmp/ansible-tmp-1461933187.79-89398459364617/foreman_os_default_template", line 2115, in <module>
    main()
  File "/home/stijn/.ansible/tmp/ansible-tmp-1461933187.79-89398459364617/foreman_os_default_template", line 175, in main
    changed, os_default_template = ensure()
  File "/home/stijn/.ansible/tmp/ansible-tmp-1461933187.79-89398459364617/foreman_os_default_template", line 112, in ensure
    os_default_templates = theforeman.get_operatingsystem_default_templates(id=os.get('id'))
AttributeError: 'list' object has no attribute 'get'

fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "invocation": {"module_name": "foreman_os_default_template"}, "parsed": false}

This happens because I have >1 Ubuntu versions in Foreman. In this case, search_operatingsystem returns a list. Removing either of them solves the issue.

>>> os = theforeman.search_operatingsystem(data=dict(name='Ubuntu'))
>>> print os
[{u'major': u'14', u'description': None, u'family': u'Debian', u'title': u'Ubuntu 14.04', u'created_at': u'2016-04-28 15:11:48 UTC', u'updated_at': u'2016-04-29 10:00:34 UTC', u'name': u'Ubuntu', u'release_name': u'trusty', u'id': 11, u'minor': u'04', u'password_hash': u'SHA256'}, {u'major': u'16', u'description': None, u'family': u'Debian', u'title': u'Ubuntu 16.04', u'created_at': u'2016-04-28 15:11:48 UTC', u'updated_at': u'2016-04-29 10:00:34 UTC', u'name': u'Ubuntu', u'release_name': u'xenial', u'id': 12, u'minor': u'04', u'password_hash': u'SHA256'}]
>>> type(os)
<type 'list'>

vs

>>> os = theforeman.search_operatingsystem(data=dict(name='Ubuntu'))
>>> print os
{u'major': u'16', u'description': None, u'family': u'Debian', u'title': u'Ubuntu 16.04', u'created_at': u'2016-04-28 15:11:48 UTC', u'updated_at': u'2016-04-29 10:00:34 UTC', u'name': u'Ubuntu', u'release_name': u'xenial', u'id': 12, u'minor': u'04', u'password_hash': u'SHA256'}
>>> type(os)
<type 'dict'>

Reporting it here as I'm not sure how to fix this yet. Maybe someone else has a great idea.

stintel commented 8 years ago

I see a few options:

Not sure what I prefer yet...

Nosmoht commented 8 years ago

Setting the template for all would be a bad idea. So we should find the right one (if possible) or reject at all.

Darwiner commented 5 years ago

I'm getting the same issue with foreman_host.py when trying to specify "operatingsystem".

As I have multiple "CentOS" versions defined, it seems to be giving me the same error...

The full traceback is:
Traceback (most recent call last):
  File "/home/mnguyen/.ansible/tmp/ansible-tmp-1567542570.99-20348233523044/AnsiballZ_foreman_host.py", line 114, in <module>
    _ansiballz_main()
  File "/home/mnguyen/.ansible/tmp/ansible-tmp-1567542570.99-20348233523044/AnsiballZ_foreman_host.py", line 106, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/home/mnguyen/.ansible/tmp/ansible-tmp-1567542570.99-20348233523044/AnsiballZ_foreman_host.py", line 49, in invoke_module
    imp.load_module('__main__', mod, module, MOD_DESC)
  File "/tmp/ansible_foreman_host_payload_6TJWMs/__main__.py", line 742, in <module>
  File "/tmp/ansible_foreman_host_payload_6TJWMs/__main__.py", line 735, in main
  File "/tmp/ansible_foreman_host_payload_6TJWMs/__main__.py", line 406, in ensure
AttributeError: 'list' object has no attribute 'get'

fatal: [localhost]: FAILED! => {
    "changed": false,
    "module_stderr": "Traceback (most recent call last):\n  File \"/home/mnguyen/.ansible/tmp/ansible-tmp-1567542570.99-20348233523044/AnsiballZ_foreman_host.py\", line 114, in <module>\n    _ansiballz_main()\n  File \"/home/mnguyen/.ansible/tmp/ansible-tmp-1567542570.99-20348233523044/AnsiballZ_foreman_host.py\", line 106, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/mnguyen/.ansible/tmp/ansible-tmp-1567542570.99-20348233523044/AnsiballZ_foreman_host.py\", line 49, in invoke_module\n    imp.load_module('__main__', mod, module, MOD_DESC)\n  File \"/tmp/ansible_foreman_host_payload_6TJWMs/__main__.py\", line 742, in <module>\n  File \"/tmp/ansible_foreman_host_payload_6TJWMs/__main__.py\", line 735, in main\n  File \"/tmp/ansible_foreman_host_payload_6TJWMs/__main__.py\", line 406, in ensure\nAttributeError: 'list' object has no attribute 'get'\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 1
}

Any chance of implementing the same fix in there also?