|
13 | 13 | connection: ssh_lxc |
14 | 14 | short_description: connect via ssh client binary and then to a container with lxc-attach |
15 | 15 | 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 |
16 | 20 | - This connection plugin allows ansible to communicate to the target machines via normal ssh command line. |
17 | 21 | - Ansible does not expose a channel to allow communication between the user and the ssh process to accept |
18 | 22 | a password manually to decrypt an ssh key when using this connection plugin (which is the default). The |
|
21 | 25 | version_added: "2.9.6" |
22 | 26 | options: |
23 | 27 | 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. |
25 | 29 | default: inventory_hostname |
26 | 30 | vars: |
27 | 31 | - name: ansible_host |
28 | 32 | - 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 |
29 | 43 | container_name: |
30 | | - description: name of lxc container to attach to |
| 44 | + description: name of lxc container to attach to. |
31 | 45 | vars: |
| 46 | + - name: ansible_lxc_name |
32 | 47 | - name: ansible_ssh_lxc_name |
33 | 48 | - name: ansible_docker_extra_args |
34 | 49 | type: str |
@@ -478,7 +493,28 @@ def __init__(self, *args, **kwargs): |
478 | 493 | # management here. |
479 | 494 |
|
480 | 495 | 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)) |
482 | 518 | return self |
483 | 519 |
|
484 | 520 | @staticmethod |
|
0 commit comments