LoginSignup
3
4

More than 5 years have passed since last update.

Ansible で中継ホストだけ違う同じアドレスのサーバをインベントリで分けるとき

Posted at

Ansible で下記のようにアドレスが同じで中継ホストが異なるインベントリを使うとき。

prod

sv01 ansible_ssh_host=192.168.88.11
sv02 ansible_ssh_host=192.168.88.12

[all:vars]
ansible_ssh_common_args = -o "ProxyCommand ssh 192.0.2.100 -l root -W %h:%p"

stage

sv01 ansible_ssh_host=192.168.88.11
sv02 ansible_ssh_host=192.168.88.12

[all:vars]
ansible_ssh_common_args = -o "ProxyCommand ssh 192.0.2.200 -l root -W %h:%p"

control_path のデフォルトが下記のようになっていると思うので、

control_path = %(directory)s/ansible-ssh-%%h-%%p-%%r

最初に接続した方のインベントリのホストで下記のような ControlMaster のソケットファイルが作成されて、

~/.ansible/cp/ansible-ssh-192.168.88.11-22-ore
~/.ansible/cp/ansible-ssh-192.168.88.12-22-ore

別のインベントリファイルを指定してもこのソケットファイルが使用されてしまい、誤ったホストに接続されてしまいます。

ので、下記のようにどちらかのインベントリで ControlPath を変更する必要があります(もしくは無効にする)。

sv01 ansible_ssh_host=192.168.88.11
sv02 ansible_ssh_host=192.168.88.12

[all:vars]
ansible_ssh_common_args = -o "ProxyCommand ssh 192.0.2.200 -l root -W %h:%p" -o "ControlPath ~/.ssh/stage-%h-%r"
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4