How to prevent 'changed' flag when 'register'-ing a variable?

后端 未结 2 889
-上瘾入骨i
-上瘾入骨i 2021-01-03 18:24

I have a register task to test for the installation of a package:

tasks:
  - name: test for nginx
    command: dpkg -s nginx-common
    register         


        
相关标签:
2条回答
  • 2021-01-03 18:29

    It is the command module causing the changed state, not the register parameter.

    You can set changed_when: to something that is only true when something changed (also look at failed_when). If your task don't change anything, you might want to set check_mode as well. (Especially if other steps depend on the value)

    This gives:

    tasks:
      - name: test for nginx
        command: dpkg -s nginx-common
        register: nginx_installed
        changed_when: False
        failed_when: False  # dpkg -s returns 1 when packages is not found
        check_mode: yes # this can safely run in check_mode
    
    0 讨论(0)
  • 2021-01-03 18:55

    It’s described in official documentation here.

    tasks:
      - name: test for nginx
        command: dpkg -s nginx-common
        register: nginx_installed
        changed_when: false
    
    0 讨论(0)
提交回复
热议问题