renaming hosts

12 views
Skip to first unread message

Joe

unread,
Mar 21, 2020, 12:07:13 AM3/21/20
to Ansible Project
I'm fairly new to ansible, so I'm still learning it.  Please bare with me.   I have a variable file that defines maps current hostnames of devices to new hostnames.  I'm trying to write a playbook that will re the variable file and change the hostnames of the devices.  So far I haven't had any luck, can some one tell me where I'm going wrong with this?  


The variable file and play book are pasted below

[joec@automation change_hostname]# cat hostname.yml
---
  - host: test13
    newhostname: test-switch1
  - host: jnpr-ans-test
    newhostname: test-switch2

[joec@automation change_hostname]# cat change_hostname.yml
---
- name: Examples of juniper_junos_command
  hosts: juniper
  gather_facts: no

  tasks:
    - include_vars:
        file: hostname.yml
        name: newnames
    - name: Get Facts.
      junos_facts:
        gather_subset: all
      register: facts

    - name: Rename device
      junos_facts:
        hostname: "{{ newnames.namehostname }}"
        when: facts.ansible_facts.ansible_net_hostname == newnames.host
        loop: "{{ newnames }}"

Reply all
Reply to author
Forward
0 new messages