Ansible fact is undefined error in conditional

821 views Asked by At

Below is the playbook

---
- name: stop agent process
  shell: "ps -ef | grep -v grep | grep -w {{ MONGODB_AGENT_PROCESS }} | awk '{print $2}'"
  register: running_agent_processes
- name: stop mongod process
  shell: "ps -ef | grep -v grep | grep -w {{ MONGODB_SERVER_PROCESS  }} | awk '{print $2}'"
  register: running_mongod_processes
- name: combine processes
  set_fact:
    all_processes: "{{ running_agent_processes.stdout_lines + running_mongod_processes.stdout_lines }}"
- name: Kill all processes
  shell: "kill {{ item }}"
  with_items: "{{ all_processes }}"
  when: ansible_facts[ansible_hostname] != primary
- wait_for:
    path: "/proc/{{ item }}/status"
    state: absent
  with_items: "{{ all_processes }}"
  ignore_errors: yes
  register: killed_processes
  when: ansible_facts[ansible_hostname] != primary
- name: Force kill stuck processes
  shell: "kill -9 {{ item }}"
  with_items: "{{ killed_processes.results | select('failed') | map(attribute='item') | list }}"
  when: ansible_facts[ansible_hostname] != primary

I have stored a fact called "primary" which stores the primary of a mongodb replica set in a previous step in the playbook. I just want to compare the ansible_facts[ansible_hostname] with my primary fact. If they are not equal, I would like to kill processes.

The error I am getting is below:

fatal: [lpdkubpoc01d.phx.aexp.com]: FAILED! => {"msg": "The conditional check 'ansible_facts[ansible_hostname] != primary' failed. The error was: error while evaluating conditional (ansible_facts[ansible_hostname] != primary): 'ansible_facts' is undefined\n\nThe error appears to have been in '/idn/home/sanupin/stop-enterprise-mongodb/tasks/stopAutomationAgent.yml': line 11, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n
all_processes: "{{ running_agent_processes.stdout_lines + running_mongod_processes.stdout_lines }}"\n- name: Kill all processes\n ^ here\n"} fatal: [lpdkubpoc01c.phx.aexp.com]: FAILED! => {"msg": "The conditional check 'ansible_facts[ansible_hostname] != primary' failed. The error was: error while evaluating conditional (ansible_facts[ansible_hostname] != primary): 'ansible_facts' is undefined\n\nThe error appears to have been in '/idn/home/sanupin/stop-enterprise-mongodb/tasks/stopAutomationAgent.yml': line 11, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n
all_processes: "{{ running_agent_processes.stdout_lines + running_mongod_processes.stdout_lines }}"\n- name: Kill all processes\n ^ here\n"} fatal: [lpdkubpoc01e.phx.aexp.com]: FAILED! => {"msg": "The conditional check 'ansible_facts[ansible_hostname] != primary' failed. The error was: error while evaluating conditional (ansible_facts[ansible_hostname] != primary): 'ansible_facts' is undefined\n\nThe error appears to have been in '/idn/home/sanupin/stop-enterprise-mongodb/tasks/stopAutomationAgent.yml': line 11, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n
all_processes: "{{ running_agent_processes.stdout_lines + running_mongod_processes.stdout_lines }}"\n- name: Kill all processes\n ^ here\n"}

Can someone help me with comparing an ansible_fact with a set_fact fact?

1

There are 1 answers

0
gary lopez On BEST ANSWER

You can comparing use directly ansible facts without write ansible_facts before. Just use as when: ansible_hostname != primary