在ansible中一个接一个地执行一个主机的整个yaml任务文件 [英] Execute an entire yaml task file one host after the other in ansible

查看:133
本文介绍了在ansible中一个接一个地执行一个主机的整个yaml任务文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to run test.yaml, which contains a lot of tasks. I expect that the first host in my inventory runs all tasks in test.yaml before moving to the next one. But the actual result is that each task in test.yml is run on all three hosts at a time. How to implement this function?

这是我的清单

[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

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

And this is how I include the task file in my playbook

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

推荐答案

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

What you are trying to do is to play a set of tasks serially on a set of hosts. Vladimir's answer points out why your current implementation cannot achieve your requirement.

您可以通过包含和循环来实现(如果由于特定原因确实需要,请参见下文),但是IMO的最佳方法是在游戏中将serial用作

You could do that with an include and a loop (see below if you really need that for a particular reason), but the best way IMO is to use serial in your play as described in the documentation for rolling upgrades

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

For both example below, I created a "fake" inventory file with 3 declared hosts all using the local connection type

[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 run (alternative if really needed)

If you really need to use include and delegate to a set of hosts, this would still be possible but you need:

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

请注意,在您的问题中,您正在使用include,该版本已经宣布将来将弃用(请参阅 include_tasks

Note that in your question, you are using include which is already announce for future deprecation (see the notes on module documentation). You should prefer all the include_* and import_* replacements modules, in your case include_tasks

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

这篇关于在ansible中一个接一个地执行一个主机的整个yaml任务文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