Ansibleで対象がUbuntu 16.04のとき


自分用の備忘録.
AnsibleでUbuntu16.04を対象にするときの手順.

python2 がない

Ubuntu 16.04はデフォルトでpython3なのでpython2がありません.つまり.あらゆるモジュールが動きません.

無理やり python2 を入れる

ターゲットのIPアドレスは仮です.

user@ubuntu$ cat >> hosts <<__EOT__
[targets]
172.16.130.41
172.16.129.118
__EOT__

user@ubuntu$ ansible -v all -i hosts -m raw -a "sudo apt -y install python"
No config file found; using defaults
172.16.129.118 | SUCCESS | rc=0 >>
sudo: unable to resolve host test.xff: Connection timed out
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-4.4.0-36 linux-headers-4.4.0-36-generic
  linux-image-4.4.0-36-generic
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python-minimal
  python2.7 python2.7-minimal
Suggested packages:
  python-doc python-tk python2.7-doc binfmt-support
The following NEW packages will be installed:
  libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python
  python-minimal python2.7 python2.7-minimal
0 upgraded, 7 newly installed, 0 to remove and 16 not upgraded.
Need to get 3,907 kB of archives.
After this operation, 16.6 MB of additional disk space will be used.
Get:1 http://172.16.128.11/ubuntu xenial-updates/main amd64 libpython2.7-minimal amd64 2.7.12-1~16.04 [339 kB]
Get:2 http://172.16.128.11/ubuntu xenial-updates/main amd64 python2.7-minimal amd64 2.7.12-1~16.04 [1,294 kB]
Get:3 http://172.16.128.11/ubuntu xenial/main amd64 python-minimal amd64 2.7.11-1 [28.2 kB]
Get:4 http://172.16.128.11/ubuntu xenial-updates/main amd64 libpython2.7-stdlib amd64 2.7.12-1~16.04 [1,877 kB]
Get:5 http://172.16.128.11/ubuntu xenial-updates/main amd64 python2.7 amd64 2.7.12-1~16.04 [224 kB]
Get:6 http://172.16.128.11/ubuntu xenial/main amd64 libpython-stdlib amd64 2.7.11-1 [7,656 B]
Get:7 http://172.16.128.11/ubuntu xenial/main amd64 python amd64 2.7.11-1 [137 kB]
Fetched 3,907 kB in 0s (13.5 MB/s)
Selecting previously unselected package libpython2.7-minimal:amd64.
(Reading database ... 114255 files and directories currently installed.)
Preparing to unpack .../libpython2.7-minimal_2.7.12-1~16.04_amd64.deb ...
Unpacking libpython2.7-minimal:amd64 (2.7.12-1~16.04) ...
Selecting previously unselected package python2.7-minimal.
Preparing to unpack .../python2.7-minimal_2.7.12-1~16.04_amd64.deb ...
Unpacking python2.7-minimal (2.7.12-1~16.04) ...
Selecting previously unselected package python-minimal.
Preparing to unpack .../python-minimal_2.7.11-1_amd64.deb ...
Unpacking python-minimal (2.7.11-1) ...
Selecting previously unselected package libpython2.7-stdlib:amd64.
Preparing to unpack .../libpython2.7-stdlib_2.7.12-1~16.04_amd64.deb ...
Unpacking libpython2.7-stdlib:amd64 (2.7.12-1~16.04) ...
Selecting previously unselected package python2.7.
Preparing to unpack .../python2.7_2.7.12-1~16.04_amd64.deb ...
Unpacking python2.7 (2.7.12-1~16.04) ...
Selecting previously unselected package libpython-stdlib:amd64.
Preparing to unpack .../libpython-stdlib_2.7.11-1_amd64.deb ...
Unpacking libpython-stdlib:amd64 (2.7.11-1) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for mime-support (3.59ubuntu1) ...
Setting up libpython2.7-minimal:amd64 (2.7.12-1~16.04) ...
Setting up python2.7-minimal (2.7.12-1~16.04) ...
Linking and byte-compiling packages for runtime python2.7...
Setting up python-minimal (2.7.11-1) ...
Selecting previously unselected package python.
(Reading database ... 115001 files and directories currently installed.)
Preparing to unpack .../python_2.7.11-1_amd64.deb ...
Unpacking python (2.7.11-1) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up libpython2.7-stdlib:amd64 (2.7.12-1~16.04) ...
Setting up python2.7 (2.7.12-1~16.04) ...
Setting up libpython-stdlib:amd64 (2.7.11-1) ...
Setting up python (2.7.11-1) ...
Shared connection to 172.16.129.118 closed.


172.16.130.41 | SUCCESS | rc=0 >>
sudo: unable to resolve host test2: Connection timed out
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python-minimal
  python2.7 python2.7-minimal
Suggested packages:
  python-doc python-tk python2.7-doc binutils binfmt-support
The following NEW packages will be installed:
  libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python
  python-minimal python2.7 python2.7-minimal
0 upgraded, 7 newly installed, 0 to remove and 4 not upgraded.
Need to get 3,907 kB of archives.
After this operation, 16.6 MB of additional disk space will be used.
Get:1 http://172.16.128.11/ubuntu xenial-updates/main amd64 libpython2.7-minimal amd64 2.7.12-1~16.04 [339 kB]
Get:2 http://172.16.128.11/ubuntu xenial-updates/main amd64 python2.7-minimal amd64 2.7.12-1~16.04 [1,294 kB]
Get:3 http://172.16.128.11/ubuntu xenial/main amd64 python-minimal amd64 2.7.11-1 [28.2 kB]
Get:4 http://172.16.128.11/ubuntu xenial-updates/main amd64 libpython2.7-stdlib amd64 2.7.12-1~16.04 [1,877 kB]
Get:5 http://172.16.128.11/ubuntu xenial-updates/main amd64 python2.7 amd64 2.7.12-1~16.04 [224 kB]
Get:6 http://172.16.128.11/ubuntu xenial/main amd64 libpython-stdlib amd64 2.7.11-1 [7,656 B]
Get:7 http://172.16.128.11/ubuntu xenial/main amd64 python amd64 2.7.11-1 [137 kB]
Fetched 3,907 kB in 0s (34.9 MB/s)
Selecting previously unselected package libpython2.7-minimal:amd64.
(Reading database ... 53631 files and directories currently installed.)
Preparing to unpack .../libpython2.7-minimal_2.7.12-1~16.04_amd64.deb ...
Unpacking libpython2.7-minimal:amd64 (2.7.12-1~16.04) ...
Selecting previously unselected package python2.7-minimal.
Preparing to unpack .../python2.7-minimal_2.7.12-1~16.04_amd64.deb ...
Unpacking python2.7-minimal (2.7.12-1~16.04) ...
Selecting previously unselected package python-minimal.
Preparing to unpack .../python-minimal_2.7.11-1_amd64.deb ...
Unpacking python-minimal (2.7.11-1) ...
Selecting previously unselected package libpython2.7-stdlib:amd64.
Preparing to unpack .../libpython2.7-stdlib_2.7.12-1~16.04_amd64.deb ...
Unpacking libpython2.7-stdlib:amd64 (2.7.12-1~16.04) ...
Selecting previously unselected package python2.7.
Preparing to unpack .../python2.7_2.7.12-1~16.04_amd64.deb ...
Unpacking python2.7 (2.7.12-1~16.04) ...
Selecting previously unselected package libpython-stdlib:amd64.
Preparing to unpack .../libpython-stdlib_2.7.11-1_amd64.deb ...
Unpacking libpython-stdlib:amd64 (2.7.11-1) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for mime-support (3.59ubuntu1) ...
Setting up libpython2.7-minimal:amd64 (2.7.12-1~16.04) ...
Setting up python2.7-minimal (2.7.12-1~16.04) ...
Linking and byte-compiling packages for runtime python2.7...
Setting up python-minimal (2.7.11-1) ...
Selecting previously unselected package python.
(Reading database ... 54377 files and directories currently installed.)
Preparing to unpack .../python_2.7.11-1_amd64.deb ...
Unpacking python (2.7.11-1) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up libpython2.7-stdlib:amd64 (2.7.12-1~16.04) ...
Setting up python2.7 (2.7.12-1~16.04) ...
Setting up libpython-stdlib:amd64 (2.7.11-1) ...
Setting up python (2.7.11-1) ...
Shared connection to 172.16.130.41 closed.

user@ubuntu$ ansible -v all -i hosts -m ping
172.16.129.118 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
172.16.130.41 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
user@ubuntu$ ansible -v all -i hosts -m command -a uptime
172.16.129.118 | SUCCESS | rc=0 >>
 10:43:40 up  1:52,  2 users,  load average: 0.00, 0.02, 0.00

172.16.130.41 | SUCCESS | rc=0 >>
 19:43:40 up  1:52,  1 user,  load average: 0.12, 0.10, 0.05

playbookでやるとき

- hosts: all
  gather_facts: no
  remote_user: ubuntu
  become: yes
  become_user: root
  become_method: sudo

  pre_tasks:
    - raw: apt -y install python