Ansible とは、構成管理ツールである。書籍「初めての Ansible」も参照。
インストールは簡単だろうとタカをくくっていた。私の環境は WSL2 の Ubuntu 20.04 LTS なので、 まず何も考えず次のようにした。
$ sudo apt install ansible
このコマンドは正常に完了した。
ところが、いざ適用しようとすると、全く動かない。
$ ansible --version Traceback (most recent call last): File "/usr/bin/ansible", line 34, infrom ansible import context ModuleNotFoundError: No module named 'ansible'
バカにされているようだ。Ansible ModuleNotFounderror で検索してヒットした下記ページ
https://wesley.sh/solving-modulenotfounderror-no-module-named-ansible/
を読んでみる。このブログの主は $ pip list を実行したようだ。私もやってみた。
(base) $ pip3 list Package Version ---------------------------------- ------------------- absl-py 0.12.0 ad3 2.2.1 alabaster 0.7.12 anaconda-client 1.7.2 anaconda-navigator 1.10.0 anaconda-project 0.9.1 anyio 2.2.0 appdirs 1.4.4 argh 0.26.2 argon2-cffi 20.1.0 arviz 0.11.1 (後略)
確かに、ansible というパッケージはない。ただ、私の場合は Anaconda を入れているので、 Anaconda で確認するのが筋だろう。
(base) $ conda list # packages in environment at /home/satosi/anaconda3: # # Name Version Build Channel _anaconda_depends 2020.07 py38_0 _ipyw_jlab_nb_ext_conf 0.1.0 py38_0 _libgcc_mutex 0.1 conda_forge conda-forge _openmp_mutex 4.5 1_gnu conda-forge _tflow_select 2.3.0 mkl absl-py 0.12.0 py38h06a4308_0 alabaster 0.7.12 pyhd3eb1b0_0 anaconda custom py38_1 anaconda-client 1.7.2 py38_0 anaconda-navigator 1.10.0 py38_0 anaconda-project 0.9.1 pyhd3eb1b0_1 anyio 2.2.0 py38h06a4308_1 appdirs 1.4.4 py_0 argh 0.26.2 py38_0 argon2-cffi 20.1.0 py38h27cfd23_1 (後略)
入っていない。pip と Anaconda では Anaconda を優先すべきだ。
(base) $ conda install ansible Collecting package metadata (current_repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. Collecting package metadata (repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. PackagesNotFoundError: The following packages are not available from current channels: - ansible Current channels: - https://repo.anaconda.com/pkgs/main/linux-64 - https://repo.anaconda.com/pkgs/main/noarch - https://repo.anaconda.com/pkgs/r/linux-64 - https://repo.anaconda.com/pkgs/r/noarch To search for alternate channels that may provide the conda package you're looking for, navigate to https://anaconda.org and use the search bar at the top of the page.
ここでも嫌われた。仕方がない。上記 Anaconda のサイトで Ansible を検索すると、 チャンネル conda-forge にあることがわかった。再度試す。
(base) $ conda install -c conda-forge ansible
途中で Procees ([y]/n) と出るので y を入力すると、 10 を超えるパッケージがインストールされる。インストールは無事終わったようだ。さて、 起動できるだろうか。
(base) $ ansible --version ansible 2.10.9 config file = /etc/ansible/ansible.cfg configured module search path = ['~/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = ~/anaconda3/lib/python3.8/site-packages/ansible executable location = ~/anaconda3/bin/ansible python version = 3.8.5 (default, Sep 4 2020, 07:30:14) [GCC 7.3.0] (base) $
インベントリファイル hosts に設定を書き込んで、 testserver という名前のサーバーに接続させてみようとした。
(base) $ ansible testserver -i hosts -m ping [WARNING]: * Failed to parse /path/to/hosts with script plugin: problem running /path/to/hosts --list ([Errno 8] Exec format error: '/path/to/hosts') [WARNING]: * Failed to parse /path/to/hosts with yaml plugin: YAML inventory has invalid structure, it should be a dictionary, got: <class 'ansible.parsing.yaml.objects.AnsibleUnicode'> [WARNING]: * Failed to parse /path/to/hosts with ini plugin: /path/to/hosts:1: Expected key=value host variable assignment, got: ansible_ssh [WARNING]: Unable to parse /path/to/hosts as an inventory source [WARNING]: No inventory was parsed, only implicit localhost is available [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' [WARNING]: Could not match supplied host pattern, ignoring: testserver
エラーが出て失敗した。コマンドオプションを指定してエラーを特定しようとしたが、やはりわからん。
(base) $ ansible testserver -i hosts -m ping -vvvv
仕方がないので、ssh 接続をあきらめた。localhost に対してだからいいや。
(base) $ ansible localhost -m ping localhost | SUCCESS => { "changed": false, "ping": "pong" }
Ansible 2 では、localhost に対するコマンドは SSH 接続を使用しないのがデフォルトになっている。 明示的に指定するには、ansible local -c local -m ping などとする。ここまではできた。
「初めての Ansible」のp.20 をまねて、 仮想マシンの Ubuntu に nginx をインストールしようとしたが、うまくいかない。
$ ansible localhost --become-user root -m apt -a name=nginx [WARNING]: Updating cache and auto-installing missing dependency: python3-apt localhost | FAILED! => { "changed": false, "cmd": "apt-get update", "msg": "E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)\n→ E: Unable to lock directory /var/lib/apt/lists/\n→ W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)\n→ W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)", "rc": 100, "stderr": "E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)\n→ E: Unable to lock directory /var/lib/apt/lists/\n→ W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)\n→ W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)\n", "stderr_lines": [ "E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)", "E: Unable to lock directory /var/lib/apt/lists/", "W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)", "W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)" ], "stdout": "Reading package lists...\n", "stdout_lines": [ "Reading package lists..." ] }
うまくいかない。ロックファイルを消せばいいのだろうか。
$ ansible localhost --become-user root -a "rm /var/lib/apt/lists/lock" [WARNING]: Consider using the file module with state=absent rather than running 'rm'. → If you need to use command because file is insufficient you can add 'warn: false' → to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message. localhost | FAILED | rc=1 >> rm: '/var/lib/apt/lists/lock' を削除できません: 許可がありませんnon-zero return code
Ansible からはロックファイルも消せない。仕方がないので Virtual Box 上でロックファイルを消した。 再度 Ansible から nginx をインストールしようとしたが、同じメッセージが出てインストールできない。 すでに /var/lib/apt/list/lock は削除したのに、なぜだろう。