0

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.

1

1 Answer 1

0

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 @Vladimir Botka asks for an MCVE in the comments, this is a typical example.

1
  • 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.
    – mbb
    Commented Aug 12, 2019 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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