简体   繁体   English

如何在嵌套的 for 循环中使用 ansible-playbook?

[英]How to use ansible-playbook in a nested for loop?

I am trying to write a script that will automatically reboot servers in our environment.我正在尝试编写一个脚本来自动重启我们环境中的服务器。 We are using ansible-playbook -i rebootlist reboot.yml for rebooting 100 servers at a time as we have around 400+ servers and its needs to reboot in a order.我们正在使用 ansible-playbook -i rebootlist reboot.yml 一次重新启动 100 个服务器,因为我们有大约 400 多台服务器并且需要按顺序重新启动。 So, i came up with this:所以,我想出了这个:

for j in $(cat rebootlist); do for k in $(cat $j); do ansible-playbook -i $k reboot.yml  >> $output; done; done

here,这里,

rebootlist has 4 list of 100 servers. rebootlist 有 4 个 100 个服务器的列表。

$ cat rebootlist
reboot00
reboot02
reboot03
reboot04

I am getting this warning below我在下面收到此警告

[WARNING]: No inventory was parsed, only implicit localhost is available [警告]:没有解析库存,只有隐式本地主机可用

[WARNING]: provided hosts list is empty, only localhost is available. [警告]:提供的主机列表为空,只有 localhost 可用。 Note that the implicit localhost does not match 'all'请注意,隐式本地主机与“全部”不匹配

Thanks RaalK谢谢拉尔克

Let's simplify the data a bit.让我们稍微简化一下数据。 For example, given the files例如,给定文件

shell> cat rebootlist 
reboot00
reboot02

shell> cat reboot00
host000
host099

shell> cat reboot02
host100
host199

In the playbook below create a dynamic group in the first play and use it in the second, eg在下面的剧本中,在第一个剧本中创建一个动态组并在第二个剧本中使用它,例如

shell> cat reboot.yml
- hosts: localhost
  gather_facts: false
  tasks:
    - add_host:
        name: "{{ item }}"
        groups: "{{ group }}"
      loop: "{{ lookup('file', group).splitlines() }}"

- hosts: "{{ group }}"
  gather_facts: false
  tasks:
    - debug:
        msg: "Reboot {{ inventory_hostname }}"

Then iterate the items from the file rebootlist .eg然后从文件rebootlist .eg 中迭代项目

shell> for j in $(cat rebootlist); do ansible-playbook -e group=$j reboot.yml; done

gives (abridged)给(略)

PLAY [localhost] ***********************

TASK [add_host] ************************
ok: [localhost] => (item=host000)
ok: [localhost] => (item=host099)

PLAY [reboot00] ************************

TASK [debug] ***************************
ok: [host000] => 
  msg: Reboot host000
ok: [host099] => 
  msg: Reboot host099

PLAY RECAP *****************************
host000: ok=1    changed=0    unreachable=0
host099: ok=1    changed=0    unreachable=0
localhost: ok=1    changed=0    unreachable=0


PLAY [localhost] ***********************

TASK [add_host] ************************
ok: [localhost] => (item=host100)
ok: [localhost] => (item=host199)

PLAY [reboot02] ************************

TASK [debug] ***************************
ok: [host100] => 
  msg: Reboot host100
ok: [host199] => 
  msg: Reboot host199

PLAY RECAP *****************************
host100: ok=1    changed=0    unreachable=0
host199: ok=1    changed=0    unreachable=0
localhost: ok=1    changed=0    unreachable=0

I would like to suggest a good app to learn and understand unix and linux in better way.我想推荐一个好的应用程序来更好地学习和理解 unix 和 linux。 In this app you can find a good explanation of concepts and diagrams.在这个应用程序中,您可以找到对概念和图表的很好的解释。

[Link] - https://play.google.com/store/apps/details?id=com.kanha.unixlinuxpocketbook [链接] - https://play.google.com/store/apps/details?id=com.kanha.unixlinuxpocketbook

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM