** What is Ansible ** Let me briefly introduce Ansible. --It is a tool that automates server construction without agents. --It connects to the machine running sshd with SSH and builds the environment. --Write the configuration file in YAML. No programming knowledge (Ruby, Python, etc.) is required. ――It is easier than chef and puppet to build a server with several to dozens of servers.
** make module ** The make module is a new feature added from Ansible ver.2.1. make - Run targets in a Makefile
** Preparing the environment ** See previous article. Ansible First Step See the article below for how to manually install Git from source. Install the latest Git (ver.2.9) from source
playbook.yml
---
- hosts: all
remote_user: ec2-user
become: true
vars:
version: 2.9.0
prefix_dir: /usr/local
src_dir: "{{ prefix_dir }}/src"
bin_dir: "{{ prefix_dir }}/bin"
tasks:
- name: original git exist check
stat: path={{ bin_dir }}/git
register: exist_git
- debug: msg="git not installed."
when: exist_git.stat.exists == false
- name: original git version check
command: "{{ bin_dir }}/git --version warn=false"
register: org_version
changed_when: false
when: exist_git.stat.exists == true
- name: install dependences
yum: name={{ item }}
with_items:
- curl-devel
- expat-devel
- gettext-devel
- openssl-devel
- zlib-devel
- gcc
- perl-ExtUtils-MakeMaker
when: (exist_git.stat.exists == false) or
(exist_git.stat.exists == true and org_version.stdout != "git version {{ version }}")
- name: make download dir
file: path={{ src_dir }} state=directory owner=root group=root mode=755
when: (exist_git.stat.exists == false) or
(exist_git.stat.exists == true and org_version.stdout != "git version {{ version }}")
- name: download git version {{ version }}
unarchive:
src: https://www.kernel.org/pub/software/scm/git/git-{{ version }}.tar.gz
dest: "{{ src_dir }}"
copy: no
when: (exist_git.stat.exists == false) or
(exist_git.stat.exists == true and org_version.stdout != "git version {{ version }}")
- name: make all
make:
chdir: "{{ src_dir }}/git-{{ version }}"
target: all
params:
prefix: "{{ prefix_dir }}"
when: (exist_git.stat.exists == false) or
(exist_git.stat.exists == true and org_version.stdout != "git version {{ version }}")
- name: make install
make:
chdir: "{{ src_dir }}/git-{{ version }}"
target: install
params:
prefix: "{{ prefix_dir }}"
when: (exist_git.stat.exists == false) or
(exist_git.stat.exists == true and org_version.stdout != "git version {{ version }}")
The execution is as follows.
$ ansible-playbook playbook.yml
I'm doing git --version
in the command
module, but Ansible's default behavior warns me to use the git
module. Ansible's git
module doesn't have a parameter to get the executable version. So, for now, I'm using the command
module to get the version and specifying warn = false
to remove the warning prompting me to use the git
module.
Source Control Modules-git (link)
The repeating part of the same conditional judgment of when
is smarter if it is stored in a variable using the set_fact
module.
Please refer to the following articles for the handling of become
and PATH.
Set PATH equivalent to "sudo su-" using Ansible environment
Ansible's make module will be chenged
every time I run it as it is.
I want it to be changed
when I first run the PlayBook, but I want it to be ʻok instead of
changedwhen I rerun the same PlayBook. However, if you specify
changed_when: False, it will be ʻok
even though the change was made at the first execution, so you should avoid specifying changed_when: False
.
So, this time, I added a version check function that combines register
and when
.
The combination of ʻun archive and
https was the songwriter. It worked fine on Amazon Linux, but it didn't work when I set the proxy in an environment where the proxy authenticates. Probably because you didn't specify to invalidate the SSL certificate. The ʻunarchive
module has a validate_certs
option, and it is OK if you specify validate_crets = no
, but this option is not supported unless it is Ansible ver.2.2 or later.
In the environment before Ansible ver.2.1, it was necessary to download with the combination of get_url
, https
and validate_crets = no
.
This time, I tried installing Git from the source, but if you install a package that is not published by rpm from the source, it seems that this PlayBook can be applied.
Well then.
Recommended Posts