Pages

Saturday, April 11, 2015

Ansible Install and Playbooks

$ sudo rpm -ivh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm

Enable ol7_optional_latest Repository for python-jinja2 package.

$ sudo yum install ansible

Edit /etc/ansible/hosts and put remote systems in it.

mail.example.com

[webservers]
web1.example.com
web2.example.com

[dbservers]
db1.example.com
db2.example.com

Ping all the nodes

$ ansible all -m ping

Run a live command on all of the nodes

$ ansible all -a "echo hello"

Playbook for Pre-requisites on all nodes :

$ cat roles/common/tasks/main.yml
---
- hosts: all
  user: vagrant
  sudo: yes

  tasks:

  - name: sudo access to Hadoop Admins

  - name: hostname must be fully qualified domain name
    ansible edge -a hostname

  - name: DNS lookup should resolve to correct IP Address
    command: nslookup {{ ansible_hostname }}
    register: ns

  - debug: var=ns.stdout

  - name: Reverse DNS lookup should be successful
    command: nslookup `host {{ ansible_hostname }} | awk '{print $4}'`

  - name: Verify sticky bit permission set on /tmp filesystem on all nodes
    ansible edge -a 'ls -ld /tmp'

  - name: Verify /etc/hosts file entries
    ansible edge -a 'cat /etc/hosts'

  - name: Verify Network configuration

  - name: Make sure the system is up to date
    yum: name=* state=latest

  - name: Ensure NTP (for time synchronization) is installed.
    yum: name=ntp state=present

  - name: Ensure NTP is running and enable it to run at boot.
    service: name=ntpd state=started enabled=yes

  - name: Install libselinux-python
    yum: name=libselinux-python state=present

  - name: Disable SELINUX
    selinux: state=disabled

  - name: Disable iptables
    service: name=iptables state=stopped enabled=no

  - name: Disable transparent huge pages on data nodes
    lineinfile: dest=/etc/rc.d/rc.local regexp='^.*transparent_hugepage.*$' line='echo never > /sys/kernel/mm/transparent_hugepage/defrag'

  - name: Execution permission
    file: path=/etc/rc.d/rc.local mode=u+x

  - name: Set swappiness to 1 on data nodes (Default is 60)
    sysctl: name=vm.swappiness value=1 state=present

  - name: Set overcommit_memory to 1 on data nodes (Default is 0)
    sysctl: name=vm.overcommit_memory value=1 state=present

Playbook for Data nodes:

---
- hosts: dn
  user: vagrant
  sudo: yes

  vars:
    fstype: ext4
    device: /dev/sdb
    mntdir: /data1

  tasks:

    - name: Check if partition present
      shell: fdisk -l | grep {{device}} | wc -l
      changed_when: False
      register: partition_present

    - name: fdisk/create partition/format/Reserved Block Count to 1%
      shell: "echo -e 'n\np\n1\n\n\nw\n' | fdisk {{ device }} && mkfs -t {{ fstype }} {{ device }}1 && tune2fs -m 1 {{ device }}1"
      when: partition_present.stdout is defined and partition_present.stdout|int == 1

    - name: Create Directory for mount
      file: path={{ mntdir }} state=directory

    - name: Mount device
      mount: name={{ mntdir }} src={{ device }}1 fstype={{ fstype }} opts="noatime,nodiratime" passno=2 state=mounted

# - name: Make sure data disks are configured with RAID10 on non-data nodes
# - name: Make sure data disks are configured as JBODs and with no RAID on data nodes
    command: pvs
    register: pvs

  - debug: var=pvs.stdout_lines

# - name: Make sure JBOD are ext4 type and mounted on /data{1..20} mount points with noatime on data nodes
    command: df -T | grep "/data"
    command: cat /etc/fstab | grep /data

# - name: Make sure data disks won't fsck based on time or number of reboots on data nodes
    command: tune2fs -l /dev/sdb1 | grep -i 'Maximum mount count|Check interval'

# - name: Make sure data disks Reserved block count is 4882414 or 1% (Default is 5%) on data nodes
    command: tune2fs -l /dev/sdb1 | grep -i 'Reserved block count'

# - name: filesystem sizes /var/log

Playbook for JDK Install:

$ cat roles/jdk/tasks/main.yml
---
- hosts: all
  user: vagrant
  sudo: yes

  vars:
    java_rpm_url: http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm
    java_archive: /tmp/jdk-7u79-linux-x64.rpm

  tasks:

    - name: Download Java Development Kit
      command: 'wget -q -O {{ java_archive }} --no-cookies --header "Cookie: gpw_e24=http://www.oracle.com/; oraclelicense=accept-securebackup-cookie" {{ java_rpm_url }} creates={{ java_archive }}'

    - name: Install JDK on all Servers
      yum: name={{ java_archive }} state=present

Playbook for MySQL Server and JDBC Driver:

$ cat roles/mysql/tasks/main.yml

