2
2

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 1 year has passed since last update.

macにAnsibleのインストール

Last updated at Posted at 2019-11-14

はじめに

タイトルそのまんまですが、手元のmacにAnsible実行環境を作成します。
参画プロジェクトのProduction環境でPythonのバージョンも異なるだろうと思い、pipenvを採用。
macの場合はpyenvの方が良い(表現がアレですが、pyenvの方がスムーズなのかな?)とのこと、またvirtualenvも利用頻度は低いが覚えておくとよきとのことです。それはまたどこかで。

環境作成にあたりAnsible実践ガイドと下記サイトを参考にさせて頂きました。
この場を借りてお礼申し上げます:pray:

Pipenvの基本的な使い方
[Python] pyenv と pipenv による python 仮想環境を構築する(CentOS 7 ほぼ初期状態から )
Pipenvことはじめ

環境と使用書籍

mac
Ansible実践ガイド

作業開始

pipenvのインストール

macの場合は開発ツール(python-devel, python-dev)はいらないのかな?
わからないので進める!

# 現在のPythonのバージョンを確認
$ python -V
Python 2.7.10

# homebrewでPython3系のインストール
$ brew install python3

# バージョン確認
$ python3 -V
Python 3.7.5

# pipのインストール
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ python3 get-pip.py

# pipenvのインストール
$ brew install pipenv

# pipenvのバージョン確認
$ pipenv --python 3.7.5

# Pipfile自動生成とcat
$ ls -l Pipfile
$ cat Pipfile
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]

[requires]
python_version = "3.7"

サイトを読み進めてると、パッケージのインストールが必要のように見える。
Pipenvの基本的な使い方に倣って、requestsをインストールしてみます。
用途に合わせてパッケージをインストールするんでしょうね。

$ pipenv install requests
$ cat Pipfile
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
requests = "*"

[requires]
python_version = "3.7"

次にAnsibleを実行するディレクトリ「ansible」を作成します。
その後はcdしてpipenvを起動。

$ mkdir ~/ansible
$ cd ~/ansible
$ pipenv --version
pipenv, version 2018.11.26
$ pipenv shell
(admin)$

bashが起動しました!嬉しい!w
仮想環境でAnsibleをインストールします。

(admin)$ pip install ansible
(admin)$ ansible --version
ansible 2.9.1

これにて完了!

おわりに

断片的ではあるのですがAnsibleの実行環境に関する知識がなかったら頓挫していたと思います。。先人の知恵をお借りして実現できてよかった!
次はこの環境を使って実際にAnsibleをガリガリ実行するところから。

おまけ

localhostにpingを投げるときはinventoryを以下のように記述します。

・inventory
ファイル名に拡張子を付けなくても読み込みます(inventory.ini)。
また、インベントリはyamlでも良い。
pipenvでインストールしなかったら/etc/ansible/にhostsという名前のファイルが生成されるので、そこに記述すれば良い。ように読めた。

localhostの書き方でハマり数時間消し飛びました。。
グループは無くても動きます。

$ cat inventory
[test]
localhost ansible_connection=local

$ ansible -i inventory test[もしくはall] -m ping
[WARNING]: Platform darwin on host localhost is using the discovered Python interpreter at /usr/bin/python, but future installation of another
Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more
information.

localhost | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

-i:インベントリの指定
-m:モジュールを使う際のオプション

pingじゃなくてpongってなってるのは、ターゲットノードにpingを投げてる訳じゃなくてsshで接続した結果をpingの結果として返しているからとのこと。
playbook.ymlで指定しても動くはず。これはまた次回行います。

以上です:innocent:

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?