如何通过Ansible借助Linode Lish控制台执行命令?
用Ansible通过Linode Lish执行命令的问题与进展
初始问题
尝试用Ansible操作Linode实例时遇到阻碍,当前playbook如下:
- name: run command through Linode Lish hosts: localhost gather_facts: false vars_files: - ../group_vars/vars.yml tasks: - include_tasks: ../tasks/get_console_through_lish_and_run_commands.yml vars: ansible_connection: ansible.builtin.ssh ansible_ssh_host: lish-ap-southeast.linode.com ansible_ssh_user: my_user_name ansible_ssh_common_args: "-t -o ControlMaster=auto -o ControlPersist=300s" ansible_ssh_timeout: 300 connect_timeout: 30
对应的任务文件内容:
- name: connect console through lish ansible.builtin.raw: my_linode_name register: ssh_command_output - name: Print response of ssh command debug: msg: "{{ ssh_command_output }}" - name: run command through lish console ansible.builtin.raw: ls -lah register: ssh_command_output - name: Print response of ssh command debug: msg: "{{ ssh_command_output }}"
第一个任务始终挂起,开启ANSIBLE_DEBUG=1后发现已成功获取Linode控制台,但Lish未返回Ansible要求的成功反馈,导致任务停滞。尝试添加超时和忽略错误配置,但超时后SSH连接会被重置。
两天后进展
给Linode实例加上linode_前缀的标签后,在~/.ssh/config中添加以下配置:
Match exec "echo %h | grep -E 'linode_'" HostName lish-ap-southeast.linode.com Port 22 IdentityFile ~/.ssh/id_rsa User my_user_name RequestTTY yes RemoteCommand %n
现在可以通过ssh linode_whatever自动进入对应Linode实例,但无法执行ssh linode_whatever ls -lah这类Ansible需要的命令。尝试建立SSH隧道时,报错channel 2: open failed: administratively prohibited: open failed。
又两天后进展
参考相关方案,尝试后台启动SSH并通过命名管道发送命令,但输入Linode名称时出现No pseudo-tty detected错误。
最终临时解决方法
通过screen会话结合命名管道,成功在Lish控制台执行了ls -lah命令,但仍希望找到Ansible原生方式实现该需求的方法。
内容的提问来源于stack exchange,提问作者Greg0ry
相关产品推荐
相关产品推荐

