Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

ansible roles

======================================================
hp-serverinit.yml
======================================================
---
- hosts: DBALL
  remote_user: myadmin
  gather_facts: false
  sudo: yes

  roles:

 #       - { role: dbfolder }
        - { role: postgres_install, pgconf_max_connections: 100, pg_port: 5432, pg_directory: /usr/local/pgsql }
======================================================
internal
======================================================
[1TEST]
172.16.16.84

[DB1TEST]
172.16.16.85

[DB2TEST]
172.16.16.86

[DB3TEST]
172.16.16.87

[DBALL:children]
1TEST
DB1TEST
DB2TEST
DB3TEST
======================================================


======================================================
postgres_install   main.yml
======================================================

---

## Create Postgres user with /bin/sh

  - name: Create Postgres User
    user:
      name=postgres
      shell=/bin/sh
      password="$6$rounds=656000$3lA6vA8TcL5FR.OT$3Ia.LZMu/w.S7LjgGAv3Br23v8gOb8rD/xcl1hzhEk9h8TfuYEgq8ATH3.aX2EgliMfev3T6uB6qb6yIyw0St."
      system="yes"
      state="present"

## Install required packages before installing Postgres
#  - name: Check if postgresql is installed
  - stat: path={{pg_directory}}/data/postgresql.conf
    register: pg_avail

  - name: Install required packages
    yum: name="{{ item }}" state=present
    with_items:
    - zlib-devel
    - readline-devel
    - gcc-c++
    - tcl-devel
    when: pg_avail.stat.exists == false

  - name: Create Repository folder
    file: path="/tmp/postgres" state=directory mode=775 owner="root" group="root"
    when: pg_avail.stat.exists == false

  - name: Download postgresql-8.2.13.tar.gz
    get_url:
      url="https://ftp.postgresql.org/pub/source/v8.2.13/postgresql-8.2.13.tar.gz"
      dest="/tmp/postgres" force="no"
    when: pg_avail.stat.exists == false

  - name: Unarchive Source - Required to pass ABSOLUTE PATH to the destination
    when: pg_avail.stat.exists == false
    unarchive:
      src="/tmp/postgres/postgresql-8.2.13.tar.gz"
      dest="/tmp/postgres"
      copy="no"
      mode="775"
      owner="root" group="root"


  - name: Install Postgres
    when: pg_avail.stat.exists == false
    command: chdir=/tmp/postgres/postgresql-8.2.13 {{ item }}
    with_items:
    - ./configure --prefix={{pg_directory}} --with-tcl --with-pgport={{pg_port}} --enable-thread-safety
    - gmake all
    - gmake install


  - name: Create Postgres Data folder
    file: path={{pg_directory}}/data state=directory mode=700 owner=postgres


  - name: Initialize Postgres DB
    command: runuser -l postgres -c '{{pg_directory}}/bin/initdb -D {{pg_directory}}/data'
    args:
      creates: '{{pg_directory}}/data/postgresql.conf'

  - name: Add "psql" to the "bashrc" PATH
    when: pg_avail.stat.exists == false
    lineinfile:
      dest=/etc/bashrc
      regexp="export PATH={{pg_directory}}/bin:$PATH"
      line="export PATH={{pg_directory}}/bin:$PATH"
      state=present

  - name: Reload bashrc
    when: pg_avail.stat.exists == false
    shell: source /etc/bashrc

