Ansible - 根据输出仅定位一个主机

时间:2016-12-07 18:56:18

标签: ansible

我的目标是根据输出仅执行一个主机的任务。搜索了--limit模块,但它不存在。

在3个节点上执行shell脚本,返回一个布尔值。

ok: [localMulti3] => {
    "variable": {
        ...
        "stdout": "false",
        ...
    }
}
ok: [localMulti2] => {
    "variable": {
        ...
        "stdout": "false",
        ...
    }
}
ok: [localMulti1] => {
    "variable": {
        ...
        "stdout": "true",
        ...
    }
}

如何仅在localMulti3localMulti2上执行任务?

失败:

- shell: echo
  when: variable.stdout == "false" and play_hosts[1]

3 个答案:

答案 0 :(得分:0)

要在特定主机上执行任务,您可以检查inventory_hostname变量:

- shell: echo ok
  when: variable.stdout | bool == false and inventory_hostname == 'localMulti3'

更新(使用评论信息) - 仅使用run_once执行任务:

- shell: echo ok
  when: variable.stdout | bool == false
  run_once: true

答案 1 :(得分:0)

如果您使用register结果,则会保存动作内容。因此,在另一个操作中,可以访问此注册变量。

- name: shell_script
  script: shell_script.sh
  register: variable

- shell: echo
  when: variable.stdout == 'false'

答案 2 :(得分:0)

**更新** 真实答案

在节点2和3上执行:

$: touch /tmp/hi

Ansible:

    - stat:
        path: /tmp/hi
      register: me

    - set_fact:
       run_on: "{{ item }}"
      with_items: "{{ play_hosts }}"
      when: run_on is undefined and hostvars[item].me.stat.exists
      run_once: true

    - shell: echo
      when: inventory_hostname == run_on

**以下是旧答案**

好吧,已经完成了。这不是一个很好的方法,但它完成了工作。这是一个令人讨厌的bitc * h,因为你不能简单地在游戏中定位主机,或者为角色中的主机设置全局变量。

主持人的共同点是最终的数字。它可以是1,2或3.并且可以在游戏中为其他主机设置变量。

希望有一天我能用这个拯救另一个人。

我们走了:

- hosts:
      - node1
      - node2
      - node3

  tasks:
    - name: some command to check on host for TRUE or false
      shell: somecommand
      register: commando

    - name: set fact where stat is false
      set_fact:
        theCheck: present
      when: commando.stat.exists == false

    - name: echo the hostnames, for convience and shorter naming
      shell: hostname
      register: hn
      changed_when: false

    # First host check
    - name: check if 1st node is applicable
      set_fact:
        firstNode: True
      when: "{{ hn.stdout | last | last }} == 1 and theCheck is defined"

    - name: set a fact for all hosts in play
      set_fact:
        checkFirst: "{{ hostvars[groups['all'][0]]['firstNode'] | default(False) }}"

    - name: perform task if applicable on first host
      shell: echo
      when: firstNode is defined

    # Second host check
    - name: check if 2nd node is applicable host
      set_fact:
        secondNode: present
      when: "{{ hn.stdout | last | last }} == 2 and theCheck is defined and checkFirst == False"

    - name: set a fact for all hosts in play
      set_fact:
        checkSecond: "{{ hostvars[groups['all'][1]]['secondNode'] | default(False) }}"

    - name: perform task on second node if applicable
      shell: echo
      when: secondNode is defined

    # Third host check
    - name: perform task on third node if applicable
      shell: echo
      when: "{{ hn.stdout | last | last }} == 3 and mongoSlaves is defined and checkSecond == False"
      register: thirdNode

    ### EXTRA
    - name: perform task on first node if not selected
      shell: another echo
      when: "{{ hn.stdout | last | last }} == 1 and firstNode is not defined"

    - name: perform task on secondary node if not selected
      shell: another echo
      when: "{{ hn.stdout | last | last }} == 2 and secondNode is not defined"

    - name: perform task on third node if not selected
      shell: another echo
      when: "{{ hn.stdout | last | last }} == 3 and thirdNode is not defined"