Answers: <p>Your statements look ok. If I create a test case from the sparse info you provided and what I can observe when gathering facts from my own hosts, I can detect old/recent distributions correctly using your conditions. See my example below.</p>
  
  <p>I suggest you double check the data gathered from your host to understand why it doesn't match. The easiest way for that: from the same host your use to launch your playbook, run</p>
  
  <pre><code>ansible -i path/to/your_inventory.ini your_target_group_name -m setup -a "filter=ansible_distribution*"
  </code></pre>
  
  <p>Here is the <code>test.yml</code> playbook I wrote to test your (known-so-far) values and your conditions</p>
  
  <pre><code>---
  - name: Statements for checking old/recent distribs
    hosts: localhost
    gather_facts: false
  
    vars:
      os_list:
        - ansible_distribution: "Amazon"
          ansible_distribution_version: "(Karoo)"
          ansible_distribution_major_version: "(Karoo)"
        - ansible_distribution: "Amazon"
          ansible_distribution_version: "2018.03"
          ansible_distribution_major_version: "2018"
        - ansible_distribution: "RedHat"
          ansible_distribution_version: "6.x"
          ansible_distribution_major_version: "6"
        - ansible_distribution: "RedHat"
          ansible_distribution_version: "7.x"
          ansible_distribution_major_version: "7"
  
    tasks:
  
      - name: Check conditions for recent distribs
        debug:
          msg: "This is a recent distrib"
        loop: "{{ os_list }}"
        loop_control:
          label: "{{ item.ansible_distribution }}-{{ item.ansible_distribution_version }}"
        when: (
                item.ansible_distribution == "Amazon"
                and
                item.ansible_distribution_version == "(Karoo)"
              )
              or
              (
                item.ansible_distribution == "RedHat"
                and
                item.ansible_distribution_major_version == "7"
              )
  
      - name: Check conditions for old distribs
        debug:
          msg: "This is an old distrib"
        loop: "{{ os_list }}"
        loop_control:
          label: "{{ item.ansible_distribution }}-{{ item.ansible_distribution_version }}"
        when: (
                item.ansible_distribution == "Amazon"
                and
                item.ansible_distribution_version == "2018.03"
              )
              or
              (
                item.ansible_distribution == "RedHat"
                and
                item.ansible_distribution_major_version == "6"
              )
  </code></pre>
  
  <p>And the result</p>
  
  <pre><code>PLAY [Statements for checking old/recent distribs] ************************************************************************************************************************************************************************
  
  TASK [Check conditions for recent distribs] *******************************************************************************************************************************************************************************
  ok: [localhost] =&gt; (item=Amazon-(Karoo)) =&gt; {
      "msg": "This is a recent distrib"
  }
  skipping: [localhost] =&gt; (item=Amazon-2018.03) 
  skipping: [localhost] =&gt; (item=RedHat-6.x) 
  ok: [localhost] =&gt; (item=RedHat-7.x) =&gt; {
      "msg": "This is a recent distrib"
  }
  
  TASK [Check conditions for old distribs] **********************************************************************************************************************************************************************************
  skipping: [localhost] =&gt; (item=Amazon-(Karoo)) 
  ok: [localhost] =&gt; (item=Amazon-2018.03) =&gt; {
      "msg": "This is an old distrib"
  }
  ok: [localhost] =&gt; (item=RedHat-6.x) =&gt; {
      "msg": "This is an old distrib"
  }
  skipping: [localhost] =&gt; (item=RedHat-7.x) 
  
  PLAY RECAP ****************************************************************************************************************************************************************************************************************
  localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  </code></pre>
  
  <p>As you can see it does the job as expected. So either your data is not gathered correctly or your check the wrong values.</p>
  
  <p><strong>Note</strong>: When @Vladimir Botka asks for an MCVE in the comments, this is a typical example.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-08-12T07:33:41.560Z
Answers: Thank you for response and apologies I couldn't answer earlier. By checking ansible hosts with the command you proposed I got the following information:
  
  "ansible_distribution_major_version": "2018" for the Amazon v1 AMI
  and  "ansible_distribution_major_version": "2" for the Amazon 2 AMI. According to that I updated my if statements in the Ansible playbook and everything works as it should. 
  Thank you for answers. Next time, when I create the question topic I would provide MCVE immediately.
