如何创建一个 Ansible playbook,一次性创建并配置 proxmox lxc 容器

如何创建一个 Ansible playbook,一次性创建并配置 proxmox lxc 容器

我设法使用 ansible 社区community.general.proxmox创建了一个角色,该角色在我的 proxmox 集群上创建了一个 lxc 机器。问题是它无法等待机器启动。它说没有名为 的虚拟机whatever-name,但如果我第二次运行它,它就会完全成功。请注意,我使用 dhcp 来分配 IP,并且在机器从我的 dhcp 服务器获取 ip 后,可以通过名称访问它,因为我的 dhcp 也充当 DNS 服务器,并且我可以通过主机名毫无问题地访问我的所有 lxc 机器。

这是创建机器的角色,然后在最后一步失败:

---
- name: bla
  debug:
    msg: "{{ api_host }}"
- name: Create new container with minimal options defining network interface with DHCP
  # delegate_to: localhost
  connection: local
  community.general.proxmox:
    api_host: "{{ api_host }}" # The proxmox instance
    api_user: root@pam
    api_password: "{{ proxmox_api_key }}" #Make sure to read from a vault
    node: "{{ proxmox_node }}"
    cores: 1
    disk: "{{ disk_size }}" # GB
    memory: "{{ memory }}"
    nameserver: 192.168.0.110
    password: "{{ password }}"
    hostname: "{{ container_name }}"
    unprivileged: no
    pubkey: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
    storage: "{{ container_storage }}"
    ostemplate: "{{ container_os_template }}"
    netif: '{"net0":"name=eth0,gw=192.168.0.1,ip=dhcp,bridge=vmbr0"}'
    features:
      - nesting=1
    description: created with ansible
    state: present
- name: Start the container
  # delegate_to: localhost
  connection: local
  community.general.proxmox:
    api_host: "{{ api_host }}" # The proxmox instance
    api_user: root@pam
    api_password: "{{ proxmox_api_key }}" #Make sure to read from a vault
    node: "{{ proxmox_node }}"
    hostname: "{{ container_name }}"
    state: started
    unprivileged: no
- name: Wait for the container to start
  connection: local
  wait_for:
    host: "{{ container_name }}.home"
    port: 22
    sleep: 3
    connect_timeout: 5
    timeout: 60

我怎样才能在一次运行中完成这项工作?

相关内容