Ansible playbook looping control flow

2019-09-06 05:27发布

问题:

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.



标签: ansible