Ansible の使い方(コマンド&Playbook&設定全般)

公開日時:2017年09月10日 / 最終更新日時:2021年08月31日

このページは Ansible で業務で私がよく利用している「機能」「ノウハウ」「メモ」「疑問点」など気が付いたら随時追加していきます。

 

 

ansible.cfg とは何か?

 

Ansible のデフォルトの動作を設定します。

両方のコマンド実行時に読み込まれます。

 

「ansible」コマンドと「ansible-playbook」コマンドの違い

以下のように「ansible」コマンドは「単発で単純」作業、「ansible-playbook」コマンドは「複雑で繰り返し作業」の場合に利用することが違いでしょうか。

つまり作業内容によって、一番使いやすい方を利用すればいいと思います。

 

 

単純な作業なら必ず「ansible」コマンドを利用しなければいけないということはありません。

単純な作業でも yml ファイルに手順を書いて「ansible-playbook」コマンドで実行した方が運用管理的にいい場合は「ansible-playbook」コマンドを使った方がいいでしょう。

 

 

 

ansible.cfg の優先順位

ansible.cfg ファイルには「優先順位」があります。

「優先度が低い」ansible.cfg ファイルは「優先度が高い」ansible.cfg ファイルに「上書き」されるので注意しましょう。

 

↑ 優先度高い

↓ 優先度低い

 

 

Ansibleでアクセス先(管理される側)のホストで以下のメッセージが出た場合

Ansibleをインストールしたサーバではなく、Ansibleでのアクセス先(Ansibleで管理される側)のホストで以下のメッセージが出た場合です。

 

[WARNING]: Platform linux on host 54.xx.xx.xx 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.

 

■コマンド実行結果

[user01@LAPTOP-2N8G36AO-wsl ansible]$ ansible -i hostlist all -m ping 
/usr/lib/python3.6/site-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.26.6) or chardet (3.0.4) doesn't match a supported version! 
  RequestsDependencyWarning) 
[WARNING]: Platform linux on host 54.xx.xx.xx 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. 
54.xx.xx.xx | SUCCESS => { 
    "ansible_facts": { 
        "discovered_interpreter_python": "/usr/bin/python" 
    }, 
    "changed": false, 
    "ping": "pong" 

[user01@LAPTOP-2N8G36AO-wsl ansible]$

 

 

 

■解決方法

以下のサイトを参考にしました。

 

ansible: How to avoid warnings

https://askubuntu.com/questions/1280544/ansible-how-to-avoid-warnings

 

 

■.ansible.cfg ファイルを作成する

[user01@LAPTOP-2N8G36AO-wsl ~]$ vi ~/.ansible.cfg
[defaults]
interpreter_python=auto_silent

 

 

 

■コマンド実行結果

[user01@LAPTOP-2N8G36AO-wsl ansible]$ ansible -i hostlist all -m ping 
/usr/lib/python3.6/site-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.26.6) or chardet (3.0.4) doesn't match a supported version! 
  RequestsDependencyWarning) 
54.xx.xx.xx | SUCCESS => { 
    "ansible_facts": { 
        "discovered_interpreter_python": "/usr/bin/python" 
    }, 
    "changed": false, 
    "ping": "pong" 

[user01@LAPTOP-2N8G36AO-wsl ansible]$

 

WARNINGが消えました。

 

 

/usr/lib/python3.6/site-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.26.6) or chardet (3.0.4) doesn’t match a supported version!のエラーが出力される

Ansibleを実行した際に以下のエラーが出力されました。

/usr/lib/python3.6/site-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.26.6) or chardet (3.0.4) doesn't match a supported version!
RequestsDependencyWarning)

 

■コマンド実行時

[user01@LAPTOP-2N8G36AO-wsl ansible]$ ansible -i hostlist all -m ping 
/usr/lib/python3.6/site-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.26.6) or chardet (3.0.4) doesn't match a supported version! 
  RequestsDependencyWarning) 
54.xx.xx.xx | SUCCESS => { 
    "ansible_facts": { 
        "discovered_interpreter_python": "/usr/bin/python" 
    }, 
    "changed": false, 
    "ping": "pong" 

[user01@LAPTOP-2N8G36AO-wsl ansible]$

 

 

以下のサイトを参考にしました。

Python (pip) - RequestsDependencyWarning: urllib3 (1.9.1) or chardet (2.3.0) doesn't match a supported version

https://stackoverflow.com/questions/50202238/python-pip-requestsdependencywarning-urllib3-1-9-1-or-chardet-2-3-0-doe

 

■コマンド実行

[user01@LAPTOP-2N8G36AO-wsl ~]$ sudo pip3 install requests 
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead. 
Requirement already satisfied: requests in /usr/lib/python3.6/site-packages 
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/lib/python3.6/site-packages (from requests) 
Requirement already satisfied: idna<2.8,>=2.5 in /usr/lib/python3.6/site-packages (from requests) 
Collecting urllib3<1.25,>=1.21.1 (from requests) 
  Downloading https://files.pythonhosted.org/packages/01/11/525b02e4acc0c747de8b6ccdab376331597c569c42ea66ab0a1dbd36eca2/urllib3-1.24.3-py2.py3-none-any.whl (118kB) 
    100% |????????????????????????????????| 122kB 279kB/s 
Installing collected packages: urllib3 
  Found existing installation: urllib3 1.26.6 
    Uninstalling urllib3-1.26.6: 
      Successfully uninstalled urllib3-1.26.6 
Successfully installed urllib3-1.24.3 
[user01@LAPTOP-2N8G36AO-wsl ~]$

 

 

 

■pip3でrequestsをインストール後にAnsible実行

[user01@LAPTOP-2N8G36AO-wsl ~]$ ansible -i ansible/hostlist all -m ping 
54.xx.xx.xx | SUCCESS => { 
    "ansible_facts": { 
        "discovered_interpreter_python": "/usr/bin/python" 
    }, 
    "changed": false, 
    "ping": "pong" 

[user01@LAPTOP-2N8G36AO-wsl ~]$

 

 

 

 

 

Posted by 100%レンタルサーバーを使いこなすサイト管理人

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

AlphaOmega Captcha Medica  –  What Do You See?
     
 

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

Secured By miniOrange