Ansible事实是条件中的未定义错误

Ansible fact is undefined error in conditional

下面是剧本

---
- name: stop agent process
  shell: "ps -ef | grep -v grep | grep -w {{ MONGODB_AGENT_PROCESS }} | awk '{print }'"
  register: running_agent_processes
- name: stop mongod process
  shell: "ps -ef | grep -v grep | grep -w {{ MONGODB_SERVER_PROCESS  }} | awk '{print }'"
  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

我已经存储了一个名为“primary”的事实,它在 playbook 的前一步中存储了 mongodb 副本集的主节点。 我只想比较 ansible_facts[ansible_hostname] 和我的 primary 事实。如果它们不相等,我想杀死进程。

我收到的错误如下:

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"}

有人可以帮我比较 ansible_fact 和 set_fact 事实吗?

不用写ansible_facts就可以直接使用ansible facts进行比较。只需使用 when: ansible_hostname != primary