## Change Configurations > postgresql.conf
  - name: Postgres - Change Configurations > postgresql.conf
    lineinfile:
                  dest: '{{pg_directory}}/data/postgresql.conf'
                  regexp: "{{ item.regexp }}"
                  line: "{{ item.line }}"
                  state: present
    with_items:
    - regexp: "^#?listen_addresses"
      line: "listen_addresses = '*'"
    - regexp: "^max_connections"
      line: "max_connections = {{ pgconf_max_connections }}"
    - regexp: "^#?port"
      line: "port = {{ pg_port }}"

  #  - regexp: "^shared_buffers"
  #    line: "shared_buffers = {{ pgconf_shared_buffers }}"
  #  - regexp: "^temp_buffers"
  #    line: "temp_buffers = {{ pgconf_temp_buffers }}"
  #  - regexp: "^max_prepared_transactions"
  #    line: "max_prepared_transactions = {{ pgconf_max_prepared_transactions }}"
  #  - regexp: "^work_mem"
  #    line: "work_mem = {{ pgconf_work_mem }}"
  #  - regexp: "^maintenance_work_mem"
  #    line: "maintenance_work_mem = {{ pgconf_maintenance_work_mem }}"
  #  - regexp: "^max_fsm_pages"
  #    line: "max_fsm_pages = {{ pgconf_max_fsm_pages }}"
  #  - regexp: "^max_fsm_relations"
  #    line: "max_fsm_relations = {{ pgconf_max_fsm_relations }}"
  #  - regexp: "^wal_buffers"
  #    line: "wal_buffers = {{ pgconf_wal_buffers }}"
  #  - regexp: "^checkpoint_segments"
  #    line: "checkpoint_segments = {{ pgconf_checkpoint_segments }}"
  #  - regexp: "^random_page_cost"
  #    line: "random_page_cost = {{ pgconf_random_page_cost }}"
  #  - regexp: "^effective_cache_size"
  #    line: "effective_cache_size = {{ pgconf_effective_cache_size }}"
  #  - regexp: "^datestyle"
  #    line: "datestyle = 'iso, mdy'"
  #  - regexp: "^lc_messages"
  #    line: "lc_messages = 'en_US.UTF-8'"
  #  - regexp: "^lc_monetary"
  #    line: "lc_monetary = 'en_US.UTF-8'"
  #  - regexp: "^lc_numeric"
  #    line: "lc_numeric = 'en_US.UTF-8'"
  #  - regexp: "^lc_time"
  #    line: "lc_time = 'en_US.UTF-8'"

  - name: Postgres - Allow access > pg_hba.conf
    lineinfile: dest={{pg_directory}}/data/pg_hba.conf
                regexp="host    all         all         0.0.0.0/0             trust"
                line="host    all         all         0.0.0.0/0             trust"
                state=present

  #- name: Postgres - stop Script
  #  copy: src= {{ item }} dest=/mysystem/myadmin/ owner=postgres group=postgres mode=0755
  #   with_fileglob:
  #    - pgstart
  #    - pgstop


  - name: Postgres - start Script
    template:
        src=pgstart.j2
        dest=/mysystem/myadmin/pgstart
        owner=postgres
        group=postgres
        mode=755

  - name: Postgres - stop Script
    template:
       src=pgstop.j2
       dest=/mysystem/myadmin/pgstop
       owner=postgres
       group=postgres
       mode=755

======================================================
templates
======================================================
pgstart.j2
=======
{{pg_directory}}/bin/postmaster -D {{pg_directory}}/data &

pgstop.j2
=======
{{pg_directory}}/bin/pg_ctl -D {{pg_directory}}/data stop -m fast

======================================================
find and replace
=======================================
- lineinfile: dest={{dest}} regexp='^(.*)jvcount -eq 0(.*)$' line='if [ $jvcount -eq 0 ] && [ $USER = myuser ]'

    - replace: dest={{dest}} regexp='(.*)runing, Please stop it before starting!!!(.*)$' replace='\1runing or trying to start using a different user !!!\2'




  - replace: dest={{dest}} regexp='^(.*)30 day ago(.*)$' replace='OLDDAY1=$(date --date "15 day ago" +"%Y-%m-%d")'   //change line some words


- lineinfile: dest={{dest}} line="find /mnt/newraid/var/log/myg/sys/Old_Logs/ -type f -mtime +15 -exec rm {} \;" state=present  ///adding line if not exsist


------------------
best way
------------------
#########roles#######################
###find and 2,3 words in the line and "replace" all - BEST WAY ########
- replace: dest={{dest}} regexp='^(.*){{oldtext}}(.*)$' replace='{{newtext}}' backup=yes

###"add" line if the line not exist - BEST WAY ####### USE present and absent to add or delete
    - lineinfile: dest={{dest}} line="{{newtext}}" state=present backup=yes


##### EXAMPLES ####################
        #####- { role: hp-findAndReplace, dest: /home/uploads/test/BKbackup_sys_log.sh, oldtext: 30 day ago, newtext: OLDDAY1=$(date --date "15 day ago" +"%Y-%m-%d") }
        #####- { role: hp-findAndReplace, dest: /home/uploads/test/jbstartnode_n1, oldtext: jvcount -eq 0, newtext: "if [ $jvcount -eq 0 ] && [ $USER = myuser ]" }
        #####- { role: hp-findAndReplace, dest: /home/uploads/test/jbstartnode_n1, oldtext: echo "java_n1_ws0 process is already runing, Please stop it before starting!!!", newtext: 'echo "java_n1_ws0 process is already runing or trying to start using different user !!!"' }

        #####- { role: hp-findAndReplace, dest: /home/uploads/test/jbstartnode_n1, oldtext: echo "java_n1_ws0 process is already runing, Please stop it before starting!!!", newtext: 'echo "java_n1_ws0 process is already runing or trying to start using different user !!!"' }

        - { role: hp-findAndReplace, dest: /home/uploads/test/BKbackup_sys_log.sh, newtext: 'find /mnt/newraid/var/log/myg/sys/Old_Logs/ -type f -mtime +15 -exec rm {} \;' }


 =======================================
