Register being set even though task is skipped

Hi there

I’m running into an issue that I don’t know if it is a expected behaviour or a bug…
I have a play with two task both of them have the conditional “when” and both of them use the same variable name as a register like so :

  • name: Forcing Termination of instances
    hosts: localhost
    connection: local
    tasks:

  • name: Finding instances to terminate base on Build Number
    #shell: aws ec2 --output=json --region eu-west-1 describe-instances|jq ‘.Reservations.Instances | select(contains({Tags’:’ [{Key’:’ “terminate_me”}]}))'|jq -r “.InstanceId”
    shell: aws ec2 --output=json --region eu-west-1 describe-instances --filter “Name=tag:jenkins_build,Values={{ jenkins_build }}” “Name=instance-state-name,Values=running”|jq -r “.Reservations.Instances.InstanceId”
    register: ec2_instancesIDs
    when: with_jenkins_build

  • name: Finding instances to terminate after Failed job run
    #shell: aws ec2 --output=json --region eu-west-1 describe-instances|jq ‘.Reservations.Instances | select(contains({Tags’:’ [{Key’:’ “terminate_me”}]}))'|jq -r “.InstanceId”
    shell: aws ec2 --output=json --region eu-west-1 describe-instances --filter “Name=tag:terminate_me,Values=true” “Name=instance-state-name,Values=running”|jq -r “.Reservations.Instances.InstanceId”
    register: ec2_instancesIDs
    when: no_build_number

  • debug: var=ec2_instancesIDs

I noticed in debug mode that even if the task is being skipped the variable is being set anyway so if I run the cleanup jenkins job and I use the jenkins build number and I call ansible like so :

ansible-playbook -vvvv --tags cleanup --extra-vars no_build_number=false cleanup=true with_jenkins_build=true jenkins_build=70 jmeter/loadtools/site.yml

The ec2_instancesIDs value ends to be :

Yes this is expected. Register always stores the state of the task, but you can just check the .skipped value in your when: statement.