[PYTHON] [Ansible] How to prevent forced change

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

environment

Referenced site

ansible Personal Notes & Tips

How to always be ok

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

Why shouldn't we allow changed?

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.

Finally

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

[Ansible] How to prevent forced change
How to change Jupyter layout
How to change Python version
How to change vim color scheme
How to prevent package updates with apt
How to change editor color in PyCharm
[Python] How to change the date format (display format)
How to use xml.etree.ElementTree
How to use virtualenv
Scraping 2 How to scrape
How to use Seaboan
How to use image-match
How to use shogun
How to install Python
How to use Pandas 2
[Ansible] How to use SSH password authentication when executing ansible
How to read PyPI
How to install pip
How to use Virtualenv
How to use numpy.vectorize
How to update easy_install
How to install archlinux
How to use pytest_report_header
How to restart gunicorn
How to install python
How to virtual host
How to debug selenium
How to use partial
How to use Bio.Phylo
How to read JSON
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to update Spyder
How to use IPython
How to install BayesOpt
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to change static directory from default in Flask
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to grow dotfiles
How to use list []
[Work efficiency] How to change file names in Python
How to use python-kabusapi
"How to count Fukashigi"
How to install Nbextensions
How to use OptParse
How to use return
How to install Prover9
How to use dotenv
How to operate NumPy
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
[Ansible] How to call variables when creating your own module