Sometimes you want to use Ansible's shell module to save only the command results in register.
In my case, I wrote this task because I wanted to find out whether the login shell is zsh or bash.
main.yml
- name: check using shell register in file path
shell: |
if [ `echo $SHELL | grep -c "zsh"` -eq 1 ]; then
echo ".zshrc"
elif [ `echo $SHELL | grep -c "bash"` -eq 1 ]; then
if [ `uname` = "Linux" ]; then
echo "~/.bashrc"
elif [ `uname` = "Darwin" ]; then
echo "~/.bash_profile"
fi
fi
register: shell_configure_file
Whenever the above task is executed, it will be `change`
.
TASK: [python/pyenv/install | check using shell register in file path] ********
changed: [127.0.0.1]
I checked various things when I wanted to make this changed always ok, so make a note
Just write False in the task
In the case of the previous task, it will be like this
#### **`main.yml`**
```yml
- name: check using shell register in file path
shell: |
if [ `echo $SHELL | grep -c "zsh"` -eq 1 ]; then
echo ".zshrc"
elif [ `echo $SHELL | grep -c "bash"` -eq 1 ]; then
if [ `uname` = "Linux" ]; then
echo "~/.bashrc"
elif [ `uname` = "Darwin" ]; then
echo "~/.bash_profile"
fi
fi
changed_when: False
register: shell_configure_file
In my opinion, this is a changed that you don't have to worry about if it's a playbook you wrote. You can judge immediately by looking at the execution result of ansible, but if you get `change`
when someone else executes this playbook, "Oh, has something changed?" I would think.
I think it is desirable that changed becomes changed only when the behavior of the target OS changes.
Ansible is easy, but there are various workarounds, so it's awkward to return and create a task. .. .. I've written the above task as a shell, so I feel like I've lost a lot ...
Recommended Posts