Answers: mbb
Answers: CC BY-SA 4.0
Answers: 2019-08-10T23:01:18.277Z
Answers: 2019-08-10T23:01:18.277Z
Answers: Zeitounator
Body: <p>I've got 1 master node from which I would like to instal NewRelic agents remotely depedning on the OS I have on them. Some of them are Amazon 2 Amis and some Amazon 1 Ami.
  I put when statement in the Ansible Playbook's task in the following way:</p>
  
  <pre><code>- name: Enable repo for Amazon Linux 2/Red Hat 7v
    yum_repository:
      name: newrelic-infra
      description: NewRelic repo
      baseurl: https://download.newrelic.com/infrastructure_agent/linux/yum/el/7/$basearch
    when: (ansible_distribution == "Amazon" and ansible_distribution_version == "(Karoo)") or
          (ansible_distribution == "RedHat" and ansible_distribution_major_version == "7")
  
  - name: Enable repo for Amazon Linux 1/Red Hat 6v
    yum_repository:
      name: newrelic-infra
      description: NewRelic repo
      baseurl: https://download.newrelic.com/infrastructure_agent/linux/yum/el/6/$basearch
    when: (ansible_distribution == "Amazon" and ansible_distribution_version == "2018.03") or
          (ansible_distribution == "RedHat" and ansible_distribution_major_version == "6")
  </code></pre>
  
  <p>I understand that when statement should check the if statement and proceed further. Nevertheless, whenever I run playbook I receive that if statement is false and it's skipping above tasks. What might be the root cause?</p>
  
  <p>I've already checked on remote host ansible_distribution and ansible_distribution_version if strings match. I suppose it must be some syntax problem as I'm new in ansible. 
  Thanks in advance.</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-08-09T12:37:23.917Z
