0

Minimal reproducible example:

role/defaults/main.yaml:

node_sysctl:
  kernel.shmall: 197951838
  kernel.shmmax: 810810728448
  kernel.shmmni: 4096

role/tasks/main.yaml:

- name: Tune sysctl
  sysctl:
    name: "{{ item.key }}"
    value: "{{ item.value }}"
    reload: true
  loop: "{{ lookup('dict', node_sysctl, wantlist=True) }}"
  become: true

I'm trying to run this task against an Ubuntu 18.04 host. When I do, I get the following ansible error:

TASK [../../../../ansible/roles/test-role : Tune sysctl] *********************************************************************************************************
fatal: [coordinator]: FAILED! => {"msg": "Invalid data passed to 'loop', it requires a list, got this instead:
 [{'key': 'kernel.shmall', 'value': 197951838}, {'key': 'kernel.shmmax', 'value': 810810728448}, {'key': 'kernel.shmmni', 'value': 4096}, {'key': 'vm.overcommit_memory', 'value': 2}, {'key': 'vm.overcommit_ratio', 'value': 95}, {'key': 'net.ipv4.ip_local_port_range', 'value': '10000 65535'}, {'key': 'kernel.sem', 'value': '250 2048000 200 8192'}, {'key': 'kernel.sysrq', 'value': 1}]. Hint: If you passed a list/dict of just one element, try adding wantlist=True to your lookup invocation or use q/query instead of lookup."}

loop is clearly receiving a list here, and I don't understand why it won't work.

Running with python 3.9 on the local machine, and ansible:

ansible --version

  import pkg_resources
ansible 2.7.10
  config file = None
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/user/.local/share/virtualenvs/linux-p5PCkeXK/lib/python3.9/site-packages/ansible
  executable location = /home/user/.local/share/virtualenvs/linux-p5PCkeXK/bin/ansible
  python version = 3.9.18 (main, Nov 28 2023, 14:57:43) [GCC 13.2.1 20231011 (Red Hat 13.2.1-4)]

I also tried using with_items instead of loop, which also produces an error:

FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'key'

1 Answer 1

0

It appears that the root cause was some internal incompatibility between the ancient ansible I'm forced to use and python 3.9.

Simply switching the virtual environment on my machine to use python 3.7 resolved the issue and now the task runs successfully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.