ansible tasks always show changed even when nothing changes

ansibleautomationidempotency4 replies · 68 views · started
  1. ok so every run of my Ansible playbook shows lots of "changed" tasks, so I cant tell what actually changed. The tasks use shell commands for things like adding lines to files and restarting services.

    what are the usual patterns for making playbooks idempotent? im fairly new to Ansible and learned mostly from copy-pasting examples. most offenders are shell tasks. restarts happen every run too. any ideas??

    cry

    2
  2. @ashm229 are you using templates for config files, or editing them line by line? Templates usually make idempotency much easier.

    0 2 helpful
  3. Mostly echo something >> file style shell tasks and a systemctl restart at the end of each role. Packages are installed with apt module already.

    4 1 helpful
  4. been there man well... run with --check --diff before applying. In check mode, a well-written playbook should report no changes on a configured server. It's a good test of idempotency and also shows you exactly what would change on servers that drifted. Keep in mind that command and shell tasks are skipped in check mode by dfeault, so they won't show up there.

    2 0 helpful
  5. oh ive been there lol use handlers for restarts. instead of a task that always restarts the serivce, notify a hnadler from the task that changes its config:

      notify: Restart myapp
    
    handlers:
      - name: Restart myapp
        ansible.builtin.service:
          name: myapp
          state: restarted
    
    7 0 helpful

Reply to this topic

Sign in or create an account to reply.