【问题标题】:Ansible fact is undefined error in conditionalAnsible 事实是条件中未定义的错误
【发布时间】:2020-10-14 20:53:06
【问题描述】:

下面是剧本

---
- 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”的事实,它在 playbook 的上一步中存储了 mongodb 副本集的主节点。 我只想将ansible_facts[ansible_hostname] 与我的primary 事实进行比较。如果它们不相等,我想杀死进程。

我得到的错误如下:

致命:[lpdkubpoc01d.phx.aexp.com]:失败! => {“味精”:“该 条件检查 'ansible_facts[ansible_hostname] != primary' 失败。 错误是:评估条件时出错 (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 事实吗?

【问题讨论】:

    标签: mongodb ansible yaml ansible-facts


    【解决方案1】:

    你可以直接比较使用 ansible 事实而不用写 ansible_facts 之前。只需使用when: ansible_hostname != primary

    【讨论】:

      猜你喜欢
      • 2013-12-29
      • 2019-09-08
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      • 2016-02-15
      相关资源
      最近更新 更多