简体   繁体   中英

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

$ 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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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