Ansible事实是条件

问题描述

下面是剧本

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

我存储了一个称为“ primary”的事实,该事实存储了剧本中上一步中的mongodb副本集的primary。 我只想将ansible_facts[ansible_hostname]与我的primary事实进行比较。如果它们不相等,我想杀死进程。

我得到的错误如下:

致命:[lpdkubpoc01d.phx.aexp.com]:失败! => {“ msg”:“ 条件检查“ ansible_facts [ansible_hostname]!=主要”失败。 错误是:评估条件错误 (ansible_facts [ansible_hostname]!= primary):“ ansible_facts”为 未定义\ n \ n错误似乎出在 '/idn/home/sanupin/stop-enterprise-mongodb/tasks/stopAutomationAgent.yml': 第11行第3列,但\ n可能不在文件的其他位置,具体取决于 确切的语法问题。\ n \ n出现问题的行似乎是:\ n \ n
all_processes:“ {{running_agent_processes.stdout_lines + running_mongod_processes.stdout_lines}}“ \ n-名称:全部杀死 进程\ n ^这里\ n“}致命:[lpdkubpoc01c.phx.aexp.com]:失败!=> {“ msg”:“条件检查'ansible_facts [ansible_hostname]!= 主要”失败。错误是:评估条件错误 (ansible_facts [ansible_hostname]!= primary):“ ansible_facts”为 未定义\ n \ n错误似乎出在 '/idn/home/sanupin/stop-enterprise-mongodb/tasks/stopAutomationAgent.yml': 第11行第3列,但\ n可能不在文件的其他位置,具体取决于 确切的语法问题。\ n \ n出现问题的行似乎是:\ n \ n
all_processes:“ {{running_agent_processes.stdout_lines + running_mongod_processes.stdout_lines}}“ \ n-名称:全部杀死 进程\ n ^这里\ n“}致命:[lpdkubpoc01e.phx.aexp.com]:失败!=> {“ msg”:“条件检查'ansible_facts [ansible_hostname]!= 主要”失败。错误是:评估条件错误 (ansible_facts [ansible_hostname]!= primary):“ ansible_facts”为 未定义\ n \ n错误似乎出在 '/idn/home/sanupin/stop-enterprise-mongodb/tasks/stopAutomationAgent.yml': 第11行第3列,但\ n可能不在文件的其他位置,具体取决于 确切的语法问题。\ n \ n出现问题的行似乎是:\ n \ n
all_processes:“ {{running_agent_processes.stdout_lines + running_mongod_processes.stdout_lines}}“ \ n-名称:全部杀死 进程\ n ^这里\ n“}

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

解决方法

您可以比较直接使用的可疑事实,而无需事先写ansible_facts。只需用作when: ansible_hostname != primary

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...