Optional conditional based on file content

Sincerely, if someone can indicate what is wrong with this ...

The following code is for installing a test in a register module that prints the current value /etc/timezone. Then a task is executed that compares this with the group / host {{timezone}} variable and runs the task only if it is different (that is, it does not call handlers unnecessarily).

But it always works independently.

- 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
+4
source share
1 answer

python string.find -1, (https://docs.python.org/2/library/string.html, . string.find). , yml :

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

" ":

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

Source: https://habr.com/ru/post/1537272/


All Articles