概要

パスワード認証ありのサーバに接続する必要があり、 且つ踏み台(こちらもパスワード認証)を越える必要があった。

この時の作業メモ

前準備

パスワード認証を通るためには sshpassが必要なので入れておく。

yum -y install --enablerepo=epel sshpass

ansible.cfgを使って回避する。

[ssh_connection]
scp_if_ssh = True

# 下記に記載の<gateway-server>を踏み台サーバIPに書き換えてください.
ssh_args = -o ProxyCommand='sshpass -f passfile ssh -l %r <gateway-server> -W %h:%p'

passfileにsshログインパスワードを書いておく

loginpasswd

playbookの作成

プレイブックの中身

- hosts: all
  gather_facts: False
  su: True
  remote_user: loginuser
  su_user: root

  tasks:
    - name: command whoami
      command: whoami
      register: result

    - debug: msg={{result.stdout}}

実行

ansible実行時に頭にLANG=Cを付けないと固まる。 環境によってはLANG=Cつけようが固まる。

$ LANG=C ansible-playbook -i host site.yml -k --ask-su-pass
SSH password:
su password:

PLAY [all] ********************************************************************

TASK: [command whoami] ********************************************************
changed: [client1]

TASK: [debug msg={{result.stdout}}] *******************************************
ok: [clinet1] => {
   "msg": "root"
}

PLAY RECAP ********************************************************************
client1                      : ok=2    changed=1    unreachable=0    failed=0



Comments

comments powered by Disqus