11
4

More than 3 years have passed since last update.

ansible 〜つなぐ〜

Last updated at Posted at 2019-01-29

ansible 〜つなぐ〜

インベントリファイル(今回はhosts.ymlで統一します)、ansible.cfgssh_configを駆使し、様々な状況でansibleをつなぎます。
繋いだ後は好きにしてください。

接続の確認に用いたコマンド

ansible -m ping all -i hosts.yml

allとはansibleが認識したホストが自動的に入れられるグループです。
その名の通り全ホストが対象になります。

初級

普通につなぐ

  • hosts.yml
all:
  hosts:
    server1:
      ansible_host: 192.168.122.100
      ansible_user: vagrant
      ansible_ssh_private_key_file: ~/.ssh/id_rsa

ansibleは基本公開鍵認証でログインします。

「pingなのにログイン???」

って思うと思います。私は思いました。
-vvvオプションを使うと実行したコマンド等がダラダラ表示され、
ansible -m ping all -vvvとするとわかりますが、
ansible -m pingpingを実行しているのではなく、sshでログインを試行し、
ログイン成功した場合SUCCESSと帰ってきます。

パスワード認証でつなぐ

  • hosts.yml
all:
  hosts:
    server1:
      ansible_host: 192.168.122.100
      ansible_user: vagrant
      ansible_pass: vagrant

ansible_passを設定した場合、パスワード認証でログインを試行します。
ただファイルにパスワードを直で書くのは少し気が引けますね

パスワード認証でつなぐask_pass

  • hosts.yml
all:
  hosts:
    server1:
      ansible_host: 192.168.122.100
      ansible_user: vagrant
  • ansible.cfg
[defaults]
ask_pass = True

ansible.cfg内でask_passの値をTrueに設定することで、
ansibleコマンド実行前にパスワードの入力が促されます。

$ ansible -m ping all
SSH password: 
server1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

ansible.cfgに記述せず、コマンド実行時に--ask-passオプションを付けることでも同様です。

$ ansible -m ping all --ask-pass
SSH password: 
server1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

中級

locahostにつなぐ

  • hosts.yml
all:
  hosts:
    server1:
      ansible_connection: local

ansible_connectionの値にlocalを設定するとlocalhostにアクセスします。
この時ssh接続は行いませんので、ansible_useransible_ssh_passの設定は必要ありません。

ssh_configを使ってつなぐ

ssh_configファイルはssh接続を行うときの設定ファイルです。
ドキュメント
これ自体はOpenSSHの設定ファイルなのですが、ansibleと組み合わせると非常に便利になります。

  • hosts.yml
all:
  hosts:
    server1: {}
  • ansible.cfg
[ssh_connection]
ssh_args = -F ssh_config
  • ssh_config
Host server1
  HostName 192.168.122.100
  User vagrant
  Port 22
  PasswordAuthentication no
  IdentityFile ~/.ssh/id_rsa
  IdentitiesOnly yes

ansible.cfgansiblessh接続を行う際のオプションを追加しています。
接続情報をすべてssh_configに記載してしまったのでhosts.ymlでは何も設定していません。

上級

踏み台サーバーを経由してつなぐ

ssh_configProxyCommandを利用すると踏み台サーバーを経由したアクセスが可能になります。
ansibleというよりOpenSSHのお話ですね。
今回の構成は以下

[ansible] --> [server1(踏み台)] --> [server2(目的のサーバー)]
  • hosts.yml
all:
  hosts:
    server1: {}
    server1: {}
  • ansible.cfg
[ssh_connection]
ssh_args = -F ssh_config
  • ssh_config
Host server1
  HostName 192.168.122.100
  User vagrant
  Port 22
  PasswordAuthentication no
  IdentityFile ~/.ssh/id_rsa
  IdentitiesOnly yes

Host server2
  HostName 192.168.122.101
  User vagrant
  Port 22
  PasswordAuthentication no
  IdentityFile ~/.ssh/id_rsa
  IdentitiesOnly yes
  ProxyCommand ssh -F ssh_config -W %h:%p server1

ssh_config内のProxyCommandの設定を記述するだけですね。
ProxyCommandを設定するとターゲットになるホストにssh接続を行う前に、 指定されたコマンドを実行します。
そして別のホストにsshコネクションをはってからssh接続を行うようになります。

ポートフォーディングを利用してつなぐ

踏み台サーバーを経由しポートフォーディングでssh接続を行います。
これもansibleというよりOpenSSHのお話ですね。
今回の構成は以下

[ansible] --> [server1(踏み台)] --> [server2(目的のサーバー)]

server2の22番ポートをlocalhostの10022番ポートにつなぎます。

ssh -F ssh_config -f -N -L 10022:server2:22 server1

これでlocalhostの10022番にアクセスするとserver2の22番ポートにフォーディングされます。
あとはlocalhostの10022番にssh接続するだけでおっけーです。

  • hosts.yml
all:
  hosts:
    server1: {}
    server1: {}
  • ansible.cfg
[ssh_connection]
ssh_args = -F ssh_config
  • ssh_config
Host server1
  HostName 192.168.122.100
  User vagrant
  Port 22
  PasswordAuthentication no
  IdentityFile ~/.ssh/id_rsa
  IdentitiesOnly yes

Host server2
  HostName 127.0.0.1
  User vagrant
  Port 10022
  PasswordAuthentication no
  IdentityFile ~/.ssh/id_rsa
  IdentitiesOnly yes

おわり

他にも違うパターンでのつなぎ方&解決策を見つけ次第追記していきたいです。

アイデアある方コメントでいただければ幸いです。

11
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
11
4