<p><code>register</code> will always populate your registered var even when the task is skipped, e.g. you will end up with something like the following:</p>

<pre><code>"kafkaip": {
    "changed": false,
    "skip_reason": "Conditional result was False",
    "skipped": true
}
</code></pre>

<p>Since only the commands differ depending on your env, I would store the command in a variable and then execute it, something like the following.</p>

<pre><code>- name: "Set command for env &lt;&gt; QA"
  set_fact:
    get_ip_command: &gt;-
      aws cloudformation describe-stacks
      --stack-name "{{ stack_kafka }}"
      --query "Stacks[*].Outputs[?OutputKey=='KafkaLink'].OutputValue"
  when:
    - stack_kafka is defined
    - ( deployment_env != "QA" )

- name: "Set command for env = QA"
  set_fact:
    get_ip_command: &gt;-
      aws ec2 describe-instances
      --filter "Name=tag:StackName,Values=KafkaCluster"
      --query 'Reservations[].Instances[].PrivateIpAddress'
      --output text
  when:
    - stack_kafka is defined
    - ( deployment_env == "QA" )

- name: Get the Kafka InstancesPrivateIps
  shell: "{{ get_ip_command }}"
  register: kafkaip
  when: stack_kafka is defined

- name: Register kafka_host
  set_fact:
    kafka_host: "{{ kafkaip.stdout }}"
  when: stack_kafka is defined

</code></pre>

<ul>
<li><strong>Note1</strong>: there is an output redirection in your QA command which will prevent capturing the output as you wish, I left it out in my example.</li>
<li><strong>Note2</strong>: I used <code>set_fact</code> to create the command variable but you can put that in your vars or your inventory to have a leaner playbook.</li>
</ul>

CC BY-SA 4.0
2019-04-15T10:59:58.243Z
2019-04-15T10:59:58.243Z
Zeitounator
<p>I have situation where i fetch some ip address on the fly with this play,  when env variable is passed as prod, i have to fetch ip in a diff way then when i am in QA env.</p>

<p>If env is prod the 1st play would get ip address and store in register <strong>"kafkaip"</strong> and i have put a when condition on the 2nd play it gets skipped which is fine as the env is diff.</p>

<p>But although its skip the 2nd play, it wipes out the register value and my set_fact fails,, any way to overcome below scenario.</p>

<pre><code>    - name: Get the Kafka InstancesPrivateIps
      local_action: shell aws cloudformation describe-stacks --stack-name "{{ stack_kafka }}" --query "Stacks[*].Outputs[?OutputKey=='KafkaLink'].OutputValue"
      register: kafkaip
      when: 
      - stack_kafka is defined

    - name: Get the Kafka InstancesPrivateIps New Automation.
      shell: |
        aws  ec2 describe-instances --filter "Name=tag:StackName,Values=KafkaCluster" --query 'Reservations[].Instances[].PrivateIpAddress' --output text &gt;  /tmp/kafka_ip.txt
      register: kafkaip   
      when: 
      - stack_kafka is defined
      - ( deployment_env == "QA" )    
      delegate_to: localhost

    - name: setting kafka_host.
      set_fact:
        kafka_host: "{{ kafkaip.stdout }}"
      when: stack_kafka is defined    
</code></pre>

<ansible>
unable to save register value even if the play is skipped!
Mohammed Ali
# unable to save register value even if the play is skipped!

(Asked by: username_0 on 2019-03-06)

I have situation where i fetch some ip address on the fly with this play,  when env variable is passed as prod, i have to fetch ip in a diff way then when i am in QA env.
If env is prod the 1st play would get ip address and store in register "kafkaip" and i have put a when condition on the 2nd play it gets skipped which is fine as the env is diff.
But although its skip the 2nd play, it wipes out the register value and my set_fact fails,, any way to overcome below scenario.
    - name: Get the Kafka InstancesPrivateIps
      local_action: shell aws cloudformation describe-stacks --stack-name "{{ stack_kafka }}" --query "Stacks[*].Outputs[?OutputKey=='KafkaLink'].OutputValue"
      register: kafkaip
      when: 
      - stack_kafka is defined

    - name: Get the Kafka InstancesPrivateIps New Automation.
      shell: |
        aws  ec2 describe-instances --filter "Name=tag:StackName,Values=KafkaCluster" --query 'Reservations[].Instances[].PrivateIpAddress' --output text >  /tmp/kafka_ip.txt
      register: kafkaip   
      when: 
      - stack_kafka is defined
      - ( deployment_env == "QA" )    
      delegate_to: localhost

    - name: setting kafka_host.
      set_fact:
        kafka_host: "{{ kafkaip.stdout }}"
      when: stack_kafka is defined

**Tags:** ansible


---

## 1 answers

### Answer by username_1 on 2019-04-15. Score: 1

register will always populate your registered var even when the task is skipped, e.g. you will end up with something like the following:
"kafkaip": {
    "changed": false,
    "skip_reason": "Conditional result was False",
    "skipped": true
}

Since only the commands differ depending on your env, I would store the command in a variable and then execute it, something like the following.
- name: "Set command for env <> QA"
  set_fact:
    get_ip_command: >-
      aws cloudformation describe-stacks
      --stack-name "{{ stack_kafka }}"
      --query "Stacks[*].Outputs[?OutputKey=='KafkaLink'].OutputValue"
  when:
    - stack_kafka is defined
    - ( deployment_env != "QA" )

- name: "Set command for env = QA"
  set_fact:
    get_ip_command: >-
      aws ec2 describe-instances
      --filter "Name=tag:StackName,Values=KafkaCluster"
      --query 'Reservations[].Instances[].PrivateIpAddress'
      --output text
  when:
    - stack_kafka is defined
    - ( deployment_env == "QA" )

- name: Get the Kafka InstancesPrivateIps
  shell: "{{ get_ip_command }}"
  register: kafkaip
  when: stack_kafka is defined

- name: Register kafka_host
  set_fact:
    kafka_host: "{{ kafkaip.stdout }}"
  when: stack_kafka is defined



Note1: there is an output redirection in your QA command which will prevent capturing the output as you wish, I left it out in my example.
Note2: I used set_fact to create the command variable but you can put that in your vars or your inventory to have a leaner playbook.


---

