使用 ansible 获取 linode 的 lish 控制台并发出命令

问题描述 投票:0回答:0

我正在玩 ansible 和我的宠物 linode,结果掉进了兔子洞。

这是我的剧本的当前版本:

- 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
      # https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ssh_connection.html
      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
调用 playbook 时,我看到该任务获取了 linode 控制台,但随后它永远留在那里。似乎在 lish 中获取控制台并没有提供 ansible 所需的反馈,让 ansible 知道任务已成功完成(在这种情况下,我将控制台连接到了 linode)

我天真地认为我可以在一个任务中发出我的 linode 名称,让它超时,然后让第二个任务与准备好命令的控制台建立 ssh 连接。

所以我在第一个任务中添加了以下内容:

- name: connect console through lish
  ansible.builtin.raw: my_linode_name
  register: ssh_command_output
  timeout: 20
  ignore_errors: true

但是当任务超时时,ssh 连接似乎会被重置。

我不知道还有什么可以尝试通过 linode lish 控制台发出命令,请提示我是否还有其他方法可以实现这一点。非常感谢!

---2天后编辑---

我已经取得了一些进展,但仍然在黑暗中疑惑。

如果我用前缀标记我的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 ansible linode
© www.soinside.com 2019 - 2024. All rights reserved.