ignore error (unreachable)
=======================================
---
- hosts: all
  max_fail_percentage: 90
  serial: 10  //number of concurrent
  remote_user: myadmin
  gather_facts: false
  sudo: yes

=======================================

loops in templates

   {% for id in range(0,nodecount) %}

   BalancerMember http://{{server_ip}}:8{{id}}80 route=n{{ id + 1 }} loadfactor=100 timeout=120 retry=300

   {% endfor %}

-------------------------
    {% set count = 1 %}
    {% for host in groups['XMLREST'] %}
    BalancerMember http://{{ hostvars[host].inventory_hostname }}:8080 route=n{{ count }} loadfactor=100      timeout=120 disablereuse=On
    {% set count = count + 1 %}
    {% endfor %}

=======================================
insert after
  - name: Adding configuration line to nagios
    lineinfile: dest={{ nagioscfg }} insertafter="^#ANSIBLE" line="cfg_file={{ local_cfg_location }}{{ hostn.stdout }}.cfg" state=present backup=yes
    delegate_to: 172.16.16.86

//////////if use regexp="^#ANSIBLE", it will delete #ANSIBLE"

======================================
- name: Check for open ports
    wait_for: host="{{ item }}"
          port=22
          timeout=15
          delay=3
        #  state=stopped
    delegate_to: xxxxx
    with_items: "{{ groups['all'] }}"

====================================================
Tips                                                                       
====================================================
    - set_fact:
           pip: "{{ avail_ip }}"

        - name: connectivity testing
          local_action: command /home/uploads/check_post_urlall "{{ pip }}":{{avail_port}} 20
          register: webpage
          ignore_errors: yes


        - local_action: debug msg="====================================== Availability {{webpage.stdout.split(')')[0]}} ) for {{avail_ip}} port {{avail_port}} ===={{webpage.stdout.split(';')[4:8]}}======(Availability OK)========="
   
          when: webpage.stdout.split(':')[0]=="OK"

=============
         - name: set fact node 4
           set_fact: nodescount="[ 0, 1, 2, 3 ]"

           when: availservername == 'app-01' or availservername == 'app-02'

=============
         - pause: minutes=10
           when: availservername != 'APP-02'
=============
   - name: folders
           file:
             path: "{{ item.dest }}"
             owner: myuser
             group: myuser
             mode: 0775
             recurse: yes
             state: directory
           with_items:
           - { dest: '/my/jboss/' }
           - { dest: '/mnt/newraid/var/log/sys/' }
           - { dest: '/mnt/newraid/var/log/app/' }
           delegate_to: "{{ servername }}"
           become: yes
           become_method: sudo

=============
         - name: unlink
           file:
             path: /my/jboss/jboss_n{{ item|int + 1|int }}_ws0/server/default/deploy/web.ear
             state: absent
           with_items: "{{ nodescount }}"
           delegate_to: "{{ servername }}"
           become: yes
           become_user: myuser

=============
         - name: symlink
           file:
             src: /my/EAR_{{ear_name}}/web.ear   ########## new source
             dest: /my/jboss/jboss_n{{ item|int + 1|int }}_ws0/server/default/deploy/web.ear
             state: link
           with_items: "{{ nodescount }}"
           delegate_to: "{{ servername }}"
           become: yes
           become_user: myuser

=============

   - name: restart nodes
           shell: su - myuser -c "/my/restart_all.sh"

           delegate_to: "{{ servername }}"
=============
         - include: /my/roles/test_availability/tasks/main.yml avail_ip="{{availserver}}" avail_port="8{{ item }}80"
           with_items: nodescount
=============
         - local_action: command date +"%D %T"

           register: endtime
=============
 - local_action: mail
            host='127.0.0.1'
            subject="Report"
            body="Start Time = {{starttime.stdout}} \n
                  End Time = {{endtime.stdout}} \n
               
                 {{results1.stdout.split(')')[0]}} ) {{availserver}} {{servername}} port 80  {{results1.stdout.split(';')[4:8]}} {{results1.stdout.split(':')[0]}} \n
                  "
            from="hasarangaprasad@gmail.com (AnsibleServer)"
            to="HP <hasarangaprasad@gmail.com>"
=============
- name: data refresh
  uri: url="http://{{ hostvars[item[1]]['rax_addresses']['RC-CLOUD'][0]['addr'] }}:{{ item[0] }}/................AdminPage.do?ActionType={{actiontype}}" timeout=300
  delegate_to: "{{ item[1] }}"
  with_nested:
        - "{{ nodes4count }}"
        - "{{ groups['APPS'] }}"
=============

No comments:

Post a Comment