Skip to content

Commit 69e68b8

Browse files
committed
connection/ssh_lxc.py: new invocation method
Now this connection can (also) be used directly indicating the LXC container as the target (or delegated host), if the variables `ansible_lxc_host` and `ansible_lxc_name` are provided, either in invetory, role or task. `ansible_lxc_host` is the inventory hostname of the LXC running physical host. `ansible_lxc_name` is the container name. File `hosts.example` is provided to show how this variables can be set up in an inventory.
1 parent 9e72163 commit 69e68b8

2 files changed

Lines changed: 71 additions & 3 deletions

File tree

connection_plugins/ssh_lxc.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
connection: ssh_lxc
1414
short_description: connect via ssh client binary and then to a container with lxc-attach
1515
description:
16+
- Normally this connection target host is the one running LXC. Roles which set variable
17+
`ansible_connection` and `ansible_ssh_lxc_name` will be executed on the container.
18+
- If the target host variable `ansible_lxc_host` is defined the behavior is reverted, and the connection
19+
is established
1620
- This connection plugin allows ansible to communicate to the target machines via normal ssh command line.
1721
- Ansible does not expose a channel to allow communication between the user and the ssh process to accept
1822
a password manually to decrypt an ssh key when using this connection plugin (which is the default). The
@@ -21,14 +25,25 @@
2125
version_added: "2.9.6"
2226
options:
2327
host:
24-
description: Hostname/ip to connect to.
28+
description: Hostname/ip running LXC to connect to, or name of the container if `lxc_host` is set.
2529
default: inventory_hostname
2630
vars:
2731
- name: ansible_host
2832
- name: ansible_ssh_host
33+
lxc_host:
34+
descriotion: Hostname/ip running LXC, if `ansible_host` is the container.
35+
vars:
36+
- name: ansible_lxc_host
37+
type: str
38+
hostvars:
39+
description: obtain invetory values for use in `delegate_to` mode with `lxc_host` set.
40+
vars:
41+
- name: hostvars
42+
type: dict
2943
container_name:
30-
description: name of lxc container to attach to
44+
description: name of lxc container to attach to.
3145
vars:
46+
- name: ansible_lxc_name
3247
- name: ansible_ssh_lxc_name
3348
- name: ansible_docker_extra_args
3449
type: str
@@ -478,7 +493,28 @@ def __init__(self, *args, **kwargs):
478493
# management here.
479494

480495
def _connect(self):
481-
self.container_name = self.get_option('container_name')
496+
if self.get_option('lxc_host') is None:
497+
self.container_name = self.get_option('container_name')
498+
499+
display.vvv("lxc_host=None; so container_name={}, host={}".format(self.container_name,
500+
self.host))
501+
else:
502+
self.container_name = self.get_option('container_name')
503+
504+
lxc_host_hostname = self.get_option('lxc_host')
505+
try:
506+
lxc_host_vars = self.get_option('hostvars')[lxc_host_hostname]
507+
except KeyError:
508+
raise AnsibleError("ansible_lxc_host={} not found in invetory.".format(lxc_host_hostname))
509+
510+
self.host = lxc_host_vars['ansible_host']
511+
if 'ansible_port' in lxc_host_vars:
512+
self.port = lxc_host_vars['ansible_port']
513+
if 'ansible_user' in lxc_host_vars:
514+
self.user = lxc_host_vars['ansible_user']
515+
516+
display.vvv("lxc_host={1}; so container_name={0}, host={1}".format(self.container_name,
517+
self.host))
482518
return self
483519

484520
@staticmethod

hosts.example

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
localhost ansible_connection=local
2+
gandalf ansible_host=10.150.40.1 ansible_user=root
3+
4+
# CERTIFICATION AUTHORITIES
5+
authorities ansible_host=10.150.40.8 ansible_user=root
6+
authorities_request ansible_host=10.150.40.8 ansible_user=request
7+
8+
# PHYSICAL HOST -------------------------------------------------------------- <LVM VG Name>
9+
black ansible_host=10.150.40.42 ansible_user=root vg_name=black-vg
10+
ca ansible_host=10.150.40.8 ansible_user=root
11+
12+
# LXC GUEST ------------------------------------------------------------------ <LXC Running Host> ------ <LXC Container Name>
13+
#blogs ansible_host=10.150.42.17 ansible_user=root ansible_lxc_host=black ansible_lxc_name=blogs
14+
ldap ansible_host=10.150.42.10 ansible_user=root ansible_lxc_host=black ansible_lxc_name=ldap
15+
#lists ansible_host=10.150.42.15 ansible_user=root
16+
#login ansible_host=10.150.42.100 ansible_user=root
17+
#mail ansible_host=10.150.42.36 ansible_user=root
18+
#matrix ansible_host=10.150.42.26 ansible_user=root
19+
#media ansible_host=10.150.42.104 ansible_user=root
20+
#projects ansible_host=10.150.42.12 ansible_user=root
21+
#status ansible_host=10.150.42.103 ansible_user=root
22+
#users ansible_host=10.150.42.18 ansible_user=root
23+
#webmail ansible_host=10.150.42.14 ansible_user=root
24+
#wiki ansible_host=10.150.42.16 ansible_user=root
25+
26+
# NETWORK NODES
27+
management_gateway ansible_host=10.150.40.1 ansible_user=root
28+
vm_gateway ansible_host=10.150.42.1 ansible_user=root
29+
reverse_proxy ansible_host=10.150.42.1 ansible_user=root
30+
31+
[vm_hosts]
32+
black

0 commit comments

Comments
 (0)