Using ansible to modify ip address on system

4,267 views
Skip to first unread message

Gerald Spencer

unread,
Dec 30, 2016, 4:37:26 PM12/30/16
to Ansible Project
Okay, we've ran out of ip addresses, and got a larger subnet from our ip. I am currently in the process of switching them over, and decided it would be nice to do this with ansible, but I have ran into a little snag. 

Currently I have:

    - shell: "ifconfig | grep -B1 {{ old_address }}| grep -E '^(\\w+)[[:space:]]' | sed 's/\\s.*$//'"
      args:
        executable: /bin/bash
      register: network_interface

    - template:
        src: files/linux_network_interfaces.j2
        dest: /etc/network/interfaces
        owner: root
        group: root
        mode: '0644'
      become: true
      when: item.key == inventory_hostname
      with_dict: "{{ new_addresses }}"

    - shell: "ifdown {{ network_interface.stdout }} && ifup {{ network_interface.stdout }}"
      args:
        executable: /bin/bash
      become: true

But it hangs in the last shell task... since the IP address was changed. 

Is there a way I can tell this task to just run, then use set_fact to modify each host to its new IP to ping and make sure it worked, then I'll move forward and modify our DNS records to match the new hosts..

Gerald Spencer

unread,
Dec 30, 2016, 11:34:19 PM12/30/16
to Ansible Project
Solved it via running the command as "fire and forget" with async and pool, using a bash command in the background. Then modifying the ansible_ssh_host to the new IP address, and waiting for it port 22 to come up on that host.

    - name: Grab the network interface that the old IP is using and store it.
      shell: "ifconfig | grep -B1 {{ old_address_start }}| grep -E '^(\\w+)[[:space:]]' | sed 's/\\s.*$//'"
      args:
        executable: /bin/bash
      register: network_interface

    - name: Pushout new /etc/network/interface to hosts
      template:
        src: files/linux_network_interfaces.j2
        dest: /etc/network/interfaces
        owner: root
        group: root
        mode: '0644'
      become: true
      when: item.key == inventory_hostname
      with_dict: "{{ new_addresses }}"

    - name: Reload the hosts' network interface
      shell: "(sleep 1; ifdown {{ network_interface.stdout }} && ifup {{ network_interface.stdout }}) &"
      args:
        executable: /bin/bash
      become: true
      async: 100
      poll: 0
    
    - name: Change ansible's ip address for each host
      set_fact:
        ansible_ssh_host: "{{ item.value.address }}"
      when: item.key == inventory_hostname
      with_dict: "{{ new_addresses }}"

    - name: Wait for the hosts' network interface to come back up
      local_action:
        module: wait_for
        host: "{{ ansible_ssh_host }}"
        port: 22
        delay: 10
        state: started
      register: wait_result

    - name: Modify all of the route53 records for ip changes that succeeded
      local_action:
        module: route53
        command: create
        zone: xxxxx
        record: "{{ item.key }}"
        type: A
        value: "{{ item.value.address }}"
        overwrite: true 
      when: item.key == inventory_hostname and wait_result|succeeded
      with_dict: "{{ new_addresses }}"

Michael Lee

unread,
May 22, 2018, 5:59:37 PM5/22/18
to Ansible Project
Hi, I was wondering if you could provide us with the j2 template for your network interface?  If not, what are the basics that you used for it?

Thank you!
Reply all
Reply to author
Forward
0 new messages