Ansible:通过堡垒主机从本地主机执行剧本

时间:2020-04-06 09:29:22

标签: ansible

我是Ansible的新手

我们正在通过ansible进行部署,并为部署配置了堡垒主机。

我目前使用的方法是在堡垒主机中克隆ansible存储库,并从该文件夹中运行命令

我的问题是可以通过堡垒在本地计算机上运行ansible代码吗?

(基本上,避免在堡垒主机中进行回购)

2 个答案:

答案 0 :(得分:1)

假设您要在172.20.0.10堡垒中的开发环境中预配置几个VM 172.20.0.11172.20.0.1。您的库存看起来像这样

[development]
172.20.0.10
172.20.0.11

然后,您可以编辑~/.ssh/config并添加

Host bastion
    Hostname 172.20.0.1
    User youruser

Host 172.20.*
    ProxyJump bastion
    User youruser

然后,您可以测试一个ssh 172.20.0.10,它将使您进入第一个VM。如果它适用于SSH,则Ansible应该也可以使用。

请注意,您可以使用-vvv运行ansible(或者是另一个v,不确定atm),您将看到Ansible正在运行SSH命令。<​​/ p>

注意2,ProxyJump需要最新的OpenSSH,至少在我没记错的情况下需要6.7

答案 1 :(得分:0)

使用此数据:

  const BucketSchema = new Schema(
      {
        id: { type: objectid },
        user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
        buckets: [{
           name: { type: string },
           items: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Item' }],
        }]
      },
    );

并使用密钥通过ssh进行访问(您必须将密钥存储在安全的存储器中,而不是与无用的剧本一起存储)

通过ssh单个命令

host remoto : 10.0.1.121
user remoto : application_user
ssh key : app_ssh_key

host bastian : 212.34.345.12
user bastian : bastian_user
ssh key: bastian_ssh_key

不可更改 您可以使用两种方法:

  • 为库存机/组使用变量,以便为不同的机/组具有不同的连接选项

添加到库存文件

ssh application_user@10.0.1.121 -i path/to/app_ssh_key -o ProxyCommand="ssh -q bastian_user@212.34.345.12 -i path/to/bastian_ssh_key -W %h:%p"
  • 所有库存机的单一配置有效期

添加到/替换为ansible.cfg

[remote-vm]
10.0.1.121

[remote-vm:vars]
ansible_ssh_user=application_user
ansible_ssh_private_key_file=path/to/app_ssh_key
ansible_ssh_common_args= -o ProxyCommand="ssh -q bastian_user@212.34.345.12 -i path/to/bastian_ssh_key -W %h:%p"