Ansible playbook looping control flow

↘锁芯ラ 提交于 2019-12-25 09:00:03

问题


I have a playbook that checks the list of installed plugins for 6 jenkins servers.

Here is the hostfile:

[masters]
server1
server2
server3
server4
server5
server6

Here is the task within the playbook that handles obtaining the list of installed plugins:

 - name: Obtaining a list of Jenkins Plugins
   jenkins_script:
     script: 'println(Jenkins.instance.pluginManager.plugins)'
     url: "{{ item }}"
     user: 'admin'
     password: 'password'
   with_items:
     - 'url1'
     - 'url2'
     - 'url3'
     - 'url4'
     - 'url5'
     - 'url6'

This runs perfectly fine, but prints out 36 lists of installed plugins rather than just 6. It appears that the playbook is plugging every url in for each of the hosts, however I'm fairly new to ansible, so does anyone know how to get around this issue?


回答1:


If you have six Jenkins servers named server1-server6, you don't need to make a loop. Just fire jenkins_script task and set hosts pattern to run this task on every server:

---
- hosts: server*
  tasks:
    - name: Obtaining a list of Jenkins Plugins
      jenkins_script:
        script: 'println(Jenkins.instance.pluginManager.plugins)'
        url: 'http://{{ inventory_hostname }}:8080/'
        user: 'admin'
        password: 'password'

This will execute the task on every server once.



来源:https://stackoverflow.com/questions/42493579/ansible-playbook-looping-control-flow

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!