Ansible loops for assigning sequential integers as

2019-06-10 01:02发布

问题:

I'm new to Ansible. I have the following playbook that changes the hostname for a remote server:

---
- hosts: dbservers
  remote_user: testuser1
  become: yes
  become_method: sudo

  vars: 
    LOCAL_HOSTNAME: 'db01'
    LOCAL_DOMAIN_NAME: 'ansibletest.com'

  tasks:
    # Checks and removed the existing occurences of <IP hostname FQDN> from /etc/hosts
    - name: Remove occurences of the existing IP
      lineinfile: dest=/etc/hosts
                  regexp='{{ hostvars[item].ansible_default_ipv4.address }}'
                  state=absent
      when: hostvars[item].ansible_default_ipv4.address is defined
      with_items: "{{ groups['dbservers'] }}"

    #  Adds the IP in the format  <IP hostname FQDN> to /etc/hosts
    - name: Add the IP and hostname to the hosts file
      lineinfile: dest=/etc/hosts
                  regexp='.*{{ item }}$'
                  line="{{ hostvars[item].ansible_default_ipv4.address }} {{ LOCAL_HOSTNAME }} {{ LOCAL_HOSTNAME }}.{{ LOCAL_DOMAIN_NAME }}"
                  state=present
      when: hostvars[item].ansible_default_ipv4.address is defined
      with_items: "{{ groups['dbservers'] }}"

    - name: Remove HOSTNAME occurences from /etc/sysconfig/network
      lineinfile: dest=/etc/sysconfig/network
                  regexp='^HOSTNAME'
                  state=absent
      when: hostvars[item].ansible_default_ipv4.address is defined
      with_items: "{{ groups['dbservers'] }}"

    - name: Add new HOSTNAME to /etc/sysconfig/network
      lineinfile: dest=/etc/sysconfig/network
                  regexp='^HOSTNAME='
                  line="HOSTNAME={{ LOCAL_HOSTNAME }}.{{ LOCAL_DOMAIN_NAME }}"
                  state=present
      when: hostvars[item].ansible_default_ipv4.address is defined
      with_items: "{{ groups['dbservers'] }}"

    - name: Set up the hostname
      hostname: name={{ LOCAL_HOSTNAME }}.{{ LOCAL_DOMAIN_NAME }}

In this example, LOCAL_HOSTNAME is already assigned a value of db01. And in this scenario, the dbservers group has only one server:

[dbservers]
192.168.1.93

However, I also have 2 other servers that are designated to be webservers:

[webservers]
192.168.1.95
192.168.1.96

[dbservers]
192.168.1.93

The aim is to name them as web01.domain, web02.domain and so on.

As per the docs it seems that this could be achieved by using with_sequence.

My question is, is it possible (in Ansible) to use 2 variables in loops? Something along the lines of the pseudo-code below:

i=1
for host in webservers:
   open host(/etc/hosts):
       add "IP<space>HOSTNAME{i}<space>"<space>"HOSTNAME{i}.FQDN"
       i++

Could this be achieved using playbooks or am I approaching the issue in an wrong way?

回答1:

Generate indexed hostname first, define it as hostfact and use it later to fill other servers' hosts files.

- hosts: webservers
  gather_facts: no
  tasks:
    - set_fact:
        indexed_hostname: "{{ 'web{0:02d}'.format(play_hosts.index(inventory_hostname)+1) }}"

- hosts: dbservers
  gather_facts: no
  tasks:
    - debug:
        msg: "{{ hostvars[item].indexed_hostname }}"
      with_items: "{{ groups['webservers'] }}"

Also there is such thing as with_indexed_items.