ansible playbook: using multiple variables in loops

0 votes

I'm trying to change the hostnames for a remote server using the following ansible playbook:

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

Here, the LOCAL_HOSTNAME is assigned the value of db01 and dbservers group has one server

[dbservers]
192.168.1.93

But I have 2 more servers assigned to webservers

[webservers]
192.168.1.95
192.168.1.96

[dbservers]
192.168.1.93

I want to change their names to web01.domain and web02domain. I've read the docs and it says that we can do this by using with_sequence. I want to know if I could possibly use 2 variables in loops? like this:

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

Can this be done using playbook or is there any other way to achieve this??

Jul 16, 2018 in Ansible by Damon Salvatore
• 5,510 points
4,829 views

1 answer to this question.

0 votes

Use an indexed hostname, and then define it like hostfact and then use it to fill other servers host 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'] }}"

There this thing with_indexed_items that you should check out

answered Jul 16, 2018 by DareDev
• 6,810 points
That's a very good approach! Thanks

Related Questions In Ansible

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Create directories and download files in Ansible using Ansible playbook

Hey @Yash,  you could either use file module ...READ MORE

answered Jan 24 in Ansible by Cerdin
685 views
0 votes
1 answer

Define variables in Ansible Playbook

Variable names should be letters, numbers, and ...READ MORE

answered Jan 24 in Ansible by Jinha
63 views
+13 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
181 views
0 votes
1 answer
0 votes
1 answer

Using multiple config files using same template in a role in ansible

Go through this include_role module: tasks: - ...READ MORE

answered Jun 5, 2018 in Ansible by DareDev
• 6,810 points
629 views
0 votes
1 answer

Using lxc_container module in ansible

This mostly happens when you've not installed ...READ MORE

answered Aug 10, 2018 in Ansible by DareDev
• 6,810 points
143 views