14
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ansibleでパスワード認証&suでのroot実行

Posted at

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

この時の作業メモ

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

# yum -y install --enablerepo=epel sshpass

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

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(site.yml)
- 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 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
14
15
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
14
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?