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,980 points
18,199 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,890 points
That's a very good approach! Thanks

Related Questions In Ansible

0 votes
1 answer

Print multiple env variables using ansible playbook

Hey @Tom, you can use the lookup ...READ MORE

answered Jan 24, 2019 in Ansible by Rick
5,089 views
0 votes
1 answer

How to add multiple lines in a file using Ansible playbook?

Hi@akhtar, You can update your file using lineinfile ...READ MORE

answered Sep 16, 2020 in Ansible by MD
• 95,440 points
24,817 views
0 votes
1 answer

How do I wget a file from web server using shell in the ansible playbook

Hey Ayaan, you could probably use this ...READ MORE

answered Jan 24, 2019 in Ansible by Anushri
3,842 views
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, 2019 in Ansible by Cerdin
5,367 views
+15 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 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
3,458 views
+2 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,890 points
3,988 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,890 points
1,213 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP