AnsibleでJenkinsをインストールしようとしたら失敗した話

AnsibleのPlaybookでSandboxサーバ（CentOS 7）にJenkinsをインストールしようとしたら失敗して、解決まで時間を要したので備忘として記載しておくことにしました。

## Jenkinsのインストールで失敗

### 事象

実行した結果は以下の通り。
**TASK [Install jenkins]** のところで失敗しています。

```terminal
$ ansible-playbook -i inventory/hosts playbook.yml 
/usr/lib64/python2.7/site-packages/cryptography/__init__.py:39: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in a future release.
  CryptographyDeprecationWarning,

PLAY [sandbox] *********************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [hostname : message.] *********************************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain] => {
    "msg": "set hostname hoge-sandbox.fugafugadomain"
}

TASK [set hostname] ****************************************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [unix_groups : Add unix groups] ***********************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain] => (item={u'state': u'present', u'gid': 5100, u'name': u'group_1'})
ok: [hoge-sandbox.fugafugadomain] => (item={u'state': u'present', u'gid': 5200, u'name': u'group_2'})

TASK [unix_users : Add unix users] *************************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain] => (item={u'comment': u'User A', u'state': u'present', u'group': u'group_1', u'name': u'user_a', u'public_keys': u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyQu7mRwnjzKBCy4arUXPlaVV5O4MVoAAS1idmrZ8iQbKvfs3PSNV7eUj0FPrQ0XkvwJgqDHx6UtnKvgKR6lwgPkwQQxlreuQKGVOb1G7z4kcKSqzpv//2ht+ptrNfRryZWNCn9X/7P8IcCJg9f6THR99izDfVd6IgDD1sBw+qV3uFFRsDaCbxY6neyWmtvwRIZk/PfPL78+6b0/1N826pw4C88ynw6MUhYglQaxJqhiTf5So8ALXfQkdjxK1CfJ7sOjETM6VxwktqtJ5dnhiY0zMLbk2H6rkb2Lb77h0Ukyrar6R1ZqNlk4PINQj+vFVcEx/y6rMhn6EgIfCa57xp foo@l4a-provisioning.localdomain', u'password': u'$6$0vBh9WomCavx0fZ9$NrZZkbuEBEUojHvFBfvTR4nPdVhHLpnlGRxgwUQkW2AM2YDERmOuvlKVc5/mJBOWoXmMkd/Eu/U6vkJuGnM2m/', u'uid': 5101})
ok: [hoge-sandbox.fugafugadomain] => (item={u'comment': u'User B', u'state': u'present', u'group': u'group_2', u'name': u'user_b', u'public_keys': u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyQu7mRwnjzKBCy4arUXPlaVV5O4MVoAAS1idmrZ8iQbKvfs3PSNV7eUj0FPrQ0XkvwJgqDHx6UtnKvgKR6lwgPkwQQxlreuQKGVOb1G7z4kcKSqzpv//2ht+ptrNfRryZWNCn9X/7P8IcCJg9f6THR99izDfVd6IgDD1sBw+qV3uFFRsDaCbxY6neyWmtvwRIZk/PfPL78+6b0/1N826pw4C88ynw6MUhYglQaxJqhiTf5So8ALXfQkdjxK1CfJ7sOjETM6VxwktqtJ5dnhiY0zMLbk2H6rkb2Lb77h0Ukyrar6R1ZqNlk4PINQj+vFVcEx/y6rMhn6EgIfCa57xp foo@l4a-provisioning.localdomain', u'password': u'$6$0vBh9WomCavx0fZ9$NrZZkbuEBEUojHvFBfvTR4nPdVhHLpnlGRxgwUQkW2AM2YDERmOuvlKVc5/mJBOWoXmMkd/Eu/U6vkJuGnM2m/', u'uid': 5201})

TASK [unix_users : Set authorized key] *********************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain] => (item={u'comment': u'User A', u'state': u'present', u'group': u'group_1', u'name': u'user_a', u'public_keys': u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyQu7mRwnjzKBCy4arUXPlaVV5O4MVoAAS1idmrZ8iQbKvfs3PSNV7eUj0FPrQ0XkvwJgqDHx6UtnKvgKR6lwgPkwQQxlreuQKGVOb1G7z4kcKSqzpv//2ht+ptrNfRryZWNCn9X/7P8IcCJg9f6THR99izDfVd6IgDD1sBw+qV3uFFRsDaCbxY6neyWmtvwRIZk/PfPL78+6b0/1N826pw4C88ynw6MUhYglQaxJqhiTf5So8ALXfQkdjxK1CfJ7sOjETM6VxwktqtJ5dnhiY0zMLbk2H6rkb2Lb77h0Ukyrar6R1ZqNlk4PINQj+vFVcEx/y6rMhn6EgIfCa57xp foo@l4a-provisioning.localdomain', u'password': u'$6$0vBh9WomCavx0fZ9$NrZZkbuEBEUojHvFBfvTR4nPdVhHLpnlGRxgwUQkW2AM2YDERmOuvlKVc5/mJBOWoXmMkd/Eu/U6vkJuGnM2m/', u'uid': 5101})
ok: [hoge-sandbox.fugafugadomain] => (item={u'comment': u'User B', u'state': u'present', u'group': u'group_2', u'name': u'user_b', u'public_keys': u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyQu7mRwnjzKBCy4arUXPlaVV5O4MVoAAS1idmrZ8iQbKvfs3PSNV7eUj0FPrQ0XkvwJgqDHx6UtnKvgKR6lwgPkwQQxlreuQKGVOb1G7z4kcKSqzpv//2ht+ptrNfRryZWNCn9X/7P8IcCJg9f6THR99izDfVd6IgDD1sBw+qV3uFFRsDaCbxY6neyWmtvwRIZk/PfPL78+6b0/1N826pw4C88ynw6MUhYglQaxJqhiTf5So8ALXfQkdjxK1CfJ7sOjETM6VxwktqtJ5dnhiY0zMLbk2H6rkb2Lb77h0Ukyrar6R1ZqNlk4PINQj+vFVcEx/y6rMhn6EgIfCa57xp foo@l4a-provisioning.localdomain', u'password': u'$6$0vBh9WomCavx0fZ9$NrZZkbuEBEUojHvFBfvTR4nPdVhHLpnlGRxgwUQkW2AM2YDERmOuvlKVc5/mJBOWoXmMkd/Eu/U6vkJuGnM2m/', u'uid': 5201})

TASK [httpd : Install http server] *************************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [httpd : Start http server] ***************************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [httpd : Set 00-mpm.conf] *****************************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [sudoers/allow-all : template] ************************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [sudoers/allow-httpd : template] **********************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [yum_repository/jenkins : Install jenkins yum repository] *********************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [yum_repository/jenkins : Install jenkins-ci.org.key] *************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [Install jenkins] *************************************************************************************************************************************************************************************
fatal: [hoge-sandbox.fugafugadomain]: FAILED! => {"changed": false, "changes": {"installed": ["jenkins"]}, "msg": "warning: /var/cache/yum/x86_64/7/jenkins/packages/jenkins-2.235.4-1.1.noarch.rpm: Header V4 RSA/SHA512 Signature, key ID 45f2c3d5: NOKEY\n\n\nPublic key for jenkins-2.235.4-1.1.noarch.rpm is not installed\n", "rc": 1, "results": ["Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: ftp.iij.ad.jp\n * extras: ftp.iij.ad.jp\n * updates: ftp.iij.ad.jp\nResolving Dependencies\n--> Running transaction check\n---> Package jenkins.noarch 0:2.235.4-1.1 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package          Arch            Version                Repository        Size\n================================================================================\nInstalling:\n jenkins          noarch          2.235.4-1.1            jenkins           63 M\n\nTransaction Summary\n================================================================================\nInstall  1 Package\n\nTotal size: 63 M\nInstalled size: 63 M\nDownloading packages:\n"]}

PLAY RECAP *************************************************************************************************************************************************************************************************
hoge-sandbox.fugafugadomain    : ok=13   changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0 
```

