什么是“lxc launch ubuntu:new-container”的Ansible等效剧本

时间:2017-06-22 08:22:10

标签: ansible

Ansible等同于lxc launch ubuntu: new-container的剧本。

我可以成功ping我要创建容器的机器,当登录到该机器时,我可以创建一个没有任何问题的容器。当我尝试使用下面的剧本时,我得到以下结果:

尝试1:

- hosts: node0
  tasks:
    - name: Create a started container
      lxd_container:
        name: mycontainer
        state: started
        profiles: ["default"]

结果:

# ansible-playbook play 

PLAY [node0] ***************************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************
ok: [node0]

TASK [Create a started container] ******************************************************************************************************************************************************
fatal: [node0]: FAILED! => {"actions": [], "changed": false, "failed": true, "msg": "unknown source type "}
    to retry, use: --limit @/root/play.retry

PLAY RECAP *****************************************************************************************************************************************************************************
node0                      : ok=1    changed=0    unreachable=0    failed=1  

尝试2:

- hosts: node0
  connection: local
  gather_facts: false

  tasks:
  - name: create a container
    connection: local
    become: false
    lxd_container:
      name: test
      state: started
      source:
        type: image
        mode: pull
        server: https://images.linuxcontainers.org
        protocol: lxd
        alias: "ubuntu/xenial/amd64"
      profiles: ["default"]
      wait_for_ipv4_addresses: false
      timeout: 600

结果:

# ansible-playbook play 

PLAY [node0] ***************************************************************************************************************************************************************************

TASK [create a container] **************************************************************************************************************************************************************
fatal: [node0]: FAILED! => {"actions": [], "changed": false, "failed": true, "msg": "Failed to change ownership of: /var/lib/lxd/containers/test/rootfs"}
    to retry, use: --limit @/root/play.retry

PLAY RECAP *****************************************************************************************************************************************************************************
node0                      : ok=0    changed=0    unreachable=0    failed=1   

尝试3似乎有效但是它似乎下载了一个新图像而不是使用机器上已存在的图像:

# An example for creating a Ubuntu container and install python
- hosts: node0
  connection: local
  tasks:
    - name: Create a started container
      lxd_container:
        name: mycontainer
        state: started
        source:
          type: image
          mode: pull
          server: https://images.linuxcontainers.org
          protocol: lxd
          alias: ubuntu/xenial/amd64
        profiles: ["default"]
        wait_for_ipv4_addresses: true
        timeout: 600

如何编写相当于lxc launch ubuntu: new-container的剧本?

1 个答案:

答案 0 :(得分:1)

评论回答:

  

为什么使用connection: local?这意味着在本地ansible主机上运行命令。

您应该连接到目标主机并在那里执行lxd_container模块。