【问题标题】:How to edit /etc/netplan/50-cloud-init.yaml with Ansible in LXC container如何在 LXC 容器中使用 Ansible 编辑 /etc/netplan/50-cloud-init.yaml
【发布时间】:2021-11-25 08:15:28
【问题描述】:

我有一个创建容器的剧本:

- name: Create LXD Container #1
  become: True
  lxd_container:
    name: "{{ lxc_CT_NAME }}"
    state: started
    source:
      type: image
      mode: pull
      server: "{{ lxc_image_source }}"
      protocol: simplestreams
      alias: "{{ lxc_container_distro }}"
    profiles: "{{ lxc_profiles }}"
    wait_for_ipv4_addresses: true
    timeout: 10
  when: lxc_lxd_host is defined
  register: container_created

创建我的容器后有配置文件 /etc/netplan/50-cloud-init.yaml 如:

network:
    version: 2
    ethernets:
        eth0:
            dhcp4: true

我的目标,在 LXC 容器中编辑这个文件,并添加新的配置:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      addresses:
        - 192.168.100.119/24
      gateway4: 192.168.100.11
      mtu: 1400
      nameservers:
          addresses:
          - 208.**.222.***
          - 208.**.***.220

我尝试使用模块container_command:container_config:,但无法准确理解这些模块的代码。

您能否提供建议,如何使用 Ansible 将文件编辑到 LXC 容器中?

【问题讨论】:

    标签: ansible lxc


    【解决方案1】:

    一种方法是将信息添加到 cloud-init 以配置容器:

    - name: Create LXD Container #1
        become: True
        lxd_container:
        name: "{{ lxc_CT_NAME }}"
        state: started
        source:
          type: image
          mode: pull
          server: "{{ lxc_image_source }}"
          protocol: simplestreams
          alias: "{{ lxc_container_distro }}"
        profiles: "{{ lxc_profiles }}"
        config:
          user.network-config: |
          version: 2
          renderer: networkd
          ethernets:
            eth0:
              dhcp4: false
              dhcp6: false
              addresses:
                - 192.168.100.119/24
              routes:
                - to: 0.0.0.0/24
                  via: 192.168.100.11
              nameservers:
                search:
                  - example.org (YOUR-DOMAIN-SUFFIX)
                addresses:
                  - 192.168.100.1 (YOUR-DNS-SERVER)
        wait_for_ipv4_addresses: true
        timeout: 10
      when: lxc_lxd_host is defined
      register: container_created
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-31
      • 1970-01-01
      • 2017-04-23
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多