Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ssh 在Ansible playbook中使用用户名/密码(从API获取)登录_Ssh_Ansible_Ansible Playbook - Fatal编程技术网

Ssh 在Ansible playbook中使用用户名/密码(从API获取)登录

Ssh 在Ansible playbook中使用用户名/密码(从API获取)登录,ssh,ansible,ansible-playbook,Ssh,Ansible,Ansible Playbook,我试图在Ansible剧本中处理以下情况: 通过云提供商的API创建服务器 存储API响应中的根凭据(用户名/密码) 使用root凭据登录并放置SSH密钥 设置新用户、禁用root登录等 我可以在步骤4中使用authorized_key和user模块。我有一个模块(自建)用于第1步和第2步。我如何处理步骤3?是否有任何现有的解决方案可用于以编程方式使用用户名/密码登录?我不想让他们通过命令行 基本上,我也可以为此构建一个模块。理想情况下,我想要类似于: - task: setup SSH p

我试图在Ansible剧本中处理以下情况:

  • 通过云提供商的API创建服务器
  • 存储API响应中的根凭据(用户名/密码)
  • 使用root凭据登录并放置SSH密钥
  • 设置新用户、禁用root登录等
  • 我可以在步骤4中使用
    authorized_key
    user
    模块。我有一个模块(自建)用于第1步和第2步。我如何处理步骤3?是否有任何现有的解决方案可用于以编程方式使用用户名/密码登录?我不想让他们通过命令行

    基本上,我也可以为此构建一个模块。理想情况下,我想要类似于:

    - task: setup SSH place_ssh_with_passwd: state: present key: /path/to/local/key path: /root/.ssh/authorized_keys user: "{{ server.username }}" password: "{{ server.password }}" -任务:安装SSH 将\u ssh\u与\u passwd一起放置: 国家:现在 键:/path/to/local/key 路径:/root/.ssh/authorized\u密钥 用户:“{server.username}” 密码:{{server.password}}
    使用
    add_host
    操作将主机添加到一个新组中,并在云提供商的响应中设置ansible_ssh_主机、ansible_ssh_用户和ansible_ssh_通行证(请参见中的示例)。然后开始一个新的重头戏,针对新组执行密钥放置和任何其他针对新主机的设置任务。

    使用
    add\u host
    操作将主机添加到新组,并设置ansible\u ssh\u主机、ansible\u ssh\u用户和ansible\u ssh\u pass,其中包含来自云提供商的Jinja模板(参见中的示例)。然后开始一个新的重头戏,以新组为目标,对新主机执行密钥放置和任何其他设置任务。

    创建服务器后,我找到了一种在同一重头戏中完成此任务的方法:

    - name: create server ...use module, create server and grap password from output... - name: add SSH key to server shell: "./scp_ssh_key.sh /path/to/key user@host:/path/ {{ passwd }} -名称:创建服务器 …使用模块,从输出创建服务器和grap密码。。。 -名称:将SSH密钥添加到服务器 shell:“./scp\u ssh\u key.sh/path/to/keyuser@host:/path/{{passwd}} 其中,shell脚本的内容大致如下:


    在此之后,可以使用Ansible的内置模块设置用户、其他键等。

    我找到了一种方法,在创建服务器后的同一重头戏中实现这一点:

    - name: create server ...use module, create server and grap password from output... - name: add SSH key to server shell: "./scp_ssh_key.sh /path/to/key user@host:/path/ {{ passwd }} -名称:创建服务器 …使用模块,从输出创建服务器和grap密码。。。 -名称:将SSH密钥添加到服务器 shell:“./scp\u ssh\u key.sh/path/to/keyuser@host:/path/{{passwd}} 其中,shell脚本的内容大致如下:


    在此之后,您可以使用Ansible的内置模块设置用户和其他密钥等。

    如果一切都失败,您可以始终运行本地任务并使用凭据手动连接到主机。类似于:
    -shell:sshpass-p“password”scp/path/to/local/keyroot@xxx.xxx.xxx.xxxx:/root/.ssh/authorized_key
    。如果是ev任何失败的情况下,您都可以运行本地任务并使用凭据手动连接到主机。如下所示:
    -shell:sshpass-p“password”scp/path/to/local/keyroot@xxx.xxx.xxx.xxxx:/root/.ssh/authorized_key