LoginSignup
7
9

More than 5 years have passed since last update.

Ansibleでsudoのコマンドを実行する

Posted at

ansibleでは become: yes とかけばsudoでコマンド実行ができますが、そのためにはユーザーにsudo権限がついていないといけません。

今回Vagrantで構築したUbuntuに対して、vagrant以外のユーザーでAnsibleを叩きたいと思います。

環境

Vagrant 1.9.7
Ubuntu 14.04
(vm: "ubuntu/trusty64")
ansible 2.3.1.0

sudo権限を付ける

ubuntu という名前のユーザーを作り、sudo権限を付けます。

まずログイン

$ vagrant ssh

ここからはubuntu内の操作です。
ubuntuという名前のユーザーがいないなら作っておきます。

$ adduser ubuntu

適当なパスワードを入力してあとはEnter。

ubuntuでエディタをviにします。やらなくてもいいですがnanoが使いにくかったので。

$ sudo update-alternatives --config editor

選択肢がでますが、3を入力します。

ubuntuにsudo権限を付けます。

$ sudo visudo

末尾に以下を追加。
ubuntu ALL=(ALL) NOPASSWD: ALL

これで、su ubuntuでubuntuになり、sudo lsなどが実行できるか確認します。

だいたいこれでOKです。

あとは、1. ansibleのプレイブックでユーザーを指定するか、2. hostsファイルにユーザーをかけばOKです。

  1. プレイブック内に、user:でubuntuを指定します。
- hosts: dev
  user: ubuntu
  become: yes
  tasks:
    - name: whoami
      command: whoami

  1. hostsファイルにansible_ssh_userで指定します。
[web]
192.168.33.10 ansible_ssh_user=ubuntu

参考

Ubuntuで、デフォルトエディタを変更する | WWWクリエイターズ

linux - Ansible sudo without password - Stack Overflow

7
9
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
7
9