NAME
	blog.hoberg.ch - useless things for DevOps 

DESCRIPTION
	blog.hoberg.ch is a text mode blog about BSD, Linux and typical DevOP
        content like Ansible, Puppet etc. running on pyhpblog.
 
OPTIONS
	[website] [blog] [privacy] [impress]

CONTENT
        - Ansible with_sequence loop in when condition with zero/0 integer
	  When using Ansible function 'with_sequence' on a task where the end value
	  is zero (0) combained with a when condition it may fail with:

		to count backwards make stride negative

	  But there may be usecases where you want to evaluate the when condition
	  before the sequence will be and not to fail. So a negative value may still
	  be legit to use for your needs.

          For example you want to define if some hotfixes (or even no hotfix) should
	  be downloaded you propably would write:


		- name: Download patch files 
		    uri:
		        url: "https://files.hoberg.ch/patches/hotfix{{ item }}.tgz"
		    with_sequence: start=1 end={{ hotfix_max_patchfile }}
		    when: hotfix_max_patchfile|int > 0

	  But if 'hotfix_max_patchfile' is zero (0) it will fail. You will also find
	  some solutions like creating a condition for the end value that could look
	  like this:


		 - name: Download patch files  
    		uri:
        		url: "https://files.hoberg.ch/patches/hotfix{{ item }}.tgz"
    		with_sequence: start=1 end={{ hotfix_max_patchfile|int if hotfix_max_patchfile|int > 0 else 1 }} 
    		when: hotfix_max_patchfile|int > 0


	  This will work and set the variable 'hotfix_max_patchfile' to 1 which is no
	  problem in here, because of the defined when condition. The task will be simply
	  skipped - but this won't look that good.

	  A cleaner way would be to outsource the needed tasks in subplaybooks and
	  include them as a non static one. So the included tasks won't be executed
	  and skipped like the usual static way (default). So you could write:


	  main.yml:

		---
		- include: download.yml
  		static: no
		when:
		  - hotfix_max_patchfile|int > 0


	  download.yml:

		---
                - name: Download patch files
                    uri:
                        url: "https://files.hoberg.ch/patches/hotfix{{ item }}.tgz"
                    with_sequence: start=1 end={{ hotfix_max_patchfile }}


	  In that case you won't need any dirty hacks and have a clean way to write
	  your tasks.



blog.hoberg.ch (C) 2019 [privacy] | [impress]