title: How to loop over hostnames or IPs in ansible
body: <p>Am trying to set up and configure pacemaker using Ansible playbook in Ubuntu 14.04. </p>
  
  <p>As of now, am just testing it in one node. Hence in hosts file I just kept the information of that node</p>
  
  <pre><code>[hostname]
  1.2.3.4   ansible_ssh_private_key_file=/home/ubuntu/test.pem
  </code></pre>
  
  <p>In the playbook YAML file am trying to install as well as configure pacemaker</p>
  
  <pre><code>- hosts: all
    sudo: yes
    tasks:
      - name: install pacemaker
        apt: name=pacemaker state=present
      - name: install corosync
        apt: name=corosync state=present
      - name: install fence-agents
        apt: name=fence-agents state=present
      #- copy: src=corosync_start dest=/etc/default/corosync
      #- shell: update-rc.d -f pacemaker remove
      #- shell: update-rc.d pacemaker start 50 1 2 3 4 5 . stop 01 0 6 .
  </code></pre>
  
  <p>Installation is taking place properly in my node. But for configuration, I need to edit /etc/corosync/corosync.conf in which i need to specify my Host address in place of <code>bindnetaddress</code>.</p>
  
  <p>Say I am having more than one entry under [hostname] section - is there any way in Ansible that I can loop over them in my YAML file?</p>
  
  <p>I am trying to use sed command to replace the IP. Can you please explain how to loop over or print the IPs?</p>
  
  <p>I tried like this</p>
  
  <pre><code>- hosts: all
    sudo: yes
    tasks:
      - debug: msg = "{{ ansible_hostname }}"
      - name: Test
        task: {% for host in groups['app_servers'] %}
              {{host}}
              {% endfor %}
  </code></pre>