はじめに
今回は AnsibleのAnsible.cfgに設定する、[privilege_escalation]について調べたことをまとめます。
きっかけ
[defaults]にbecome = trueを設定し、Playbookを実行したところエラーが発生しました。
公式ドキュメントを確認したところ、become = trueは、[privilege_escalation]に入れると記載がありました。
[privilege_escalation]について知らなかったので、調べてアウトプットすることにしました!
以前のコード
[defaults]
become = true
interpreter_python = /usr/bin/python3.12
inventory = ~/git/menta/ansible/inventory.ini
private_key_file = ~/.ssh/wp-server
retry_files_enabled = False
log_path = ~/.ansible/ansible.log
host_key_checking = False
gathering = smart
発生エラー
Invalid setting in ansible.cfg: 'become' is not a valid option in section: defaults
[privilege_escalation]の記載内容
[privilege_escalation]
become = True
become_method = sudo
become_user = root
特権昇格だけをまとめて制御するセクションです。
昇格方法(sudo, su, pbrun など)、昇格後のユーザー(root 以外も可)を記載します。
特権昇格とは?
Linux サーバーを操作するとき、普通のユーザー (menta など)と、管理者 (root)があります。
普通のユーザーではできないこと(パッケージのインストール、設定ファイルの変更)は、管理者ユーザー (root) でやる必要があります。
Ansible も同じで、
「タスク実行時に一時的に root になる」=これが 特権昇格 (privilege escalation) です。
どうやってrootにする?
Linax の場合
以下の2つが使用されると思います
sudo
一般ユーザーが一時的に root になれる(Ubuntu などで一般的。sudo apt install )
su
root のパスワードを知っている前提で、直接 root になる(CentOS など昔の環境でよく使われる)
Ansible の場合
まず誰になりたいかを選択
become_user = root
rootを選択しどの方法で昇格するかを以下で選択します
become_method = sudo
修正後のAnsible.cfg
[defaults]
interpreter_python = /usr/bin/python3.12
inventory = ~/git/menta/ansible/inventory.ini
retry_files_enabled = False
log_path = ~/.ansible/ansible.log
host_key_checking = False
gathering = smart
[privilege_escalation]
become = True
become_user = root
become_method = sudo
まとめ
修正したことでエラーも出なくなり、Playbookの実行を行えました。
Ansibleでsudoを使用するために、privilege_escalationの設定をする必要があると理解ができ、勉強になりました!
マニュアル