### 解決方法

ここに回答がありました。 -> [Jenkins Redhat Packages](http://mirrors.jenkins-ci.org/redhat/)

*WARNING: The gpg key use to sign our packages has been updated on 16th of April 2020, therefore you need to reimport it if you imported before this date.*

```terminal
[root@hoge-sandbox ~]# yum install wget
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
Resolving Dependencies
--> Running transaction check
---> Package wget.x86_64 0:1.14-18.el7_6.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================================================
 Package                                       Arch                                            Version                                                  Repository                                     Size
============================================================================================================================================================================================================
Installing:
 wget                                          x86_64                                          1.14-18.el7_6.1                                          base                                          547 k

Transaction Summary
============================================================================================================================================================================================================
Install  1 Package

Total download size: 547 k
Installed size: 2.0 M
Is this ok [y/d/N]: y
Downloading packages:
wget-1.14-18.el7_6.1.x86_64.rpm                                                                                                                                                      | 547 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : wget-1.14-18.el7_6.1.x86_64                                                                                                                                                              1/1 
  Verifying  : wget-1.14-18.el7_6.1.x86_64                                                                                                                                                              1/1 

Installed:
  wget.x86_64 0:1.14-18.el7_6.1                                                                                                                                                                             

Complete!
[root@hoge-sandbox ~]# 
[root@hoge-sandbox ~]# sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo
--2020-08-17 11:18:50--  https://pkg.jenkins.io/redhat/jenkins.repo
Resolving pkg.jenkins.io (pkg.jenkins.io)... 151.101.110.133, 2a04:4e42:1a::645
Connecting to pkg.jenkins.io (pkg.jenkins.io)|151.101.110.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 71
Saving to: ‘/etc/yum.repos.d/jenkins.repo’

100%[==================================================================================================================================================================>] 71          --.-K/s   in 0s      

2020-08-17 11:18:50 (4.24 MB/s) - ‘/etc/yum.repos.d/jenkins.repo’ saved [71/71]

[root@hoge-sandbox ~]# 
[root@hoge-sandbox ~]# sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
```

## Jenkinsサーバのスタートで失敗

### 事象

上記でJenkinsのインストールはうまくいったのですが、次のTaskで失敗しました。
**TASK [Start Jenkins Server]**のところで失敗しています。（最初の方は割愛）

```terminal
TASK [Install jenkins] *************************************************************************************************************************************************************************************
changed: [hoge-sandbox.fugafugadomain]

TASK [Start jenkins server] ********************************************************************************************************************************************************************************
fatal: [hoge-sandbox.fugafugadomain]: FAILED! => {"changed": false, "msg": "Unable to start service jenkins: Job for jenkins.service failed because the control process exited with error code. See \"systemctl status jenkins.service\" and \"journalctl -xe\" for details.\n"}

PLAY RECAP *************************************************************************************************************************************************************************************************
hoge-sandbox.fugafugadomain    : ok=14   changed=2    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0 
```

### 解決方法

OpenJDK 8か11をインストールすることが必要、とのことでした。 -> [Jenkins を起動しようとすると "Job for jenkins.service failed because the control process exited with error code" と怒られる](https://yohei-a.hatenablog.jp/entry/20200217/1581896621)

めんどいので、手動でインストールを実行。（表示の途中は省略）

```terminal
[root@hoge-sandbox ~]# yum -y install java-1.8.0-openjdk-devel.x86_64
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
Resolving Dependencies
--> Running transaction check
---> Package java-1.8.0-openjdk-devel.x86_64 1:1.8.0.262.b10-0.el7_8 will be installed

〜だいぶ省略〜

  mesa-libgbm.x86_64 0:18.3.4-7.el7_8.1          mesa-libglapi.x86_64 0:18.3.4-7.el7_8.1           pango.x86_64 0:1.42.4-4.el7_7                              pcsc-lite-libs.x86_64 0:1.8.8-8.el7        
  pixman.x86_64 0:0.34.0-1.el7                   python-javapackages.noarch 0:3.4.1-11.el7         python-lxml.x86_64 0:3.2.1-4.el7                           ttmkfdir.x86_64 0:3.0.9-42.el7             
  tzdata-java.noarch 0:2020a-1.el7               xorg-x11-font-utils.x86_64 1:7.5-21.el7           xorg-x11-fonts-Type1.noarch 0:7.5-9.el7                   

Complete!
[root@hoge-sandbox ~]# 
```

ここまでやって、全てAnsibleのPlaybookが実行できるようになりました。

```terminal
$ ansible-playbook -i inventory/hosts playbook.yml 
/usr/lib64/python2.7/site-packages/cryptography/__init__.py:39: CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, and will be removed in a future release.
  CryptographyDeprecationWarning,

PLAY [sandbox] *********************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [hostname : message.] *********************************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain] => {
    "msg": "set hostname hoge-sandbox.fugafugadomain"
}

TASK [set hostname] ****************************************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [unix_groups : Add unix groups] ***********************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain] => (item={u'state': u'present', u'gid': 5100, u'name': u'group_1'})
ok: [hoge-sandbox.fugafugadomain] => (item={u'state': u'present', u'gid': 5200, u'name': u'group_2'})

TASK [unix_users : Add unix users] *************************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain] => (item={u'comment': u'User A', u'state': u'present', u'group': u'group_1', u'name': u'user_a', u'public_keys': u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyQu7mRwnjzKBCy4arUXPlaVV5O4MVoAAS1idmrZ8iQbKvfs3PSNV7eUj0FPrQ0XkvwJgqDHx6UtnKvgKR6lwgPkwQQxlreuQKGVOb1G7z4kcKSqzpv//2ht+ptrNfRryZWNCn9X/7P8IcCJg9f6THR99izDfVd6IgDD1sBw+qV3uFFRsDaCbxY6neyWmtvwRIZk/PfPL78+6b0/1N826pw4C88ynw6MUhYglQaxJqhiTf5So8ALXfQkdjxK1CfJ7sOjETM6VxwktqtJ5dnhiY0zMLbk2H6rkb2Lb77h0Ukyrar6R1ZqNlk4PINQj+vFVcEx/y6rMhn6EgIfCa57xp foo@l4a-provisioning.localdomain', u'password': u'$6$0vBh9WomCavx0fZ9$NrZZkbuEBEUojHvFBfvTR4nPdVhHLpnlGRxgwUQkW2AM2YDERmOuvlKVc5/mJBOWoXmMkd/Eu/U6vkJuGnM2m/', u'uid': 5101})
ok: [hoge-sandbox.fugafugadomain] => (item={u'comment': u'User B', u'state': u'present', u'group': u'group_2', u'name': u'user_b', u'public_keys': u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyQu7mRwnjzKBCy4arUXPlaVV5O4MVoAAS1idmrZ8iQbKvfs3PSNV7eUj0FPrQ0XkvwJgqDHx6UtnKvgKR6lwgPkwQQxlreuQKGVOb1G7z4kcKSqzpv//2ht+ptrNfRryZWNCn9X/7P8IcCJg9f6THR99izDfVd6IgDD1sBw+qV3uFFRsDaCbxY6neyWmtvwRIZk/PfPL78+6b0/1N826pw4C88ynw6MUhYglQaxJqhiTf5So8ALXfQkdjxK1CfJ7sOjETM6VxwktqtJ5dnhiY0zMLbk2H6rkb2Lb77h0Ukyrar6R1ZqNlk4PINQj+vFVcEx/y6rMhn6EgIfCa57xp foo@l4a-provisioning.localdomain', u'password': u'$6$0vBh9WomCavx0fZ9$NrZZkbuEBEUojHvFBfvTR4nPdVhHLpnlGRxgwUQkW2AM2YDERmOuvlKVc5/mJBOWoXmMkd/Eu/U6vkJuGnM2m/', u'uid': 5201})

TASK [unix_users : Set authorized key] *********************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain] => (item={u'comment': u'User A', u'state': u'present', u'group': u'group_1', u'name': u'user_a', u'public_keys': u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyQu7mRwnjzKBCy4arUXPlaVV5O4MVoAAS1idmrZ8iQbKvfs3PSNV7eUj0FPrQ0XkvwJgqDHx6UtnKvgKR6lwgPkwQQxlreuQKGVOb1G7z4kcKSqzpv//2ht+ptrNfRryZWNCn9X/7P8IcCJg9f6THR99izDfVd6IgDD1sBw+qV3uFFRsDaCbxY6neyWmtvwRIZk/PfPL78+6b0/1N826pw4C88ynw6MUhYglQaxJqhiTf5So8ALXfQkdjxK1CfJ7sOjETM6VxwktqtJ5dnhiY0zMLbk2H6rkb2Lb77h0Ukyrar6R1ZqNlk4PINQj+vFVcEx/y6rMhn6EgIfCa57xp foo@l4a-provisioning.localdomain', u'password': u'$6$0vBh9WomCavx0fZ9$NrZZkbuEBEUojHvFBfvTR4nPdVhHLpnlGRxgwUQkW2AM2YDERmOuvlKVc5/mJBOWoXmMkd/Eu/U6vkJuGnM2m/', u'uid': 5101})
ok: [hoge-sandbox.fugafugadomain] => (item={u'comment': u'User B', u'state': u'present', u'group': u'group_2', u'name': u'user_b', u'public_keys': u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyQu7mRwnjzKBCy4arUXPlaVV5O4MVoAAS1idmrZ8iQbKvfs3PSNV7eUj0FPrQ0XkvwJgqDHx6UtnKvgKR6lwgPkwQQxlreuQKGVOb1G7z4kcKSqzpv//2ht+ptrNfRryZWNCn9X/7P8IcCJg9f6THR99izDfVd6IgDD1sBw+qV3uFFRsDaCbxY6neyWmtvwRIZk/PfPL78+6b0/1N826pw4C88ynw6MUhYglQaxJqhiTf5So8ALXfQkdjxK1CfJ7sOjETM6VxwktqtJ5dnhiY0zMLbk2H6rkb2Lb77h0Ukyrar6R1ZqNlk4PINQj+vFVcEx/y6rMhn6EgIfCa57xp foo@l4a-provisioning.localdomain', u'password': u'$6$0vBh9WomCavx0fZ9$NrZZkbuEBEUojHvFBfvTR4nPdVhHLpnlGRxgwUQkW2AM2YDERmOuvlKVc5/mJBOWoXmMkd/Eu/U6vkJuGnM2m/', u'uid': 5201})

TASK [httpd : Install http server] *************************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [httpd : Start http server] ***************************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [httpd : Set 00-mpm.conf] *****************************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [sudoers/allow-all : template] ************************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [sudoers/allow-httpd : template] **********************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [yum_repository/jenkins : Install jenkins yum repository] *********************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [yum_repository/jenkins : Install jenkins-ci.org.key] *************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [Install jenkins] *************************************************************************************************************************************************************************************
ok: [hoge-sandbox.fugafugadomain]

TASK [Start jenkins server] ********************************************************************************************************************************************************************************
changed: [hoge-sandbox.fugafugadomain]

PLAY RECAP *************************************************************************************************************************************************************************************************
hoge-sandbox.fugafugadomain    : ok=15   changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
```

## 感想

思いの外、もの凄く時間がかかりました。
調べていると色々発見があって面白いですが、もう少し効率よく調べたいものです。