Diagram of three Ansible 2.20 traps: string booleans from -e, tags dropping fact gathering, lost handlers
| |

Ansible 2.20 gotchas: string booleans from -e, tags that drop fact gathering, and lost handlers

Three traps from migrating to ansible-core 2.20: -e var=false is a string, play-level tags remove fact gathering, and handlers lost after an interrupted run.

Since ansible-core 2.19, Ansible is much stricter about types, and Ansible 2.20 made the consequences visible in my playbooks. Migrating, I hit three traps in a row: a boolean that was really a string, fact gathering that silently disappeared when a play carried tags, and handlers that never ran after an interrupted run. Each one hides a real bug rather than creating one, which is why they are worth knowing.

Diagram of three Ansible 2.20 traps: string booleans from -e, tags dropping fact gathering, lost handlers
Each trap, why it happens and the fix.

At a glance

TrapWhat you seeFix
-e var=false is a stringConditional result (True) was derived from value of type 'str'Pass JSON or a file, or use | bool in the role
Play-level tags: removes the always tag from fact gatheringTasks reading ansible_facts fail or get it wrong on --tags runsgather_facts: false plus an explicit setup task tagged always
Lost handlerschanged=0 on the re-run, but the service still runs the old configRestart by hand, force_handlers, flush_handlers, and validate before installing

-e var=false is not a boolean

The most visible consequence of the stricter types is in conditionals: a when: clause must now produce a real boolean. Before, a non-empty string was silently treated as true. Now it’s an error.

The command-line trap

The most common case: turning an option off for one deployment.

ansible-playbook play.yml -e crowdsec_prometheus_enabled=false

With the key=value syntax, -e always produces strings. So the variable is "false", a five-character string, not the boolean false. In a role doing when: crowdsec_prometheus_enabled, ansible-core 2.20 stops:

Task failed: Conditional result (True) was derived from value of type 'str'
at "<CLI option '-e'>". Conditionals must have a boolean result.

The message is precise, too: it names the offending type (str) and where the value came from (the -e option). With an older version, the string "false" would have evaluated as true, since it isn’t empty: the task would have run when you meant to turn it off. So the new strictness prevents a real bug.

Three right ways to do it

  • Pass JSON, which keeps types: -e '{"crowdsec_prometheus_enabled": false}';
  • pass a file, YAML or JSON: -e @overrides.yml, where false is a real boolean;
  • in the role, explicitly convert variables that may come from the command line: when: crowdsec_prometheus_enabled | bool. The bool filter understands "false", "no", "0"…

Another Ansible 2.20 change: tags and fact gathering

Another surprise during the same migration: if a play itself carries tags:, the implicit “Gathering Facts” task loses its always tag. A --tags run that doesn’t target the play’s tag then skips fact gathering, and every task reading ansible_facts fails or gets it wrong. The fix is to turn off implicit gathering and replace it with an explicit task tagged always:

- hosts: all
  gather_facts: false
  tags: [base]
  pre_tasks:
    - name: Gather facts
      ansible.builtin.setup:
      tags: [always]

Moral: after a major ansible-core upgrade, run every playbook in --check, including with targeted --tags, before trusting any deployment.

Lost handlers

Handlers are one of Ansible’s handiest mechanisms: a task that changes a config file “notifies” a handler (restart nginx), and that handler runs only once, at the end of the play, and only if something changed. But that behaviour hides a trap.

The scenario

  • a playbook changes CrowdSec’s config: the task goes changed and notifies restart crowdsec;
  • further on, another task fails (a package not found, a command erroring out);
  • the play stops. By default, pending handlers don’t run;
  • I fix it and re-run. The config is already up to date on disk: the task is ok, notifies nothing, and the handler is never called;
  • result: changed=0, everything looks fine… but CrowdSec is still running the old config loaded in memory.

The trap is that disk state and in-memory state diverge without any signal showing it. I found out because a functional check failed, even though the file was clearly correct.

The fixes

  • By hand, after a failure: restart or reload the affected services. Make it a reflex whenever a run is interrupted;
  • force_handlers: true at play level: notified handlers run even if a later task fails;
  • meta: flush_handlers right after the config tasks, to apply restarts before the risky steps.

The downside of force_handlers

force_handlers isn’t a silver bullet. In my mail server role, the config was deployed, notified a restart, then got validated by a later task. The day validation failed, force_handlers restarted the service anyway with the invalid config, and it crash-looped for a minute.

The right structure is to validate before installing:

- name: Render config to a staging directory
  ansible.builtin.template:
    src: mox.conf.j2
    dest: /home/mox/config/.staging/mox.conf

- name: Validate the staged config
  ansible.builtin.command: /home/mox/mox config test -config /home/mox/config/.staging/mox.conf
  changed_when: false

- name: Install the validated config
  ansible.builtin.copy:
    src: /home/mox/config/.staging/mox.conf
    dest: /home/mox/config/mox.conf
    remote_src: true
  notify: restart mox

That way an invalid config never reaches the real file, and no handler gets notified. You can then keep force_handlers safely. It’s also the idea behind the template module’s validate: option, when the program can validate a single file.

What I take away from Ansible 2.20

  • A when: must produce a real boolean. -e key=value always gives a string, so pass JSON or a file, or use | bool in the role;
  • The new strictness catches a real bug. The string "false" used to evaluate as true;
  • After a major ansible-core upgrade, run every playbook in --check, including with targeted --tags, before trusting any deployment;
  • Disk state and in-memory state can diverge when a run is interrupted: restart the affected services by hand, or use force_handlers or flush_handlers;
  • Validate before installing. Otherwise force_handlers restarts a service with an invalid config.

Related articles

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *