基于运行linuxcontainers的动态库存管理
lxcible的Python项目详细描述
lxcible公司
基于运行linuxcontainers的动态库存管理
安装
$ pip install lxcible
或来源:
^{pr2}$简介
基于运行linuxcontainers的动态ansible库存。在
当用--list
调用时,lxcible
将根据
当前正在运行的容器。在
使用示例
从包含公钥的映像创建一些容器
/home/ubuntu/.ssh/authorize_keys
:
$ lxc launch bionic-with-pubkey foo Creating fooStarting foo$ lxc launch bionic-with-pubkey bar Creating barStarting bar$ lxc launch bionic-with-pubkey baz Creating bazStarting baz
$ lxc list
+------+---------+----------------------+----------------------------------------------+-----------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+------+---------+----------------------+----------------------------------------------+-----------+-----------+
| bar | RUNNING | 10.153.75.179 (eth0) | fd42:9f54:f06:b4a5:216:3eff:fe0a:a7d4 (eth0) | CONTAINER | 0 |
+------+---------+----------------------+----------------------------------------------+-----------+-----------+
| baz | RUNNING | 10.153.75.163 (eth0) | fd42:9f54:f06:b4a5:216:3eff:fe30:3194 (eth0) | CONTAINER | 0 |
+------+---------+----------------------+----------------------------------------------+-----------+-----------+
| foo | RUNNING | 10.153.75.98 (eth0) | fd42:9f54:f06:b4a5:216:3eff:fed6:ca26 (eth0) | CONTAINER | 0 |
+------+---------+----------------------+----------------------------------------------+-----------+-----------+
使用这三个运行的容器,lxcible --list
生成以下输出:
{"_meta":{"hostvars":{"bar":{"ansible_host":"10.153.75.179"},"baz":{"ansible_host":"10.153.75.163"},"foo":{"ansible_host":"10.153.75.98"}}},"all":["bar","baz","foo"]}
最小测试行动手册,main.yml
:
----hosts:allserial:1gather_facts:Falsetasks:-name:Fetch ssh host keysdelegate_to:localhostlineinfile:path:~/.ssh/known_hostscreate:yesstate:presentline:"{{lookup('pipe','ssh-keyscan-trsa'+ansible_host)}}"-hosts:allgather_facts:Falsetasks:-name:"Testconnectivity"ping:
这将使用ssh keyscan收集ssh主机密钥。这部分将连续进行,因为
lineinfile
不能很好地处理并发写操作(请参见:https://github.com/ansible/ansible/issues/31712)。
第二部分ping主机以测试ssh连接。在
让我们运行剧本:
$ ansible-playbook -u ubuntu -i /usr/local/bin/lxcible main.yml PLAY [all] ****************************************************************************************************************************TASK [Fetch ssh host keys] ************************************************************************************************************#10.153.75.179:22 SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3 changed: [bar -> localhost]PLAY [all] ****************************************************************************************************************************TASK [Fetch ssh host keys] ************************************************************************************************************#10.153.75.163:22 SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3 changed: [baz -> localhost]PLAY [all] ****************************************************************************************************************************TASK [Fetch ssh host keys] ************************************************************************************************************#10.153.75.98:22 SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3 changed: [foo -> localhost]PLAY [all] ****************************************************************************************************************************TASK [Test connectivity] **************************************************************************************************************ok: [baz]ok: [bar]ok: [foo]PLAY RECAP ****************************************************************************************************************************bar : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0baz : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0foo : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
团体
现在,让我们将机器添加到ansible组:
$ lxc config set bar user.ansible.groups lemp
$ lxc config set foo user.ansible.groups django,node_exporter
$ lxc config set baz user.ansible.groups node_exporter
$ lxcible --list
{
"_meta": {
"hostvars": {
"bar": {
"ansible_host": "10.153.75.179"
},
"baz": {
"ansible_host": "10.153.75.163"
},
"foo": {
"ansible_host": "10.153.75.98"
}
}
},
"all": [
"bar",
"baz",
"foo"
],
"django": [
"foo"
],
"lemp": [
"bar"
],
"node_exporter": [
"baz",
"foo"
]
}
变量
让我们向容器bar
添加一个变量,以便在Ansible模板中使用:
lxc config set bar user.ansible.vars '{ "documentroot": "/var/www/myapp/public" }'
您将在hostvars处看到变量:
$ lxcible --list
{
"_meta": {
"hostvars": {
"bar": {
"documentroot": "/var/www/myapp/public",
"ansible_host": "10.153.75.179"
},
"baz": {
"ansible_host": "10.153.75.163"
},
"foo": {
"ansible_host": "10.153.75.98"
}
}
},
"all": [
"bar",
"baz",
"foo"
],
"django": [
"foo"
],
"lemp": [
"bar"
],
"node_exporter": [
"baz",
"foo"
]
}
在ansible模板中,只需使用{{ documentroot }}
来获取它的值。在
历史
0.2.0(2020年10月14日)
- 在
在ansible清单中使用自定义lxc变量
在 - 在
漂亮的json输出
在
0.1.4(2020年9月22日)
- 使用jq选择数据
0.1.3(2020-09-11)
- 不要在没有IPv4地址的容器上崩溃
0.1.2(2020-04-08)
- 更新了自述文件
0.1.0(2020-04-07)
- PyPI的第一个版本。在
- 项目
标签: