Ansible Documentation
Python 3 Support参考
こちらの記事で CentOS 7.6.1810 に Ansible をインストールしました。今回は Ubuntu 19.04 desktop に Ansible をインストールします。基本的な流れは CentOS 7.6.1810 のときと同じですが、次の差異があります。- CentOS は RedHat 系だが Ubuntu は Debian 系
- CentOS は python2 が標準だが、Ubuntu は python3 が標準
- CentOS は Ansible のインストール時に sshpass コマンドがインストールされるが、Ubuntu は別途インストールが必要
- Ansible のインストール時に使用する pip3 コマンドは別途インストールが必要
- 作業用アカウント ansibleman は Ubuntu 19.04 をインストールするときに作成する
ansibleman@ubuntu-pc:~$ python --version
Command 'python' not found, but can be installed with:
sudo apt install python3 # version 3.7.3-1, or
sudo apt install python # version 2.7.16-1
sudo apt install python-minimal # version 2.7.16-1
You also have python3 installed, you can run 'python3' instead.
ansibleman@ubuntu-pc:~$
ansibleman@ubuntu-pc:~$ python3 --version
Python 3.7.3
ansibleman@ubuntu-pc:~$
ansibleman@ubuntu-pc:~$ pip3
Command 'pip3' not found, but can be installed with:
sudo apt install python3-pip
ansibleman@ubuntu-pc:~$
ToDo
作用の流れです。- pip3 コマンドのインストール
- pip3 コマンドがインストールされたことを確認
- ansible の確認
- &nbps;ansible がインストールされたことの確認
- ansible コマンドのバージョン確認
- ssh 接続時のフィンガープリントを記録しないよう設定
- 作業用ディレクトリ ansible の作成
投入コマンド
すべて、インストール時に作成したアカウント ansibleman で作業します。- sudo apt install -y python3-pip
- pip3
- pip3 install ansible
- pip3 show ansible
- ansible --version
- sudo apt install -y sshpass
- mkdir ~/.ssh
- chmod 700 ~/.ssh
- vi ~/.ssh/config
- Host *
- StrictHostKeyChecking no
- UserKnownHostsFile=/dev/null
- chmod 600 ~/.ssh/config
- mkdir ~/ansible
作業ログ
作業ログです。2019/05/12 時点で最新の Ansible 2.7.10 がインストールされました。ansibleman@ubuntu-pc:~$ sudo apt install -y python3-pip
[sudo] password for ansibleman:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
(中略)
Setting up libpython3-dev:amd64 (3.7.3-1) ...
Setting up python3-dev (3.7.3-1) ...
Processing triggers for man-db (2.8.5-2) ...
ansibleman@ubuntu-pc:~$
ansibleman@ubuntu-pc:~$ pip3
Usage:
pip3 [options]
Commands:
install Install packages.
download Download packages.
(中略)
download. Implied with --no-index.
--no-color Suppress colored output
ansibleman@ubuntu-pc:~$
ansibleman@ubuntu-pc:~$ pip3 install ansible
Collecting ansible
Downloading https://files.pythonhosted.org/packages/9a/9d/5e3d67bd998236f32a72f255394eccd1e22b3e2843aa60dc30dd164816d0/ansible-2.7.10.tar.gz (11.8MB)
100% |████████████████████████████████| 11.8MB 136kB/s
(中略)
Requirement already satisfied: PyYAML in /usr/lib/python3/dist-packages (from ansible) (3.13)
Successfully installed ansible-2.7.10 bcrypt-3.1.6 cffi-1.12.3 jinja2-2.10.1 paramiko-2.4.2 pyasn1-0.4.5 pycparser-2.19
ansibleman@ubuntu-pc:~$
ansibleman@ubuntu-pc:~$ pip3 show ansible
Name: ansible
Version: 2.7.10
Summary: Radically simple IT automation
Home-page: https://ansible.com/
Author: Ansible, Inc.
Author-email: info@ansible.com
License: GPLv3+
Location: /home/ansibleman/.local/lib/python3.7/site-packages
Requires: setuptools, paramiko, cryptography, jinja2, PyYAML
Required-by:
ansibleman@ubuntu-pc:~$
ansibleman@ubuntu-pc:~$ ansible --version
Command 'ansible' not found, but can be installed with:
sudo apt install ansible
ansibleman@ubuntu-pc:~$ logout
ここでログアウトして、再度ログインします。
ansibleman@ubuntu-pc:~$ ansible --version
ansible 2.7.10
config file = None
configured module search path = ['/home/ansibleman/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/ansibleman/.local/lib/python3.7/site-packages/ansible
executable location = /home/ansibleman/.local/bin/ansible
python version = 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.3.0]
ansibleman@ubuntu-pc:~$
ansibleman@ubuntu-pc:~$ sudo apt install -y sshpass
[sudo] password for ansibleman:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
sshpass
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 10.5 kB of archives.
After this operation, 30.7 kB of additional disk space will be used.
Get:1 http://jp.archive.ubuntu.com/ubuntu disco/universe amd64 sshpass amd64 1.06-1 [10.5 kB]
Fetched 10.5 kB in 0s (238 kB/s)
Selecting previously unselected package sshpass.
(Reading database ... 140219 files and directories currently installed.)
Preparing to unpack .../sshpass_1.06-1_amd64.deb ...
Unpacking sshpass (1.06-1) ...
Setting up sshpass (1.06-1) ...
Processing triggers for man-db (2.8.5-2) ...
ansibleman@ubuntu-pc:~$
ansibleman@ubuntu-pc:~$ mkdir ~/.ssh
ansibleman@ubuntu-pc:~$
ansibleman@ubuntu-pc:~$ chmod 700 ~/.ssh
ansibleman@ubuntu-pc:~$
ansibleman@ubuntu-pc:~$ vi ~/.ssh/config
-- INSERT --
Host *
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
ansibleman@ubuntu-pc:~$
ansibleman@ubuntu-pc:~$ chmod 600 ~/.ssh/config
ansibleman@ubuntu-pc:~$
ansibleman@ubuntu-pc:~$ mkdir ~/ansible
ansibleman@ubuntu-pc:~$
動作確認
対象ホスト node-c0706 に対し ping モジュールを実行します。■ hosts.yml
all: hosts: node-c0706:■ host_vars/node-c0706.yml
ansible_host: 192.168.101.21 ansible_user: root ansible_password: root@node-c0706■ 実行結果
ansibleman@ubuntu-pc:~/ansible$ ansible all -i hosts.yml -m ping
node-c0706 | SUCCESS => {
"changed": false,
"ping": "pong"
}
ansibleman@ubuntu-pc:~/ansible$
Ansible 2.8 にアップデート
2019/05/16 に Ansible 2.8 がリリースされたのでアップデートしました。投入コマンドです。- pip3 install -U ansible
- ansible --version
ansibleman@ubuntu-pc:~$ pip3 install -U ansible
Collecting ansible
Downloading https://files.pythonhosted.org/packages/17/c9/d379b76ecaa42f4ee08b01c365e9ed1be0b302ff8a26eef120d481b144fa/ansible-2.8.0.tar.gz (14.3MB)
100% |████████████████████████████████| 14.3MB 113kB/s
Requirement already satisfied, skipping upgrade: PyYAML in /usr/lib/python3/dist-packages (from ansible) (3.13)
Requirement already satisfied, skipping upgrade: cryptography in /usr/lib/python3/dist-packages (from ansible) (2.3)
Requirement already satisfied, skipping upgrade: jinja2 in ./.local/lib/python3.7/site-packages (from ansible) (2.10.1)
Requirement already satisfied, skipping upgrade: MarkupSafe>=0.23 in /usr/lib/python3/dist-packages (from jinja2->ansible) (1.1.0)
Building wheels for collected packages: ansible
Running setup.py bdist_wheel for ansible ... done
Stored in directory: /home/ansibleman/.cache/pip/wheels/31/ab/70/df86c93df35db7378d7bf40e30e1f2b0c43fb179cda1d940b3
Successfully built ansible
Installing collected packages: ansible
Found existing installation: ansible 2.7.10
Uninstalling ansible-2.7.10:
Successfully uninstalled ansible-2.7.10
Successfully installed ansible-2.8.0
ansibleman@ubuntu-pc:~$
ansibleman@ubuntu-pc:~$ ansible --version
ansible 2.8.0
config file = None
configured module search path = ['/home/ansibleman/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/ansibleman/.local/lib/python3.7/site-packages/ansible
executable location = /home/ansibleman/.local/bin/ansible
python version = 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.3.0]
ansibleman@ubuntu-pc:~$
対象ホストに ping を打つ play で実行を確認した結果です。 PLAY RECAP で出力される項目が Ansibel 2.7 のときよりも増えています。
nsibleman@ubuntu-pc:~/ansible/ping$ ansible-playbook -i hosts.yml site.yml
PLAY [all] *********************************************************************
TASK [対象ホストに ping を打つ] *********************************************************
ok: [node-c0706]
ok: [node-u1804]
TASK [debug] *******************************************************************
ok: [node-u1804] => {
"result": {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"failed": false,
"ping": "pong"
}
}
ok: [node-c0706] => {
"result": {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"failed": false,
"ping": "pong"
}
}
PLAY RECAP *********************************************************************
node-c0706 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node-u1804 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansibleman@ubuntu-pc:~/ansible/ping$