0

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    
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.