可以“ansible_ssh_common_args"变量可用于库存文件中的不同子组? [英] Can "ansible_ssh_common_args" variable be used for different child groups in inventory file?

查看:150
本文介绍了可以“ansible_ssh_common_args"变量可用于库存文件中的不同子组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 2 个不同的 ProxyJumpHost 连接到 2 个主机.

例如:hostname1 只能通过 ProxyJumpHost1 访问hostname2 只能通过 ProxyJumpHost2 访问

当我为该组单独提供ansible_ssh_common_args"变量但 ansible 仅选择一个 ProxyJumpHost 信息并尝试从那里连接两个主机时.

我的库存 yaml 文件看起来像这样

all_nodes:孩子们:预产:孩子们:PRE_CH:变量:ansible_ssh_common_args: '-o ProxyCommand="sshpass -p {{ 密码 }} ssh -W %h:%p -q {{ 用户 }}@"'主持人:主机名1:ansible_host:<IP_Address>主机名2:ansible_host:<IP_Address>PRE_NL:变量:ansible_ssh_common_args: '-o ProxyCommand="sshpass -p {{ 密码 }} ssh -W %h:%p -q {{ 用户 }}@"'主持人:主机名3:ansible_host:<IP_Address>主机名4:ansible_host:<IP_Address>

我的期望是通过正确的 ProxyJumpHost 连接正确的主机.

但实际上它只需要一个 ProxyJumpHost 值并尝试通过它连接所有主机.

解决方案

在您的示例中,两个变量是相同的.

 PRE_CH:变量:ansible_ssh_common_args: '-o ProxyCommand="sshpass -p {{ password }} ssh -W %h:%p -q {{ user }}@"'PRE_NL:变量:ansible_ssh_common_args: '-o ProxyCommand="sshpass -p {{ 密码 }} ssh -W %h:%p -q {{ 用户 }}@"'

<块引用>

Q:例如:hostname1 只能通过 ProxyJumpHost1 访问 hostname2 只能通过 ProxyJumpHost2 访问.我的期望是通过正确的 ProxyJumpHost 连接正确的主机

A:为每个主机设置ansible_ssh_common_args.

 主机:主机名1:ansible_host:<IP_Address>ansible_ssh_common_args: '... {{ user }}@"'主机名2:ansible_host:<IP_Address>ansible_ssh_common_args: '... {{ user }}@<ProxyJumpHost2>"'

<块引用>

问:如果我有 2 台主机可以通过 ProxyJumpHost1 连接,另外还有 2 台主机可以通过 ProxyJumpHost2 连接,我会怎么办.总共有 3 台主机通过 ProxyJumpHost1 和 3 台主机通过 ProxyJumpHost2"

A:例如(为了简单和模块化)创建库存文件 gates.ini,其中包含两个额外的组 gate1 和 gate2.在配置或命令行中将此文件添加到清单中.从其他清单文件中删除 ansible_ssh_common_args.

[gate1]主机名1主机名2主机名3[门 1:变量]ansible_ssh_common_args='-o ProxyCommand="sshpass -p {{ password }} ssh -W %h:%p -q {{ user }}@<ProxyJumpHost1>"'[门2]主机名4主机名5主机名6[gate2:vars]ansible_ssh_common_args='-o ProxyCommand="sshpass -p {{ password }} ssh -W %h:%p -q {{ user }}@<ProxyJumpHost2>"'

I am trying to connect to 2 hosts from 2 different ProxyJumpHost.

For example: hostname1 is reachable only via ProxyJumpHost1 hostname2 is reachable only via ProxyJumpHost2

when I give "ansible_ssh_common_args" variable separately for the group but ansible is picking only one ProxyJumpHost information and trying to connect both the hosts from there.

My inventory yaml file looks like this

all_nodes:
  children:
    preprod:
      children:
        PRE_CH:
          vars:
            ansible_ssh_common_args: '-o ProxyCommand="sshpass -p {{ password }} ssh -W %h:%p -q {{ user }}@<ProxyJumpHost1>"'
          hosts:
            hostname1:
              ansible_host: <IP_Address>
            hostname2:
              ansible_host: <IP_Address>
        PRE_NL:
          vars:
            ansible_ssh_common_args: '-o ProxyCommand="sshpass -p {{ password }} ssh -W %h:%p -q {{ user }}@<ProxyJumpHost1>"'
          hosts:
            hostname3:
              ansible_host: <IP_Address>
            hostname4:
              ansible_host: <IP_Address>

My expectation is to connect the correct host via correct ProxyJumpHost.

But actually it takes only one ProxyJumpHost value and tries to connect all the hosts via that.

解决方案

In your example both vars are identical.

    PRE_CH:
      vars:
        ansible_ssh_common_args: '-o ProxyCommand="sshpass -p {{ password }} ssh -W %h:%p -q {{ user }}@<ProxyJumpHost1>"'

    PRE_NL:
      vars:
        ansible_ssh_common_args: '-o ProxyCommand="sshpass -p {{ password }} ssh -W %h:%p -q {{ user }}@<ProxyJumpHost1>"'

Q: For example: hostname1 is reachable only via ProxyJumpHost1 hostname2 is reachable only via ProxyJumpHost2. My expectation is to connect the correct host via correct ProxyJumpHost

A: Set ansible_ssh_common_args for each host.

      hosts:
        hostname1:
          ansible_host: <IP_Address>
          ansible_ssh_common_args: '... {{ user }}@<ProxyJumpHost1>"'
        hostname2:
          ansible_host: <IP_Address>
          ansible_ssh_common_args: '... {{ user }}@<ProxyJumpHost2>"'

Q: "What if I have 2 more hosts that I will be able to connect via ProxyJumpHost1 and 2 more via ProxyJumpHost2. In total, I will have 3 hosts via ProxyJumpHost1 and 3 hosts via ProxyJumpHost2"

A: For example (for simplicity and modularity) create inventory file gates.ini with two additional groups gate1 and gate2. Add this file to the inventory either in the config, or command line. Remove ansible_ssh_common_args from other inventory files.

[gate1]
hostname1
hostname2
hostname3
[gate1:vars]
ansible_ssh_common_args='-o ProxyCommand="sshpass -p {{ password }} ssh -W %h:%p -q {{ user }}@<ProxyJumpHost1>"'

[gate2]
hostname4
hostname5
hostname6
[gate2:vars]
ansible_ssh_common_args='-o ProxyCommand="sshpass -p {{ password }} ssh -W %h:%p -q {{ user }}@<ProxyJumpHost2>"'

See

这篇关于可以“ansible_ssh_common_args"变量可用于库存文件中的不同子组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