1

My ansible script is made for deploying vm's in virsh and installing them with cobbler. Using a IP address fixed on mac address. To do this the mac address is needed. But I am having a hard time getting the output of the grep command to register as variable mac_address. The variable stay's undefined.

When running the command ad-hoc it works fine:

[root@pxecobbler test]# ansible pirate.rum.local -m shell -a 'virsh 
domiflist testvm3 | grep -o -E "([0-9a-f]{2}:){5}([0-9a-f]{2})"' 
pirate.rum.local | SUCCESS | rc=0 >>
52:54:00:ec:a5:49

In the ansible playbook it fails,the variable stays undefined:

- name: get MAC adres new VM
          shell: >
                  virsh domiflist {{ item.key }} | grep -o -E "([0-9a-f]{2}:){5}([0-9a-f]{2})"
          with_dict: "{{ guests }}"
          register: mac_address
        - debug:
          msg: "{{ mac_address }}"

error message:

fatal: [pirate.rum.local]: FAILED! => {"msg": "The task includes an 
option with an undefined variable. 
The error was: 'dict object' has no attribute 'stdout_lines'\n\nThe 
error appears to have been in '/root/virsh-create-vm/virsh-create- 
vm.yml': line 47, column 7,
but may\nbe elsewhere in the file depending on the exact syntax 
problem.\n\nThe offending line appears to be:\n\n      
 register: mac_address\n    - debug: var=\"{{ mac_address.stdout_lines }}\"\n      
^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. 
Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      
- {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n\nexception type: 
<class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'dict object' has no attribute 'stdout_lines'"}
to retry, use: --limit @/root/virsh-create-vm/virsh-create-vm.retry

whole script:


- name: create VMs
  hosts: pirate.rum.local
  become: true
  vars_files:
    - vms.yml

  tasks:
    - name: get VM disks
      command: "ls {{ vm_location }}"
      register: disks
      changed_when: "disks.rc != 0"

    - name: create disk
      command: >
               qemu-img create -f qcow2 -o preallocation=metadata {{ vm_location }}/{{ item.key }}.qcow2 10G
      when: item.key not in disks.stdout
      with_dict: "{{ guests }}"

    - name: get list of VMs
      virt:
        command: "list_vms"
      register: vms

    - name: create vm
      command: >
                virt-install --name {{ item.key }}
               --memory {{ item.value.mem }} --vcpus {{ item.value.cpus }}
                --pxe --network network=nat,model=virtio \
                --disk {{ vm_location }}/{{ item.key }}.qcow2
               --noautoconsole --os-variant {{ item.value.os_type }}
      when: item.key not in vms.list_vms
      with_dict: "{{ guests }}"

    - name: get MAC adres new VM
      shell: >
              virsh domiflist {{ item.key }} | grep -o -E "([0-9a-f]{2}:){5}([0-9a-f]{2})"
      with_dict: "{{ guests }}"
      register: mac_address
    - debug:
        msg: "{{ mac_address }}"


  # new tasks sequence on PXECobbler
- name: Register new VM's on Cobbler PXE host
  hosts: pxecobbler.rum.local
  vars_files:
    - vms.yml

  tasks:
    - name: register new VM on PXE cobbler host mac {{ mac_address.stdout }}
      shell: >
              cobbler system add --name={{ item.key }} --profile=CentOS-7-x86_64  --interface=eth0 --mac={{ mac_address.stdout }}  --ip-address={{ item.value.ip }} --netboot-enabled=1 --static=1
      with_dict: "{{ guests }}"  
Ewt
  • 23
  • 2
  • 6
  • Showing us the exact error/output you see would help. Also you seem to be missing a : in your debug task? `msg"{{ mac_address }}"` should be `msg: "{{ mac_address }}"` right? – Zoredache Apr 13 '18 at 19:07
  • I added the error message, and tested with the msg: "{{}}" but the variable was still undefined. – Ewt Apr 14 '18 at 11:12

1 Answers1

0

When you say with_dict it means you're looping. With register: mac_address you get not a simple usual object but an array filled by your loop.

Try something like this:

  # new tasks sequence on PXECobbler
- name: Register new VM's on Cobbler PXE host
  hosts: pxecobbler.rum.local
  vars_files:
    - vms.yml

  - shell: echo {{ item.changed }} {{ item.stdout }}     # will print: True 00:11:22:33:44
    with_items: "{{ mac_address.results }}"
    register: b
  - debug:
      msg: "{{ b }}"

Next time when you've lost pay special attention to get the really basic tasks correct. Your question would be much simpler if you cited your actual yml as-is (it contained debug: var="{{ mac_address.stdout_lines }}" which you didn't cite).

kubanczyk
  • 13,812
  • 5
  • 41
  • 55