Ansible 如何将主机从;任务“;在剧本中,我';我会得到;“主机名”;在运行playbook时作为参数来自用户

Ansible 如何将主机从;任务“;在剧本中,我';我会得到;“主机名”;在运行playbook时作为参数来自用户,ansible,Ansible,基本上,我在清单主机列表上有6个节点,我想一次停止5台主机上的服务,最后停止一台。 最后一个主机不会每次都相同,我要求用户在触发playbook时输入该主机名作为参数 这是我所做的,但“!”不在“委派到”选项中工作 - name: Action on processor - stop shell: ./all.ksh "{{ Action }}" args: chdir: "/local" register: action_result_stop_inactive #del

基本上,我在清单主机列表上有6个节点,我想一次停止5台主机上的服务,最后停止一台。 最后一个主机不会每次都相同,我要求用户在触发playbook时输入该主机名作为参数

这是我所做的,但“!”不在“委派到”选项中工作

- name: Action on processor - stop
  shell: ./all.ksh "{{ Action }}"
  args:
   chdir: "/local"
  register: action_result_stop_inactive
  #delegate_to: UAT:!"{{ active_server }}"
  when: parameter == "stop"
  notify:
   - "If action is to stop the service"

- name: Action on processor - stop
  shell: ./all.ksh "{{ Action }}"
  args:
   chdir: "/local"
  register: action_result_stop_active
  delegate_to: "{{ active_server }}"
  when: parameter == "stop"
  notify:
   - "If action is to stop the service"

- name: Action on processor - status, start, restart
  shell: ./all.ksh "{{ Action }}"
  args:
   chdir: "/local"
  register: action_result
  when: parameter == "status" or parameter == "start" or parameter == "restart"
  notify:
   - "if action is status or start or restart"
处理程序:

- name: If action is to start service or to query status of service
  debug:
    msg: "{{ action_result.stdout }}"
  when: action_result is defined
  listen:
   - "if action is status or start or restart"

- name: If action is to stop the service
  debug:
    msg: "Stop service: {{item}}"
  with_items:
   - "{{ action_result_stop_inactive.stdout }}"
   - "{{ action_result_stop_active.stdout }}"
  when: action_result_stop_inactive is defined or action_result_stop_active is defined
  listen:
   - "If action is to stop the service"
我正在尝试一次性在主机列表中的所有节点上执行./all.ksh stop,但“active_consumer”节点(也是主机列表的一部分)除外 和
然后,要在“active_consumer”节点上执行./all.ksh stop

一种简单的方法是附加到您的when条件。如果“active_server”是您在命令行中由最终用户提供的服务器,则您可以更改何时执行此操作:

when:parameter==“stop”和inventory\u主机名!=活动\u服务器

然后在下一个任务中将其更改为:

when:parameter==“stop”和inventory\u hostname==active\u server

此外,如果您试图在资源清册中当前所在的主机以外的其他主机上运行任务,则只需将delegate_添加到行。因此,在您的示例中,第二个delegate_to命令将在“active_server”上运行相同的命令6次。所以,除非这是你想做的,否则你可以把这一行删掉

我想一次停止5台主机上的服务,最后停止一台

简单,您可以使用
serial
命令,它将同时在您的清单5台主机上运行:

- hosts: '{{ inventory }}'
  serial: 5
  tasks:
  - name: Action on processor - stop
最后一个
您可以使用
委托给
并使用外部变量或
设置事实
{{singe\u host}
。在代码中,您可以根据需要对其进行操作