什么是 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
的剧本?
评论中的答案:
你为什么使用
connection: local
?这意味着在本地 ansible 主机上运行命令。
您应该连接到目标主机并在那里执行lxd_container
模块。