简体   繁体   English

如何在 LXC 容器中使用 Ansible 编辑 /etc/netplan/50-cloud-init.yaml

[英]How to edit /etc/netplan/50-cloud-init.yaml with Ansible in LXC container

I have a playbook to create a container:我有一个创建容器的剧本:

- 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

After creating my containers has configaration file /etc/netplan/50-cloud-init.yaml as:创建我的容器后,配置文件 /etc/netplan/50-cloud-init.yaml 为:

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

My goal, edit this file in LXC container, and add new configuration:我的目标是在 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

I tried to use module container_command: and container_config: but can not understand exactly code of this modules.我尝试使用模块container_command:container_config:但无法准确理解此模块的代码。

Could you please help with advice, how to edit file into LXC container using Ansible?您能否提供建议,如何使用 Ansible 将文件编辑到 LXC 容器中? Thank you!谢谢!

One way is to add the information to cloud-init to configure the container:一种方法是将信息添加到 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM