subtitle

RaiseTechの各種コースをはじめとしたイロイロな学習の記録

Ansibleを使う前の事前準備(作業フォルダの作成と設定ファイルの作成)

ターゲットノードとなるインスタンスAにAnsibleをインストールした後は、$HOMEの下に.ansibleというフォルダができているようだ。

中を見ても特に何が入っているというわけでもなさそうなので、ここを作業フォルダとして使っていこうと思う。

[ec2-user@ip-172-16-23-121 ~]$ ls -a
.   .ansible       .bash_logout   .bashrc  .local           .ssh
..  .bash_history  .bash_profile  .cache   .python_history  .viminfo
[ec2-user@ip-172-16-23-121 ~]$ cd .ansible
[ec2-user@ip-172-16-23-121 .ansible]$ ls -a
.  ..  tmp
[ec2-user@ip-172-16-23-121 .ansible]$ cd tmp
[ec2-user@ip-172-16-23-121 tmp]$ ls -a
.  ..



inventoryフォルダの作成

[ec2-user@ip-172-16-23-121 .ansible]$ mkdir -vp test01/inventory

■ 補足:mkdirの-v オプションは経過を表示、-p オプションは必要に応じて親フォルダも作成してくれる



ansible.cfgの作成と設定

ansible.cfgはAnsibleの設定ファイルなので、ホームの下に配置する

[ec2-user@ip-172-16-23-121 ~]$ touch .ansible.cfg


ファイルができたら中身を記述

[defaults]
# Basic default values

forks = 10
log_path = $HOME/.ansible/ansible.log
host_key_checking = False
gathering = smart
interpreter_python = /usr/bin/python3
private_key_file = $HOME/.ssh/keypair4ansible.pem


  • forks:ターゲットノードの並列処理を行うプロセス数
  • log_path:Ansibleコマンドの実行結果ログのパス
  • host_key_checking:ターゲットノードにSSH接続するときの公開鍵のフィンガープリントのチェックをするかどうか
  • gathering:ターゲットノードの詳細情報取得の設定(smartは新規接続のみ取得)
  • interpreter_python:pythonインタプリタのパス ※[余談1] 参照
  • private_key_file:SSHで使う秘密鍵 ※[余談2] 参照


[余談1] interpreter_pythonについて

地味にハマったので、覚書きとして残しておく。

前提条件として、自分のAnsible環境は下記の通り。

[ec2-user@ip-172-16-23-121 ~]$ ansible --version
ansible 2.10.8
  config file = /home/ec2-user/.ansible.cfg
  configured module search path = ['/home/ec2-user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/ec2-user/.local/lib/python3.7/site-packages/ansible
  executable location = /home/ec2-user/.local/bin/ansible
  python version = 3.7.9 (default, Feb 18 2021, 03:10:35) [GCC 7.3.1 20180712 (Red Hat 7.3.1-12)]


Ansibleバージョン:2.10.8
Pythonバージョン:3.7.9


この状態でAnsibleコマンドを実行すると、下記のようなワーニングが出る。

[WARNING]: Platform linux on host localhost is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change the meaning of that path. See https://docs.ansible.com/ansible/2.10/reference_appendices/interpreter_discovery.html for moreinformation.


ワーニングに記載のURLに行ってみると、Ansibleは指定しなければPythonインタプリタを/usr/bin/pythonのパスに見に行ってしまうけど、今後は違う方法で検出することになるからよろしくね!という感じか。

ワーニングなので別にほっといてもいいのだが、今回の環境としてはPython3を使う予定なので、.ansible.cfg にPython3のインタプリタのパスを指定してやることで解決。

ansible_python_interpreterというパラメータをインベントリファイルに追加するという方法もあるらしいが、今回は .ansible.cfg ファイルを編集する方法を採用。


[余談2] private_key_fileについて

こちらもちょっと(というか結構ガッツリ)ハマったので、覚書きとして残しておく。

AnsibleコマンドはSSHを介して行われる。つまり、コントロールノードとターゲットノードの間に、SSH通信ができるということが前提なのだ。

以前の記事で、コントロールノードとターゲットノードをSSHでつなぐところまではやったが、AnsibleコマンドにこのSSHの公開鍵認証の部分をどうやって盛り込めばいいのか?というのがわからなかったのである。


当然のことながら、Ansibleコマンドに鍵情報を記載しなければSSHで公開鍵認証が通るわけもなくエラーになる。

[ec2-user@ip-172-16-23-121 inventory]$ ansible -i test01_inventory.ini test_servers -m ping
localhost | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).",
    "unreachable": true
}


Failed to connect to the host via ssh

はい。ごもっともでございます。(すいませんね!!)


いろいろ調べてみると、Ansibleコマンドのお尻に、秘密鍵のファイルを指定(--private-key="秘密鍵ファイル")してやればいいということがわかり、解決。

[ec2-user@ip-172-16-23-121 inventory]$ ansible -i test01_inventory.ini test_servers -m ping --private-key="$HOME/.ssh/keypair4ansible.pem"


そしてさらに調べていくと、なんと設定ファイルに記述しておけば、コマンド時の指定もいらないというではないか!

というわけで、.ansible.cfgに private_key_file パラメータを追加してやることにしたというわけ。



インベントリの作成

まずはplaybookを使用せずに、Ansibleコマンドを実行してみるので、インベントリファイルを作成する

[ec2-user@ip-172-16-23-121 ~]$ touch $HOME/.ansible/test01/inventory/test01_inventory.ini

中身を記述

[test_localhost]
localhost



Ansibleコマンドの実行①

早速Ansibleを実行してみよう

[ec2-user@ip-172-16-23-121 inventory]$ ansible -i test01_inventory.ini test_localhost -m ping
localhost | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

自分自身に対してpingを打ってみた。合格!



Ansibleコマンドの実行②

今度はファイルを作成してみる

[ec2-user@ip-172-16-23-121 inventory]$ ansible -i test01_inventory.ini test_localhost -m file -a "path=$HOME/.ansible/test01/test01.txt state=touch mode=0644"
localhost | CHANGED => {
    "changed": true,
    "dest": "/home/ec2-user/.ansible/test01/test01.txt",
    "gid": 1000,
    "group": "ec2-user",
    "mode": "0644",
    "owner": "ec2-user",
    "size": 0,
    "state": "file",
    "uid": 1000
}
[ec2-user@ip-172-16-23-121 test01]$ ls -al
total 0
drwxrwxr-x 3 ec2-user ec2-user 41 Apr 29 12:07 .
drwxrwxr-x 5 ec2-user ec2-user 60 Apr 29 08:39 ..
drwxrwxr-x 2 ec2-user ec2-user 34 Apr 29 09:45 inventory
-rw-r--r-- 1 ec2-user ec2-user  0 Apr 29 12:07 test01.txt

新しくファイルができた。OK!



Ansibleコマンドをざっくりとまとめると・・・

ansible -i [インベントリファイル] [ターゲット] -m [モジュール] [オプション]


という風に書けば、実行できるということが分かった。