python - Ansible conditional based on contents of file -


gratefully if point out what's wrong this…

the following code meant setup test in register module prints current value of /etc/timezone. there's task compares group/host {{ timezone }} variable , runs task if it's different (i.e. doesn't call handlers unnecessarily).

but runs regardless.

- name: check current timezone   shell: cat /etc/timezone   register: get_timezone  - name: set /etc/timezone   shell: echo "{{ timezone }}" > /etc/timezone   when: get_timezone.stdout.find('{{ timezone }}') == false   notify: update tzdata 

....

in group_vars/all.yml:

timezone: europe/london 

python string.find method returns -1 if can't find substring (https://docs.python.org/2/library/string.html, see string.find). so, modify yml that:

- name: set /etc/timezone   shell: echo "{{ timezone }}" > /etc/timezone   when: get_timezone.stdout.find('{{ timezone }}') == -1   notify: update tzdata 

or use "not in":

- name: set /etc/timezone   shell: echo "{{ timezone }}" > /etc/timezone   when: '"{{ timezone }}" not in get_timezone.stdout'   notify: update tzdata 

Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -