一个主机一个接一个地执行整个Yaml任务文件

时间:2019-05-15 08:44:47

标签: ansible

我想运行test.yaml,其中包含许多任务。我希望清单中的第一个主机在运行到下一个主机之前,先运行test.yaml中的所有任务。但是实际结果是test.yml中的每个任务一次都在所有三个主机上运行。如何实现此功能?

这是我的库存

[host1]
192.168.1.1
192.168.1.2
192.168.1.3

这是我的任务文件

---
# test.yaml
- name: task1
  shell: echo task1

- name: task2
  shell: echo task2

- name: task3
  shell: echo task3

这就是我将任务文件包含在剧本中的方式

- name: Multiple machine loops include
  include: test.yaml
  delegate_to: "{{item}}"
  loop: "{{ groups['host1'] }}"

实际结果是

TASK [Multiple machine loops include] **********************************************************************************************************************************************************
included: /home/learn/main.yml for 192.168.1.1, 192.168.1.2, 192.168.1.3 => (item=192.168.1.1)
included: /home/learn/main.yml for 192.168.1.1, 192.168.1.2, 192.168.1.3 => (item=192.168.1.2)
included: /home/learn/main.yml for 192.168.1.1, 192.168.1.2, 192.168.1.3 => (item=192.168.1.3)


TASK [task1] *********************************************************************************************************************************************************
ok: [192.168.11.1]
ok: [192.168.11.2]
ok: [192.168.11.3]

TASK [task2] *********************************************************************************************************************************************************
changed: [192.168.11.1]
changed: [192.168.11.2]
changed: [192.168.11.3]

TASK [task3] ******************************************************************************************************************************************************
changed: [192.168.11.1]
changed: [192.168.11.2]
changed: [192.168.11.3]

TASK [task1] *******************************************************************************************************************************************************
ok: [192.168.11.1]
ok: [192.168.11.2]
ok: [192.168.11.3]

TASK [task2] ********************************************************************************************************************************************************
changed: [192.168.11.1]
changed: [192.168.11.2]
changed: [192.168.11.3]

TASK [task3] *********************************************************************************************************************************************************
changed: [192.168.11.1]
changed: [192.168.11.2]
changed: [192.168.11.3]

TASK [task1] *********************************************************************************************************************************************************
ok: [192.168.11.1]
ok: [192.168.11.2]
ok: [192.168.11.3]

TASK [task2] ********************************************************************************************************************************************************
changed: [192.168.11.1]
changed: [192.168.11.2]
changed: [192.168.11.3]

TASK [task3] *********************************************************************************************************************************************************
changed: [192.168.11.1]
changed: [192.168.11.2]
changed: [192.168.11.3]

PLAY RECAP ***********************************************************************************************************************************************************
192.168.11.1             : ok=12   changed=6    unreachable=0    failed=0   
192.168.11.2             : ok=12   changed=6    unreachable=0    failed=0   
192.168.11.3              : ok=12   changed=6    unreachable=0    failed=0 

我期望的是:

TASK [task1]  ***********************************************************************************************************************************************************
changed: [192.168.1.1]

TASK [task2]  ***********************************************************************************************************************************************************
changed: [192.168.1.1]

TASK [task3]  ***********************************************************************************************************************************************************
changed: [192.168.1.1]



TASK [task1]  ***********************************************************************************************************************************************************
changed: [192.168.1.2]

TASK [task2]  ***********************************************************************************************************************************************************
changed: [192.168.1.2]

TASK [task3]  ***********************************************************************************************************************************************************
changed: [192.168.1.3]


TASK [task1]  ***********************************************************************************************************************************************************
changed: [192.168.1.3]

TASK [task2]  ***********************************************************************************************************************************************************
changed: [192.168.1.3]

TASK [task3]  ***********************************************************************************************************************************************************
changed: [192.168.1.3]

2 个答案:

答案 0 :(得分:1)

您要执行的操作是在一组主机上依次执行一组任务。弗拉基米尔的答案指出了为什么您当前的实现无法满足您的要求。

您可以通过包含和循环来完成此操作(如果由于特定原因确实需要,请参见下文),但是IMO的最佳方法是在游戏中将serial用作described in the documentation for rolling upgrades < / p>

对于下面的两个示例,我创建了一个“假” inventory文件,其中3个声明的主机都使用本地连接类型

[my_group]
host1 ansible_connection=local
host2 ansible_connection=local
host3 ansible_connection=local

连续运行(首选)

这是test.yml连续运行的剧本

---
- name: Serial run demo 
  hosts: my_group
  serial: 1

  tasks:
    - name: task1
      shell: echo task 1

    - name: task2
      shell: echo task 2

    - name: task3
      shell: echo task 3

结果

$ ansible-playbook -i inventory test.yml 

PLAY [Serial run demo] ******************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************
ok: [host1]

TASK [task1] ****************************************************************************************************************
changed: [host1]

TASK [task2] ****************************************************************************************************************
changed: [host1]

TASK [task3] ****************************************************************************************************************
changed: [host1]

PLAY [Serial run demo] ******************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************
ok: [host2]

TASK [task1] ****************************************************************************************************************
changed: [host2]

TASK [task2] ****************************************************************************************************************
changed: [host2]

TASK [task3] ****************************************************************************************************************
changed: [host2]

PLAY [Serial run demo] ******************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************
ok: [host3]

TASK [task1] ****************************************************************************************************************
changed: [host3]

TASK [task2] ****************************************************************************************************************
changed: [host3]

TASK [task3] ****************************************************************************************************************
changed: [host3]

PLAY RECAP ******************************************************************************************************************
host1                      : ok=4    changed=3    unreachable=0    failed=0   
host2                      : ok=4    changed=3    unreachable=0    failed=0   
host3                      : ok=4    changed=3    unreachable=0    failed=0

包括运行(如果确实需要,可以选择)

如果您确实需要使用include和委托给一组主机,这仍然可能,但是您需要:

  • 要更改包含的文件,请为每个带有变量的任务添加delegate_to
  • 要在播放包含内容的剧本中定位单个主机,而不要像问题中演示的那样定位主机组。

请注意,在您的问题中,您使用的是include,已经宣布将弃用(请参阅module documentation上的注释)。您最好使用include_tasks

的所有include_*import_*替换模块

这是test_include.yml文件

---
- name: task1
  shell: echo task 1
  delegate_to: "{{ delegate_host }}"

- name: task2
  shell: echo task 2
  delegate_to: "{{ delegate_host }}"

- name: task3
  shell: echo task 3
  delegate_to: "{{ delegate_host }}"

这是test.yml剧本:

---
- name: Include loop demo
  hosts: localhost
  gather_facts: false

  tasks:
    - name: Include needed tasks for each hosts 
      include_tasks: test_include.yml
      loop: "{{ groups['my_group'] }}"
      loop_control:
        loop_var: delegate_host

结果

$ ansible-playbook -i inventory test.yml 

PLAY [Include loop demo] *********************************************************************

TASK [Include needed tasks for each hosts] ***************************************************
included: /tmp/testso/test_include.yml for localhost
included: /tmp/testso/test_include.yml for localhost
included: /tmp/testso/test_include.yml for localhost

TASK [task1] *********************************************************************************
changed: [localhost -> host1]

TASK [task2] *********************************************************************************
changed: [localhost -> host1]

TASK [task3] *********************************************************************************
changed: [localhost -> host1]

TASK [task1] *********************************************************************************
changed: [localhost -> host2]

TASK [task2] *********************************************************************************
changed: [localhost -> host2]

TASK [task3] *********************************************************************************
changed: [localhost -> host2]

TASK [task1] *********************************************************************************
changed: [localhost -> host3]

TASK [task2] *********************************************************************************
changed: [localhost -> host3]

TASK [task3] *********************************************************************************
changed: [localhost -> host3]

PLAY RECAP ***********************************************************************************
localhost                  : ok=12   changed=9    unreachable=0    failed=0

答案 1 :(得分:0)

' include '未委派。引用自Delegation

  

请注意,委派所有任务,调试,add_host, include 等总是在控制器上执行是没有意义的。

要查看发生了什么,请运行下面的简化剧本

- hosts: localhost
  tasks:
    - name: Multiple machine loops include
      include: test.yaml
      delegate_to: "{{ item }}"
      loop: "{{ groups['host1'] }}"

带有' test.yaml '

- debug:
    msg: "{{ inventory_hostname }} {{ item }}"

例如,使用库存

host1:
  hosts:
    test_01:
    test_02:
    test_03:

以下摘要输出显示,尽管该任务已委派给另一台主机,但该任务仍在localhost运行。

TASK [debug] 
ok: [localhost] => {
    "msg": "localhost test_01"
}
TASK [debug] 
ok: [localhost] => {
    "msg": "localhost test_02"
}
TASK [debug] 
ok: [localhost] => {
    "msg": "localhost test_03"
}
PLAY RECAP 
localhost            : ok=6    changed=0    unreachable=0    failed=0   

解决方案是将“ delegate_to ”移至所包含的任务。下面的游戏

- hosts: localhost
  tasks:
    - name: Multiple machine loops include
      include: test.yaml
      loop: "{{ groups['host1'] }}"

附带的任务

- command: hostname
  register: result
  delegate_to: "{{ item }}"
- debug: var=result.stdout

给予(节略):

TASK [command] 
changed: [localhost -> test_01]
TASK [debug] 
ok: [localhost] => {
    "result.stdout": "test_01.example.com"
}
TASK [command] 
changed: [localhost -> test_02]
TASK [debug] 
ok: [localhost] => {
    "result.stdout": "test_02.example.com"
}
TASK [command] 
changed: [localhost -> test_03]
TASK [debug] 
ok: [localhost] => {
    "result.stdout": "test_03.example.com"
}
PLAY RECAP 
localhost              : ok=9    changed=3    unreachable=0    failed=0