2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ansibleを使用して、CentOS 7にGit(v2.9.5)をインストールする

Last updated at Posted at 2019-09-28

Ansibleを使用して、CentOS 7にGit( ver: 2.9.5 )をインストールする

作成するに至った経緯

  • Gitを手動でインストールするのが面倒なので、自動化したかったため
  • 下記コマンドでインストールされるGitのバージョンが古いため
# yum install git

対象者

  • CentOS 7 にGit( ver: 2.9.5 )を簡単にインストールしたい方

前提条件

  • Ansibleはインストール済みであるとする
  • config、inventoryの設定は完了済みであるとする
  • 鍵生成、鍵交換、疎通確認は完了済みであるとする
  • 今回は、proxy環境下ではないものとする

実行環境

# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
# ansible --version
ansible 2.8.4
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, Aug  7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

本playbookで行っていることの概要

  • yum updateの実施
  • Gitのインストールに必要なパッケージのインストール
  • 古いGitのアンインストール
  • git-2.9.5.tar.gz のダウンロードと解凍
  • Gitのビルド
  • Gitのインストール
  • シンボリックリンクの作成

本playbook実行後に得られるもの

  • playbookを実行した環境のGitのバージョンが v.2.9.5になる

本playbookの実行方法

# ansible-playbook InstallGit_v2_9_5.yml 

出力されるログの詳細を見たい場合

# ansible-playbook InstallGit_v2_9_5.yml -v

yum updateを飛ばして実行したい場合

# ansible-playbook InstallGit_v2_9_5.yml -v --start-at="Install a list of packages"

VagrantやOpenStackなどで、他のノードに対して本playbookを実行したい場合

下記の"localhost"に、インベントリに記載した変数に変更の上、playbookを実行してください。

hosts: localhost

Git(v2.9.5)をインストールするplaybook

InstallGit_v2_9_5.yml
- name: Install Git
  hosts: localhost
  tasks:
  - name: Upgrade all packages
    yum:
      name: '*'
      state: latest

  - name: Install a list of packages
    yum:
      name:
        - autoconf
        - gcc
        - curl-devel
        - expat-devel
        - gettext-devel
        - openssl-devel
        - perl-ExtUtils-MakeMaker
        - wget
        - zlib-devel
        - "@Development tools"
      state: latest

  - name: Remove old git
    yum:
      name: git
      state: absent

  - name: Unarchive a file that needs to be downloaded
    unarchive:
      src: https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.9.5.tar.gz
      dest: /usr/local/src
      remote_src: yes

  - name: Build 'all' target as root
    make:
      chdir: /usr/local/src/git-2.9.5
      target: all
    become: yes
    become_user: root

  - name: Run 'install' target as root
    make:
      chdir: /usr/local/src/git-2.9.5
      target: install
    become: yes
    become_user: root

  - name: Create a symbolic link
    file:
      src: /usr/local/src/git-2.9.5/git
      dest: /bin/git
      owner: root
      group: root
      state: link

playbook実行時のログ

# ansible-playbook InstallGit_v2_9_5.yml

PLAY [Install Git] ********************************************************************************************

TASK [Gathering Facts] ****************************************************************************************
ok: [127.0.0.1]

TASK [Upgrade all packages] ***********************************************************************************
ok: [127.0.0.1]

TASK [Install a list of packages] *****************************************************************************
changed: [127.0.0.1]

TASK [Remove old git] *****************************************************************************************
changed: [127.0.0.1]

TASK [Unarchive a file that needs to be downloaded] ***********************************************************
ok: [127.0.0.1]

TASK [Build 'all' target as root] *****************************************************************************
changed: [127.0.0.1]

TASK [Run 'install' target as root] ***************************************************************************
changed: [127.0.0.1]

TASK [Create a symbolic link] *********************************************************************************
changed: [127.0.0.1]

PLAY RECAP ****************************************************************************************************
127.0.0.1                  : ok=8    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

"-v"のオプションをつけて実行した場合、下記のように詳細に表示されます。

# ansible-playbook InstallGit_v2_9_5.yml -v
Using /etc/ansible/ansible.cfg as config file

PLAY [Install Git] ********************************************************************************************

TASK [Gathering Facts] ****************************************************************************************
ok: [127.0.0.1]

TASK [Upgrade all packages] ***********************************************************************************
ok: [127.0.0.1] => changed=false 
  msg: ''
  rc: 0
  results:
  - Nothing to do here, all packages are up to date

TASK [Install a list of packages] *****************************************************************************
changed: [127.0.0.1] => changed=true 
  changes:
    installed:
    - gettext-devel
    updated:
    - - '@Development tools'
      - null
  msg: ''
  rc: 0
  results:
  - All packages providing autoconf are up to date
  - All packages providing gcc are up to date
  - All packages providing curl-devel are up to date
  - All packages providing expat-devel are up to date
  - All packages providing openssl-devel are up to date
  - All packages providing perl-ExtUtils-MakeMaker are up to date
  - All packages providing wget are up to date
  - All packages providing zlib-devel are up to date
  - |-
    Loaded plugins: fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
     * base: ftp-srv2.kddilabs.jp
     * epel: nrt.edge.kernel.org
     * extras: ftp-srv2.kddilabs.jp
     * updates: ftp-srv2.kddilabs.jp
    Package autoconf-2.69-11.el7.noarch already installed and latest version
    Package gcc-4.8.5-39.el7.x86_64 already installed and latest version
    Package libcurl-devel-7.29.0-54.el7.x86_64 already installed and latest version
    Package expat-devel-2.1.0-10.el7_3.x86_64 already installed and latest version
    Package 1:openssl-devel-1.0.2k-19.el7.x86_64 already installed and latest version
    Package perl-ExtUtils-MakeMaker-6.68-3.el7.noarch already installed and latest version
    Package wget-1.14-18.el7_6.1.x86_64 already installed and latest version
    Package zlib-devel-1.2.7-18.el7.x86_64 already installed and latest version
    Resolving Dependencies
    --> Running transaction check
    ---> Package gettext-devel.x86_64 0:0.19.8.1-2.el7 will be installed
    --> Finished Dependency Resolution
  
    Dependencies Resolved
  
    ================================================================================
     Package               Arch           Version                Repository    Size
    ================================================================================
    Installing:
     gettext-devel         x86_64         0.19.8.1-2.el7         base         320 k
  
    Transaction Summary
    ================================================================================
    Install  1 Package
  
    Total download size: 320 k
    Installed size: 1.4 M
    Downloading packages:
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : gettext-devel-0.19.8.1-2.el7.x86_64                          1/1
      Verifying  : gettext-devel-0.19.8.1-2.el7.x86_64                          1/1
  
    Installed:
      gettext-devel.x86_64 0:0.19.8.1-2.el7
  
    Complete!

TASK [Remove old git] *****************************************************************************************
(以下省略)

ちなみに私は、結果出力の形式をyamlに変更しております。こちらの見やすいと感じたため。
設定方法に関しては、参考URLの【Ansibleの結果出力の形式をyamlに変更する方法】を参照願います。

Gitのバージョン確認

# git --version
git version 2.9.5
# which git
/bin/git
# ls -l /bin/git
lrwxrwxrwx 1 root root 28  9月 28 19:19 /bin/git -> /usr/local/src/git-2.9.5/git

無事、バージョンが2.9.5のGitをインストールすることができました。

参考URL

今回使用したモジュール

Gitのインストール関連

Ansibleの結果出力の形式をyamlに変更する方法

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?