I am running script with Ansible task which installs some repositories and my goal is to show changed state when running change mode to notify, that script will run if check mode will not be activated.
My first task is to check existence of this repository:
- name: Check repository existence
become: yes
shell: yum repolist -v | grep some-repository
register: repo_exists
check_mode: false
failed_when: false
changed_when: false
This task should run even in check mode, so I can operate with repo_exists variable in next tasks. This works fine when I run Ansible in check mode, this is the output:
"msg": {
"changed": false,
"cmd": "yum repolist -v | grep some-repository",
"delta": "0:00:02.022965",
"end": "2023-06-12 13:06:54.611504",
"failed": false,
"failed_when_result": false,
"msg": "non-zero return code",
"rc": 1,
"start": "2023-06-12 13:06:52.588539",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
My script should run only when repository is missing:
- name: Add repository
become: yes
shell: echo "install repository"
changed_when: "repo_exists.rc != 0"
when: "repo_exists.rc != 0"
when
condition should ensure, task will run only if rc is not zero, which means, repository was not in repolist.
changed_when
should show state CHANGED even in check mode rc is not zero.
Instead of expected result, I can see state skipping:
{
"changed": false,
"cmd": "echo \"install repository\"",
"delta": null,
"end": null,
"msg": "Command would have run if not in check mode",
"rc": 0,
"start": null,
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
I do not understand, why task is in skipping state and what I do wrong? Is this right way how to see shell task in change state when running check mode?