【问题标题】:How to configure LXD container devices using saltstack "lxd_container.present" function?如何使用 saltstack \"lxd_container.present\" 函数配置 LXD 容器设备?
【发布时间】:2022-09-29 10:35:56
【问题描述】:

我有以下在职的Salt状态文件:1)下载图像2)从该图像创建一个容器,3)之后添加一个nic。

这些状态使用以下设置工作:

  • 盐SSH版本 3004
  • Python3.9.7
  • LXC(快照)版本 5.0.0
  • PyLXD2.3.0 版
  • Linux的aarch64
---
# Create Penguin Container
#---

get_focal:
  lxd_image.present:
    - name: \'focal\'
    - source:
        type: simplestreams
        server: https://cloud-images.ubuntu.com/releases 
        name: \'20.04\'

create_penguin:
  lxd_container.present:
    - name: penguin
    - profiles: [\'default\']
    - source: \'focal\'
    - running: true
    - devices:
    ### I want to create NIC here. ###

add_nic_card:
  cmd.run: 
    - name: |
        lxc config device add penguin eth0 nic nictype=bridged parent=br0

我需要结合状态#2 和#3,以便 nic 与容器同时创建。根据official documentation,这应该是可能的。但是,我无法正确使用语法,错误代码也无济于事。

我尝试了以下多种变体:

变化 1


create_penguin:
  lxd_container.present:
    - name: penguin
    - profiles: [\'default\']
    - source: \'focal\'
    - running: true
    - devices:
        eth0: {
          type: \"nic\",
          nictype: \"bridged\",
          parent: \"br0\" }

变体 2

create_penguin:
  lxd_container.present:
    - name: penguin
    - profiles:
      - default
    - source: \'focal\'
    - running: true
    - devices:
        eth0:
          type: nic
          nictype: bridged
          parent: br0

变体 2 产生以下错误:

----------
          ID: create_penguin
    Function: lxd_container.present
        Name: penguin
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File \"/var/tmp/.ubuntu_a31665_salt/pyall/salt/state.py\", line 2179, in call
                  ret = self.states[cdata[\"full\"]](
                File \"/var/tmp/.ubuntu_a31665_salt/pyall/salt/loader/lazy.py\", line 149, in __call__
                  return self.loader.run(run_func, *args, **kwargs)
                File \"/var/tmp/.ubuntu_a31665_salt/pyall/salt/loader/lazy.py\", line 1201, in run
                  return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
                File \"/var/tmp/.ubuntu_a31665_salt/pyall/salt/loader/lazy.py\", line 1216, in _run_as
                  return _func_or_method(*args, **kwargs)
                File \"/var/tmp/.ubuntu_a31665_salt/pyall/salt/loader/lazy.py\", line 1249, in wrapper
                  return f(*args, **kwargs)
                File \"/var/tmp/.ubuntu_a31665_salt/pyall/salt/states/lxd_container.py\", line 235, in present
                  __salt__[\"lxd.container_create\"](
                File \"/var/tmp/.ubuntu_a31665_salt/pyall/salt/loader/lazy.py\", line 149, in __call__
                  return self.loader.run(run_func, *args, **kwargs)
                File \"/var/tmp/.ubuntu_a31665_salt/pyall/salt/loader/lazy.py\", line 1201, in run
                  return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
                File \"/var/tmp/.ubuntu_a31665_salt/pyall/salt/loader/lazy.py\", line 1216, in _run_as
                  return _func_or_method(*args, **kwargs)
                File \"/var/tmp/.ubuntu_a31665_salt/pyall/salt/modules/lxd.py\", line 691, in container_create
                  container_device_add(name, dn, **dargs)
                File \"/var/tmp/.ubuntu_a31665_salt/pyall/salt/modules/lxd.py\", line 1431, in container_device_add
                  return _set_property_dict_item(container, \"devices\", device_name, kwargs)
                File \"/var/tmp/.ubuntu_a31665_salt/pyall/salt/modules/lxd.py\", line 3544, in _set_property_dict_item
                  raise SaltInvocationError(\"path must be given as parameter\")
              salt.exceptions.SaltInvocationError: path must be given as parameter
     Started: 09:43:31.807609
    Duration: 5147.141 ms
     Changes:   
----------

    标签: salt-stack lxc lxd


    【解决方案1】:

    我从lxd-formula 得到提示,如文档中所述,我们需要以 YAML 格式向devices: 提供一个字典。因此,不需要大括号 { .. }

    之后,我发现以下语法可以正常工作:

    create_penguin:
      lxd_container.present:
        - name: penguin
        - profiles: ['default']
        - source: 'focal'
        - running: true
        - devices:
            eth0:
              type: nic
              nictype: bridged
              parent: br0
            # another network interface
            eth1:
              type: nic
              nictype: bridged
              parent: br0
    

    作为参考,我的设置:

    component version
    Salt 3003.3
    Python 3.8.10
    dist ubuntu 20.04 focal
    lxc (apt) 4.0.9
    pylxd 2.3.1

    Saltstack 的输出(已从状态中删除 eth1):

    ----------
              ID: create_penguin
        Function: lxd_container.present
            Name: penguin
          Result: True
         Comment: 1 changes
         Started: 12:38:58.041506
        Duration: 27277.86 ms
         Changes:   
                  ----------
                  devices:
                      ----------
                      eth0:
                          Added device "eth0"
    

    【讨论】:

    • 只是好奇,你的设置是什么@sthadri_c。我已经在 OP 中包含了关于我的注释。
    • 我更新了我的设置详细信息。
    • 我将您的答案标记为正确,因为它似乎是您版本的官方文档中提供的答案。我仍然不得不做一个工作。
    【解决方案2】:

    以下解决方案为原始帖子提供了令人满意的解决方法。我没有添加单独的节,而是创建了一个名为“natted”的配置文件并将其添加到容器中。虽然它适用于我的 Salt 版本,但它比文档中的“官方”答案更不明确/透明。

    ---
    # Create & Start the Penguin Container
    #---
    
    penguin_create_start:
      lxd_container.present:
        - name: penguin
        - source: 'focal'
        - running: true
        - profiles: ['default', 'natted']
    ...
    
    

    配置文件看起来像这样:

    ---
    # equivalent to --> 'lxc config device add penguin eth0 nic nictype=bridged parent=br0'
    #---
    
      config: {}
      description: Adds eth1 to 'lxdbr0' bridge
      devices:
        eth1:
          nictype: bridged
          parent: lxdbr0
          type: nic
      name: natted
    

    【讨论】:

      猜你喜欢
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      相关资源
      最近更新 更多