Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ansible 2.8: Regression because of new sentinel class representing None or [] #46

Closed
dmsimard opened this issue May 22, 2019 · 1 comment
Labels

Comments

@dmsimard
Copy link
Contributor

Example trace from ara 1.0:

[WARNING]: Failure using method (v2_playbook_on_task_start) in callback plugin (<ansible.plugins.callback./home/dmsimard/dev/git/ansible-community/ara/ara/plugins/callback/ara_default.CallbackModule object at 0x7fb58bb36a90>): Object of type type is not
JSON serializable

Callback Exception: 
  File "/home/dmsimard/dev/git/ansible-community/ara/.tox/ansible-integration/lib/python3.7/site-packages/ansible/executor/task_queue_manager.py", line 333, in send_callback
    method(*new_args, **kwargs)
   File "/home/dmsimard/dev/git/ansible-community/ara/ara/plugins/callback/ara_default.py", line 232, in v2_playbook_on_task_start
    handler=handler,
   File "/home/dmsimard/dev/git/ansible-community/ara/ara/clients/http.py", line 104, in post
    return self._request("post", endpoint, **kwargs)
   File "/home/dmsimard/dev/git/ansible-community/ara/ara/clients/http.py", line 82, in _request
    response = func(url, **kwargs)
   File "/home/dmsimard/dev/git/ansible-community/ara/ara/clients/http.py", line 63, in post
    return self._request("post", url, data=json.dumps(payload))
   File "/usr/lib64/python3.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
   File "/usr/lib64/python3.7/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
   File "/usr/lib64/python3.7/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
   File "/usr/lib64/python3.7/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '

Or screenshot from 0.x:
Screenshot

This is because the structure of the task object has changed.

In 2.7:

(Pdb) task._attributes
{'args': {'name': '{{ item }}', 'ansible_host': '127.0.0.1', 'ansible_connection': 'local'}, 'action': 'add_host', 'async_val': 0, 'changed_when': [], 'delay': 5, 'delegate_to': None, 'delegate_facts': None, 'failed_when': [], 'loop': ['host1', 'host2', 'host3'], 'loop_control': None, 'notify': None, 'poll': 10, 'register': None, 'retries': 3, 'until': [], 'loop_with': 'items', 'name': 'Add fake hosts in inventory', 'connection': None, 'port': None, 'remote_user': None, 'vars': {}, 'module_defaults': None, 'environment': None, 'no_log': None, 'run_once': None, 'ignore_errors': None, 'ignore_unreachable': None, 'check_mode': None, 'diff': None, 'any_errors_fatal': None, 'debugger': None, 'when': [], 'tags': [], 'become': None, 'become_method': None, 'become_user': None, 'become_flags': None}
(Pdb) task._attributes["tags"]
[]

In 2.8 (notice all the ansible.utils.sentinel.Sentinel):

(Pdb) task._attributes
{'args': {'name': '{{ item }}', 'ansible_host': '127.0.0.1', 'ansible_connection': 'local'}, 'action': 'add_host', 'async_val': <class 'ansible.utils.sentinel.Sentinel'>, 'changed_when': <class 'ansible.utils.sentinel.Sentinel'>, 'delay': <class 'ansible.utils.sentinel.Sentinel'>, 'delegate_to': <class 'ansible.utils.sentinel.Sentinel'>, 'delegate_facts': <class 'ansible.utils.sentinel.Sentinel'>, 'failed_when': <class 'ansible.utils.sentinel.Sentinel'>, 'loop': ['host1', 'host2', 'host3'], 'loop_control': <class 'ansible.utils.sentinel.Sentinel'>, 'notify': <class 'ansible.utils.sentinel.Sentinel'>, 'poll': <class 'ansible.utils.sentinel.Sentinel'>, 'register': <class 'ansible.utils.sentinel.Sentinel'>, 'retries': <class 'ansible.utils.sentinel.Sentinel'>, 'until': <class 'ansible.utils.sentinel.Sentinel'>, 'loop_with': 'items', 'name': 'Add fake hosts in inventory', 'connection': <class 'ansible.utils.sentinel.Sentinel'>, 'port': <class 'ansible.utils.sentinel.Sentinel'>, 'remote_user': <class 'ansible.utils.sentinel.Sentinel'>, 'vars': {}, 'module_defaults': <class 'ansible.utils.sentinel.Sentinel'>, 'environment': <class 'ansible.utils.sentinel.Sentinel'>, 'no_log': <class 'ansible.utils.sentinel.Sentinel'>, 'run_once': <class 'ansible.utils.sentinel.Sentinel'>, 'ignore_errors': <class 'ansible.utils.sentinel.Sentinel'>, 'ignore_unreachable': <class 'ansible.utils.sentinel.Sentinel'>, 'check_mode': <class 'ansible.utils.sentinel.Sentinel'>, 'diff': <class 'ansible.utils.sentinel.Sentinel'>, 'any_errors_fatal': <class 'ansible.utils.sentinel.Sentinel'>, 'debugger': <class 'ansible.utils.sentinel.Sentinel'>, 'when': <class 'ansible.utils.sentinel.Sentinel'>, 'tags': <class 'ansible.utils.sentinel.Sentinel'>, 'become': <class 'ansible.utils.sentinel.Sentinel'>, 'become_method': <class 'ansible.utils.sentinel.Sentinel'>, 'become_user': <class 'ansible.utils.sentinel.Sentinel'>, 'become_flags': <class 'ansible.utils.sentinel.Sentinel'>, 'collections': <class 'ansible.utils.sentinel.Sentinel'>}
(Pdb) task._attributes["tags"]
<class 'ansible.utils.sentinel.Sentinel'>

After discussing this with upstream in #ansible-devel, it turns out that this new Sentinel object is used to indicate that something has not been set (to allow a distinction between unset and set to None).

We can use something like from ansible.utils.sentinel import Sentinel; if foo is Sentinel to determine if the value is set and otherwise coerce to a value we can send to the database like None or an empty list, for example.

@dmsimard dmsimard added the bug label May 22, 2019
@dmsimard dmsimard pinned this issue May 22, 2019
arecordsansible pushed a commit that referenced this issue May 22, 2019
Tags are available as task.tags, there is no need to use _attributes
which now defaults unset values to a new Sentinel class instead of None
or [].

Fixes: #46
Change-Id: Ie8b2ef112a54497253ea1c3a1b5956c6284434c3
@dmsimard
Copy link
Contributor Author

Fixed.
Released in 0.16.4 and 1.0b2.

@dmsimard dmsimard unpinned this issue May 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant