Saturday, January 27, 2018

How to use Ansible

Prerequisites

  • I'm using windows8 and using virtual CentOS 7.3 in windows local as Ansible's host machine. If you don't have Linux or Mac, use virtual machine like I do. But in that case, don't forget to set the host's IP adress to 198.168.33.11 because duplicate IP causes an error.
  • As such, we will use two virtual machines in Windows.
    1. Host (CentOS7.3, IP:192.168.33.11).
    2. Guest (CentOS7.3, IP:192.169.33.10).
See here for virtual machines. (If you use two VMs, strictly speaking, it isn't host-guest relationship. But let me use the term for simplicity and because I don't know how to call it. Central and Others?)

Why Ansible?

Without Ansible, you need to do everything written here manually (to create PHP development environment for vagrant/virtualbox). Let's do it automatically with Ansible.

Install Ansible

At first, install python3.5 from IUS repository.
$ sudo rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ sudo yum install -y https://centos7.iuscommunity.org/ius-release.rpm
$ sudo yum install -y python35u python35u-libs python35u-devel python35u-pip
And install ansible.
$ sudo pip3.5 install ansible
$ sudo yum install -y sshpass 
And check if you have installed it successfully.
$ ansible --version
ansible 2.5.5
  config file = None
  configured module search path = ['/home/vagrant/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.5/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.5.5 (default, Feb  6 2018, 10:57:32) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
We installed ansible from pip. So we don't have /etc/ansible/ansible.cfg. You can create the cfg file by yourself. The default file can be found in their github. See their documentation too.

Add hosts information

Run the following to create hosts file in the host machine:
$ sudo mkdir /etc/ansible/
$ sudo vi /etc/ansible/hosts
Write as follows:
[nodes]
192.168.33.10
And save and close it by pressing :wq.
Also run this command too:
$ sudo vi /etc/ansible/ansible.cfg
Then write as follows:
[defaults]
host_key_checking = false
Then save and close it.

Basic command of Ansible

$ ansible 192.168.33.10 -m ping 192.168.33.10 : IP address of the machine in which you want to deploy an environment via Ansible. -m ping: m option means module. ping is the module name.
Usually ansible is used with public keys to connect to guest machines. But it's difficult (if you want to try, you can check it here for example). So we simply use password to login the guest machine. Run the following command. SSH password is vagrant:
$ sudo ansible 192.168.33.10 -m ping -u vagrant -k
If you successfully have done everything, you will see this:

Install packages with playbook

Create a playbook
$ cd /vagrant
$ sudo touch dstat.yaml
$ sudo vi /vagrant/dstat.yaml
Write as follows inside dstat.yaml
- hosts: 192.168.33.10
  tasks:
   - name: Install dstat
     become: yes
     become_method: sudo
     yum: pkg=dstat state=installed update_cache=true
Save and close it by :wq. Then use the playbook.
$ ansible-playbook -s dstat.yaml -u vagrant -k
If everything worked successfully, dstat is installed automatically in the guest machine. Check it by running
$ dstat
in the guest machine. If the following was displayed, you successfully did everything.

Create a playbook for PHP development environment

Use this playbook to create PHP development environment automatically in CentOS 7.3 whose IP address is 192.168.33.10. See here for yum operaion in Ansible.
Save the following as "php.yaml":
- hosts: 192.168.33.10
  tasks:
    - name: Update yum
      become: yes
      become_method: sudo
      yum: 
        name: '*' 
        state: latest
    - name: Install apache
      become: yes
      become_method: sudo
      yum: pkg=httpd state=latest update_cache=true
    - name: Add epel 7 to /etc/yum.repos.d/
      become: yes
      become_method: sudo
      yum_repository:
        name: epel
        description: EPEL YUM repo
        baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
        gpgkey: https://archive.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7
    - name: Add IUS repository to /etc/yum.repos.d/
      yum_repository:
        name: ius
        file: ius
        description: IUS Community Packages for Enterprise Linux 7 - $basearch
        mirrorlist: https://mirrors.iuscommunity.org/mirrorlist?repo=ius-centos7&arch=$basearch&protocol=http
        gpgkey: http://dl.iuscommunity.org/pub/ius/IUS-COMMUNITY-GPG-KEY
        enabled: yes
        gpgcheck: yes
    - name: Update yum again
      become: yes
      become_method: sudo
      yum: 
        name: '*' 
        state: latest
    - name: Install php packages
      become: yes
      become_method: sudo
      yum: 
        name:
          - php71u
          - php71u-mysqlnd
          - php71u-gd
          - php71u-mbstring
          - php71u-opcache
          - php71u-xml
          - php71u-pecl-xdebug
          - php71u-pdo
          - php71u-devel
          - php71u-json
    - name: Add MySQL repo 
      become: yes
      become_method: sudo
      yum:
        name: https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
    - name: Install MySQL
      become: yes
      become_method: sudo
      yum: 
        name: "mysql-server" 
        state: present
    - service: name=httpd state=started enabled=yes
    - service: name=mysqld state=started enabled=yes
    - name: Install other necessary packages
      become: yes
      become_method: sudo
      yum: 
        name:
          - kernel-devel
          - kernel-headers
          - dkms
          - gcc
          - gcc-c++
And use it like this:
$ ansible-playbook -s php.yaml -u vagrant -k
And the PHP development environment is automatically created in the guest machine.