Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
698 views
in Technique[技术] by (71.8m points)

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

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

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

Every run it gets reported as a "change":

TASK: [test for nginx] ********************************************************
changed: [vm1]

I don't regard this as a change... it was installed last run and is still installed this run. Yeah, not a biggy, just one of those untidy OCD type issues.

So am I doing it wrong? Is there some way to use register without it always being regarded as a change?

The [verbose] output is untidy, but the only way I've found to get the correct return code.

TASK: [test for nginx] ******************************************************** changed: [vm1] => {"changed": true, "cmd": ["dpkg", "-s", "nginx-common"], "delta": "0:00:00.010231", "end": "2014-05-30 12:16:40.604405", "rc": 0, "start": "2014-05-30 12:16:40.594174", "stderr": "", "stdout": "Package: nginx-common Status: install ok ... Homepage: http://nginx.net"}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It’s described in official documentation here.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...