今日のお題:ansibleを用いてパッケージ1つをインストールする。
実行環境
- virtual box
- ubuntu16.04
前提条件
- サーバ側にansibleのインストールを行っていること。 ※ここでやりました。
- クライアント側の設定とサーバ間のSSH設定を行う ※ここでやりました。
- 本手順はサーバ/クライアント側すべて一般ユーザで実施します。この手順ではクライアント側のsudoersの設定で一般ユーザ側でsudoのnopass設定をしています。
目的
ansibleというものはサーバに対して実行する事が主目的のため、playbookというものに命令を記述して「ansible-playbook」コマンドにて当該命令を指定したサーバに対して実行する。
今回は特に色気を付けずにplaybookを用いてパッケージをインストールする。
手順
サーバ側手順
cd ~
vi install_cowsay.yml
ansible-playbook install_cowsay.yml -s
- hosts: all
tasks:
- name: install cowsay package
apt:
name: cowsay
openstack@Ubuntu16041Server:~$ ansible-playbook install_cowsay.yml -s
PLAY [all] *********************************************************************
TASK [setup] *******************************************************************
ok: [192.168.56.101]
TASK [install cowsay package] **************************************************
changed: [192.168.56.101]
PLAY RECAP *********************************************************************
192.168.56.101 : ok=2 changed=1 unreachable=0 failed=0
クライアント側手順
cowsay YesYesYes
openstack@ubuntu160401client:~$ cowsay YesYesYes
___________
< YesYesYes >
-----------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
参考リンク
おまけ
設定が誤っていた時に出たエラーです。
・・・
```txt:playbook内でaptを実行した際に権限が無いためエラーになったパターン
TASK [install cowsay package] **************************************************
fatal: [192.168.56.101]: FAILED! => {"changed": false, "cmd": "apt-get update", "failed": true, "msg": "W: chmod 0700 of directory /var/lib/apt/lists/partial failed - SetupAPTPartialDirectory (1: Operation not permitted)\nE: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)\nE: Unable to lock directory /var/lib/apt/lists/\nW: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)\nW: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)\nE: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)\nE: Unable to lock the administration directory (/var/lib/dpkg/), are you root?", "rc": 100, "stderr": "W: chmod 0700 of directory /var/lib/apt/lists/partial failed - SetupAPTPartialDirectory (1: Operation not permitted)\nE: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)\nE: Unable to lock directory /var/lib/apt/lists/\nW: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)\nW: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)\nE: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)\nE: Unable to lock the administration directory (/var/lib/dpkg/), are you root?\n", "stdout": "", "stdout_lines": []}
to retry, use: --limit @/home/openstack/install_cowsay.retry
PLAY RECAP *********************************************************************
192.168.56.101 : ok=1 changed=0 unreachable=0 failed=1
・・・
```