从ansible创建VM时遇到一些问题

时间:2020-05-26 08:14:13

标签: python ansible

我正在使用Ansible版本2。 2.9.9和Python版本3.6.9;并且我也安装了请求模块,但是每次运行代码时,由于Mo模块名称请求的错误,我也将ansible_pythin_interpreter也更改为python 3,但它没有引起我的注意。请仔细阅读我的代码,并在此处为我提供帮助。

Traceback (most recent call last):
  File "/tmp/ansible_vmware_guest_payload_evshxlga/ansible_vmware_guest_payload.zip/ansible/module_utils/vmware.py", line 24, in <module>
    import requests
ImportError: No module named 'requests'

    "msg": "Failed to import the required Python library (requests) on Python /user/bin/python. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"

我也上传了我的代码

- hosts: vm
 # tasks:
#connection: local
  #gather_facts: no
  vars_prompt:
   - name: myuser
     prompt: Enter the UserName
     private: no
   - name: mypass
     prompt: Enter the Password
   - name: vc_name
     prompt: Enter the vCenter/ESXi HostName
     private: no
   - name: guest_name
     prompt: Enter the Guest VM Name
     private: no
   - name: hdd
     prompt: Enter the HardDrive Space you want to use
     private: no   
   - name: ram
     prompt: Enter the RAM in MB
     private: no
   - name: cpu
     prompt: Enter the number of processor you want to use
     private: no
   - name: ip_addr
     prompt: Enter the Ip address you want to specify
     private: no
   - name: os_system
     private: no
     prompt: |
      Which os do you want to use?
      1- Windows Server 
      2- CentOS_7
      3- CentOs_8
      4- Ubuntu
      5- Others
     tasks:  
      set_fact: os_system == "Win server 2012.iso"
      when: (os_system == "1")
      set_fact: os_system == "CentOS-7-x86_64-Minimal-1804.iso"
      when: (os_systen == "2")
      set_fact: os_system == "CentOS-8-x86_64-1905-dvd1.iso"
      when: (os_system == "3")
      set_fact : os_system == "ubuntu-16.04.6-server-amd64.iso"
      when: (os_system == "4")


  remote_user: root

  tasks:
#  - name: Create a VM
     delegate_to: localhost

  - vmware_guest:
     hostname: "{{ vc_name }}"
     username: "{{ myuser }}"
     password: "{{ mypass }}"
     validate_certs: no
     folder: /DC1/vm/
     name: "{{guest_name}}"
     state: poweredon
     guest_id: "{{guest_name}}"
    # This is hostname of particular ESXi server on which user wants VM to be deployed
     esxi_hostname: "{{ vc_name }}"
     disk:
     - size_gb: "{{hdd}}"
       type: thin
       datastore: datastore1
     hardware:
       memory_mb: "{{ram}}"
       num_cpus: "{{cpu}}"
       scsi: paravirtual
     networks:
     - name: VM Network
      # mac: aa:bb:dd:aa:00:14
       ip: "{{ip_addr}}"
       netmask: 255.255.255.0
       device_type: vmnic0
     wait_for_ip_address: yes
     cdrom:
      type: iso
      iso_path: "vmfs/volumes/datastore1/Iso/{{os_system}}"  

2 个答案:

答案 0 :(得分:0)

您必须安装请求库。您只需在CMD或终端中输入此命令即可安装它。

val x = if(a) "add b" else "add c"

它将修复pip install requests

答案 1 :(得分:0)

此问题已解决,我在我的剧本中添加了proxy_to:localhost并解决了导入错误:请求模块失败的问题

相关问题