---
- hosts: edge1
  user: vagrant
  sudo: yes

  vars:
    root_pw: rootpw
    jdbc_url: http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.35.tar.gz
    jdbc_archive: /tmp/mysql-connector-java-5.1.35.tar.gz
    jdbc_driver: mysql-connector-java-5.1.35/mysql-connector-java-5.1.35-bin.jar

    mysql_jdbc_driver: /usr/share/java/mysql-connector-java-5.1.35-bin.jar
    link_jdbc_driver: /usr/share/java/mysql-connector-java.jar


  tasks:

    - name: Install MySQL repo
      yum: name=http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm state=present

    - name: Install MySQL
      yum: name={{ item }} enablerepo=mysql56-community state=present
      with_items:
      - MySQL-python
      - mysql-community-server

    - name: Start mysqld
      service: name=mysqld state=started enabled=yes
    - name: Set root password
      mysql_user: login_user=root login_password="" name=root host={{ item }} password={{ root_pw }}
      with_items:
      - 127.0.0.1
      - ::1
      - localhost

    - name: Remove anonymous users
      mysql_user: login_user=root login_password={{ root_pw }} name="" host={{ item }} state=absent
      with_items:
      - localhost
      - "{{ ansible_nodename }}"
      - "{{ ansible_hostname }}"

    - name: Disallow root login remotely
      mysql_user: login_user=root login_password={{ root_pw }} name=root host={{ ansible_nodename }} state=absent

    - name: Remove test database and access to it
      mysql_db: login_user=root login_password={{ root_pw }} name=test state=absent

    - name: Create database
      mysql_db: login_user=root login_password={{ root_pw }} name={{ item }} encoding='utf8'
      with_items:
      - amon
      - smon
      - rman
      - hmon
      - nav
      - hive
      - hue
      - oozie

    - name: Create users
      mysql_user: login_user=root login_password={{ root_pw }} name={{ item.name }} password={{ item.password }} priv={{item.priv }} state=present
      with_items:
      - { name: 'amon', password: 'amonpw', priv: 'amon.*:ALL' }
      - { name: 'smon', password: 'smonpw', priv: 'smon.*:ALL' }
      - { name: 'rman', password: 'rmanpw', priv: 'rman.*:ALL' }
      - { name: 'hmon', password: 'hmonpw', priv: 'hmon.*:ALL' }
      - { name: 'hive', password: 'hivepw', priv: 'hive.*:ALL' }
      - { name: 'hue', password: 'huepw', priv: 'hue.*:ALL' }
      - { name: 'oozie', password: 'ooziepw', priv: 'oozie.*:ALL' }

    - name: Download JDBC Driver for MySQL on Edge Servers
      command: 'wget -q -O {{ jdbc_archive }} {{ jdbc_url }} creates={{ jdbc_archive }}'

    - name: Extract JDBC Driver
      command: 'tar -zxf {{ jdbc_archive }} -C /tmp {{ jdbc_driver }} creates={{ jdbc_driver }}'

    - name: Create /usr/share/java folder
      file: path=/usr/share/java state=directory

    - name: Copy to /usr/share/java
      copy: src=/tmp/{{ jdbc_driver }} dest=/usr/share/java

    - name: Create symbolic link
      file: src={{ mysql_jdbc_driver }} dest={{ link_jdbc_driver }} state=link

MySQL root password:

---
- name: MySQL root password
  user: vagrant
  sudo: yes
  hosts: cm

  tasks:
     - name: Create mysql root pass
       command: /usr/bin/openssl rand -base64 16
       register: mysql_root_passwd

     - debug: var=mysql_root_passwd

Playbook for Cloudera Repos :
---
- hosts: all
  user: vagrant
  sudo: yes

  vars:
    cloudera_repos:
      - http://archive-primary.cloudera.com/cm5/redhat/6/x86_64/cm/cloudera-manager.repo
      - http://archive-primary.cloudera.com/cdh5/redhat/6/x86_64/cdh/cloudera-cdh5.repo
      - http://archive-primary.cloudera.com/impala/redhat/6/x86_64/impala/cloudera-impala.repo

  tasks:

    - name: Install Cloudera repos
      get_url: url={{ item }} dest=/etc/yum.repos.d/ mode=0644
      with_items: cloudera_repos

Playbook for Cloudera Manger:

---
- hosts: edge1
  user: vagrant
  sudo: yes

  vars:
    mysql_root_pw: rootpw
    scm_pw: scmpw

  tasks:

    - name: Install Cloudera Manager Server
      yum: name=cloudera-manager-server state=latest

    - name: Prepare scm database
      command: '/usr/share/cmf/schema/scm_prepare_database.sh -h localhost -u root -p{{mysql_root_pw }} --scm-host localhost mysql scm scm {{ scm_pw }}'

    - name: Start Cloudera Manager Server
      service: name=cloudera-scm-server state=started enabled=yes

# Login Cloudera Manager Admin Console at http://edge1.example.com:7180/
# The default credentials are Username: admin Password: admin


Host variables:
---
- name: test playbook
  user: vagrant
  sudo: yes
  hosts: cm

  tasks:
#    - name: My message
#      debug: msg="{{ ansible_nodename }}"
#    - debug: var=result
    - name: Display hostname
      debug: var=hostvars[inventory_hostname]