Comments: Post: `ansible_distribution, ansible_distribution_version, ansible_distribution_major_version` and also post the output. Generally make it [mcve](https://stackoverflow.com/help/minimal-reproducible-example) (or include a crystal ball). [I downvoted because lacking an MCVE makes it hard to answer](https://idownvotedbecau.se/nomcve/).
Comments: Vladimir Botka
Tags: <ansible>
Title: Ansible - when statement task always return false value
UserDisplayName: mbb
ThreadText: # Ansible - when statement task always return false value
  
  (Asked by: username_0 on 2019-08-09)
  
  I've got 1 master node from which I would like to instal NewRelic agents remotely depedning on the OS I have on them. Some of them are Amazon 2 Amis and some Amazon 1 Ami.
  I put when statement in the Ansible Playbook's task in the following way:
  
  ```
  - name: Enable repo for Amazon Linux 2/Red Hat 7v
    yum_repository:
      name: newrelic-infra
      description: NewRelic repo
      baseurl: https://download.newrelic.com/infrastructure_agent/linux/yum/el/7/$basearch
    when: (ansible_distribution == "Amazon" and ansible_distribution_version == "(Karoo)") or
          (ansible_distribution == "RedHat" and ansible_distribution_major_version == "7")
  
  - name: Enable repo for Amazon Linux 1/Red Hat 6v
    yum_repository:
      name: newrelic-infra
      description: NewRelic repo
      baseurl: https://download.newrelic.com/infrastructure_agent/linux/yum/el/6/$basearch
    when: (ansible_distribution == "Amazon" and ansible_distribution_version == "2018.03") or
          (ansible_distribution == "RedHat" and ansible_distribution_major_version == "6")
  
  ```
  
  I understand that when statement should check the if statement and proceed further. Nevertheless, whenever I run playbook I receive that if statement is false and it's skipping above tasks. What might be the root cause?
  
  I've already checked on remote host ansible\_distribution and ansible\_distribution\_version if strings match. I suppose it must be some syntax problem as I'm new in ansible.
  Thanks in advance.
  
  **Tags:** ansible
  
  > **Comments:**
  > **username_1**: Post: `ansible_distribution, ansible_distribution_version, ansible_distribution_major_version` and also post the output. Generally make it [mcve](https://stackoverflow.com/help/minimal-reproducible-example) (or include a crystal ball). [I downvoted because lacking an MCVE makes it hard to answer](https://idownvotedbecau.se/nomcve/).  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-08-10. Score: 0 | Accepted answer
  
  Your statements look ok. If I create a test case from the sparse info you provided and what I can observe when gathering facts from my own hosts, I can detect old/recent distributions correctly using your conditions. See my example below.
  
  I suggest you double check the data gathered from your host to understand why it doesn't match. The easiest way for that: from the same host your use to launch your playbook, run
  
  ```
  ansible -i path/to/your_inventory.ini your_target_group_name -m setup -a "filter=ansible_distribution*"
  
  ```
  
  Here is the `test.yml` playbook I wrote to test your (known-so-far) values and your conditions
  
  ```
  ---
  - name: Statements for checking old/recent distribs
    hosts: localhost
    gather_facts: false
  
    vars:
      os_list:
        - ansible_distribution: "Amazon"
          ansible_distribution_version: "(Karoo)"
          ansible_distribution_major_version: "(Karoo)"
        - ansible_distribution: "Amazon"
          ansible_distribution_version: "2018.03"
          ansible_distribution_major_version: "2018"
        - ansible_distribution: "RedHat"
          ansible_distribution_version: "6.x"
          ansible_distribution_major_version: "6"
        - ansible_distribution: "RedHat"
          ansible_distribution_version: "7.x"
          ansible_distribution_major_version: "7"
  
    tasks:
  
      - name: Check conditions for recent distribs
        debug:
          msg: "This is a recent distrib"
        loop: "{{ os_list }}"
        loop_control:
          label: "{{ item.ansible_distribution }}-{{ item.ansible_distribution_version }}"
        when: (
                item.ansible_distribution == "Amazon"
                and
                item.ansible_distribution_version == "(Karoo)"
              )
              or
              (
                item.ansible_distribution == "RedHat"
                and
                item.ansible_distribution_major_version == "7"
              )
  
      - name: Check conditions for old distribs
        debug:
          msg: "This is an old distrib"
        loop: "{{ os_list }}"
        loop_control:
          label: "{{ item.ansible_distribution }}-{{ item.ansible_distribution_version }}"
        when: (
                item.ansible_distribution == "Amazon"
                and
                item.ansible_distribution_version == "2018.03"
              )
              or
              (
                item.ansible_distribution == "RedHat"
                and
                item.ansible_distribution_major_version == "6"
              )
  
  ```
  
  And the result
  
  ```
  PLAY [Statements for checking old/recent distribs] ************************************************************************************************************************************************************************
  
  TASK [Check conditions for recent distribs] *******************************************************************************************************************************************************************************
  ok: [localhost] => (item=Amazon-(Karoo)) => {
      "msg": "This is a recent distrib"
  }
  skipping: [localhost] => (item=Amazon-2018.03) 
  skipping: [localhost] => (item=RedHat-6.x) 
  ok: [localhost] => (item=RedHat-7.x) => {
      "msg": "This is a recent distrib"
  }
  
  TASK [Check conditions for old distribs] **********************************************************************************************************************************************************************************
  skipping: [localhost] => (item=Amazon-(Karoo)) 
  ok: [localhost] => (item=Amazon-2018.03) => {
      "msg": "This is an old distrib"
  }
  ok: [localhost] => (item=RedHat-6.x) => {
      "msg": "This is an old distrib"
  }
  skipping: [localhost] => (item=RedHat-7.x) 
  
  PLAY RECAP ****************************************************************************************************************************************************************************************************************
  localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  
  ```
  
  As you can see it does the job as expected. So either your data is not gathered correctly or your check the wrong values.
  
  **Note**: When @username_1 asks for an MCVE in the comments, this is a typical example.
  
  > **Comments:**
  > **username_0**: Thank you for response and apologies I couldn't answer earlier. By checking ansible hosts with the command you proposed I got the following information:  "ansible_distribution_major_version": "2018" for the Amazon v1 AMI and  "ansible_distribution_major_version": "2" for the Amazon 2 AMI. According to that I updated my if statements in the Ansible playbook and everything works as it should.  Thank you for answers. Next time, when I create the question topic I would provide MCVE immediately.  
  
  ---
  
  