From dab096599a3d1754895416694ae0918ead465f6f Mon Sep 17 00:00:00 2001 From: AR Shankar Date: Mon, 30 Dec 2019 15:08:22 +0000 Subject: [PATCH] ansible for beginners --- README.md | 1 + ansible-vault.yml | 11 ++++++ ansible.cfg | 3 ++ copy_file.yml | 11 ++++++ create_file.yml | 10 ++++++ create_user.yml | 10 ++++++ hosts | 9 +++++ index.html | 1 + install_apache2.yml | 14 ++++++++ install_apache_httpd.yml | 34 ++++++++++++++++++ install_httpd.yml | 17 +++++++++ install_packages.yml | 17 +++++++++ pass.yml | 1 + setup-apache.yml | 6 ++++ setup-apache/.travis.yml | 29 +++++++++++++++ setup-apache/README.md | 38 ++++++++++++++++++++ setup-apache/defaults/main.yml | 3 ++ setup-apache/files/index.html | 1 + setup-apache/handlers/main.yml | 21 +++++++++++ setup-apache/tasks/main.yml | 39 ++++++++++++++++++++ setup-apache/vars/main.yml | 3 ++ setup-apache_backup.yml | 65 ++++++++++++++++++++++++++++++++++ setup-tomcat.yml | 37 +++++++++++++++++++ uninstall_httpd.yml | 29 +++++++++++++++ user.yml | 1 + vault | 1 + vault-pass.yml | 6 ++++ 27 files changed, 418 insertions(+) create mode 100644 README.md create mode 100644 ansible-vault.yml create mode 100644 ansible.cfg create mode 100644 copy_file.yml create mode 100644 create_file.yml create mode 100644 create_user.yml create mode 100644 hosts create mode 100644 index.html create mode 100644 install_apache2.yml create mode 100644 install_apache_httpd.yml create mode 100644 install_httpd.yml create mode 100644 install_packages.yml create mode 100644 pass.yml create mode 100644 setup-apache.yml create mode 100644 setup-apache/.travis.yml create mode 100644 setup-apache/README.md create mode 100644 setup-apache/defaults/main.yml create mode 100644 setup-apache/files/index.html create mode 100644 setup-apache/handlers/main.yml create mode 100644 setup-apache/tasks/main.yml create mode 100644 setup-apache/vars/main.yml create mode 100644 setup-apache_backup.yml create mode 100644 setup-tomcat.yml create mode 100644 uninstall_httpd.yml create mode 100644 user.yml create mode 160000 vault create mode 100644 vault-pass.yml diff --git a/README.md b/README.md new file mode 100644 index 0000000..a0aeac4 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# ansible_for_beginners diff --git a/ansible-vault.yml b/ansible-vault.yml new file mode 100644 index 0000000..2af2a67 --- /dev/null +++ b/ansible-vault.yml @@ -0,0 +1,11 @@ +--- +- name: ansible palybook to test ansible vault + hosts: all + become: true + vars_files: + - vault-pass.yml + tasks: + - name: clone a repo + git: + repo: https://yankils:{{ password }}@github.com/yankils/vault.git + dest: /opt/ansadmin/test-vault diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..337b290 --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,3 @@ +[defaults] +inventory = /opt/ansible/hosts + diff --git a/copy_file.yml b/copy_file.yml new file mode 100644 index 0000000..30b4ff9 --- /dev/null +++ b/copy_file.yml @@ -0,0 +1,11 @@ +--- +- name: ansible playbook to copy a file + hosts: all + become: true + tasks: + - name: copy a file + copy: + src: /opt/ansible/index.html + dest: /home/ansadmin + mode: 0600 + owner: john diff --git a/create_file.yml b/create_file.yml new file mode 100644 index 0000000..e75be1d --- /dev/null +++ b/create_file.yml @@ -0,0 +1,10 @@ +--- +- name: this playbook creates a file or dir + hosts: all + # become: true + # gather_facts: no + tasks: + - name: creating a file + file: + path: /home/ansadmin/dir1 + state: directory diff --git a/create_user.yml b/create_user.yml new file mode 100644 index 0000000..c3f7908 --- /dev/null +++ b/create_user.yml @@ -0,0 +1,10 @@ +--- +- name: this playbook is to create user + hosts: all + become: true + vars_files: + - user.yml + tasks: + - name: creating user {{ user }} + user: + name: "{{ user }}" diff --git a/hosts b/hosts new file mode 100644 index 0000000..380b343 --- /dev/null +++ b/hosts @@ -0,0 +1,9 @@ +[webservers] +172.31.30.140 +172.31.16.135 + +[appservers] +172.31.16.135 + +[dbservers] +172.31.27.43 diff --git a/index.html b/index.html new file mode 100644 index 0000000..0bc46fa --- /dev/null +++ b/index.html @@ -0,0 +1 @@ +

Welcome to Apache tomcat

diff --git a/install_apache2.yml b/install_apache2.yml new file mode 100644 index 0000000..aa41084 --- /dev/null +++ b/install_apache2.yml @@ -0,0 +1,14 @@ +--- +- name: install apache2 on ubuntu server + hosts: dbservers + become: true + tasks: + - name: install apache2 + apt: + name: apache2 + state: present + + - name: start apache2 + service: + name: apache2 + state: started diff --git a/install_apache_httpd.yml b/install_apache_httpd.yml new file mode 100644 index 0000000..eea47bb --- /dev/null +++ b/install_apache_httpd.yml @@ -0,0 +1,34 @@ +--- +- name: this playbook install httpd + hosts: all + become: true + tasks: + - name: install package + yum: + name: httpd + state: installed + when: ansible_os_family == "RedHat" + + - name: start apache + service: + name: httpd + state: started + when: ansible_os_family == "RedHat" + + - name: install apache2 + apt: + name: apache2 + state: present + when: ansible_os_family == "Debian" + + - name: start apache2 + service: + name: apache2 + state: started + when: ansible_os_family == "Debian" + + - name: copy index.html + copy: + src: /opt/ansible/index.html + dest: /var/www/html + mode: 0666 diff --git a/install_httpd.yml b/install_httpd.yml new file mode 100644 index 0000000..2050dfb --- /dev/null +++ b/install_httpd.yml @@ -0,0 +1,17 @@ +--- +- name: this playbook install httpd + hosts: webservers + become: true + tasks: + - name: install package + yum: + name: httpd + state: installed + notify: start apache + + handlers: + - name: start apache + service: + name: httpd + state: started + diff --git a/install_packages.yml b/install_packages.yml new file mode 100644 index 0000000..1322580 --- /dev/null +++ b/install_packages.yml @@ -0,0 +1,17 @@ +--- +- name: this playbook install pacakges + hosts: webservers + become: true + tasks: + - name: install package + yum: + # name: ['git', 'make', 'gcc', 'wget', 'telnet', 'gzip'] + name: "{{ item }}" + state: installed + with_items: + - git + - make + - gcc + - wget + - telnet + - gzip diff --git a/pass.yml b/pass.yml new file mode 100644 index 0000000..0e4b0c7 --- /dev/null +++ b/pass.yml @@ -0,0 +1 @@ +abc123 diff --git a/setup-apache.yml b/setup-apache.yml new file mode 100644 index 0000000..12df628 --- /dev/null +++ b/setup-apache.yml @@ -0,0 +1,6 @@ +--- +- name: this playbook install httpd + hosts: all + become: true + roles: + - setup-apache diff --git a/setup-apache/.travis.yml b/setup-apache/.travis.yml new file mode 100644 index 0000000..36bbf62 --- /dev/null +++ b/setup-apache/.travis.yml @@ -0,0 +1,29 @@ +--- +language: python +python: "2.7" + +# Use the new container infrastructure +sudo: false + +# Install ansible +addons: + apt: + packages: + - python-pip + +install: + # Install ansible + - pip install ansible + + # Check ansible version + - ansible --version + + # Create ansible.cfg with correct roles_path + - printf '[defaults]\nroles_path=../' >ansible.cfg + +script: + # Basic role syntax check + - ansible-playbook tests/test.yml -i tests/inventory --syntax-check + +notifications: + webhooks: https://galaxy.ansible.com/api/v1/notifications/ \ No newline at end of file diff --git a/setup-apache/README.md b/setup-apache/README.md new file mode 100644 index 0000000..225dd44 --- /dev/null +++ b/setup-apache/README.md @@ -0,0 +1,38 @@ +Role Name +========= + +A brief description of the role goes here. + +Requirements +------------ + +Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. + +Role Variables +-------------- + +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. + +Dependencies +------------ + +A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/setup-apache/defaults/main.yml b/setup-apache/defaults/main.yml new file mode 100644 index 0000000..85584e8 --- /dev/null +++ b/setup-apache/defaults/main.yml @@ -0,0 +1,3 @@ +--- +# defaults file for setup-apache +port: 8080 diff --git a/setup-apache/files/index.html b/setup-apache/files/index.html new file mode 100644 index 0000000..0bc46fa --- /dev/null +++ b/setup-apache/files/index.html @@ -0,0 +1 @@ +

Welcome to Apache tomcat

diff --git a/setup-apache/handlers/main.yml b/setup-apache/handlers/main.yml new file mode 100644 index 0000000..faa6bae --- /dev/null +++ b/setup-apache/handlers/main.yml @@ -0,0 +1,21 @@ +--- +# handlers file for setup-apache +- name: start apache + service: + name: httpd + state: started + +- name: start apache2 + service: + name: apache2 + state: started + +- name: restart apache + service: + name: httpd + state: restarted + +- name: restart apache2 + service: + name: apache2 + state: restarted diff --git a/setup-apache/tasks/main.yml b/setup-apache/tasks/main.yml new file mode 100644 index 0000000..f98e970 --- /dev/null +++ b/setup-apache/tasks/main.yml @@ -0,0 +1,39 @@ +--- +# tasks file for setup-apache +- name: install package + yum: + name: httpd + state: installed + when: ansible_os_family == "RedHat" + notify: start apache + +- name: install apache2 + apt: + name: apache2 + state: present + when: ansible_os_family == "Debian" + notify: start apache2 + +- name: copy index.html + copy: + src: /opt/ansible/index.html + dest: /var/www/html + mode: 0666 + +- name: Ensure the default Apache port is {{ port }} + lineinfile: + path: /etc/httpd/conf/httpd.conf + regexp: '^Listen ' + insertafter: '^#Listen ' + line: Listen {{ port }} + when: ansible_os_family == "RedHat" + notify: restart apache + +- name: Ensure the default Apache port is {{ port }} on ubuntu + lineinfile: + path: /etc/apache2/ports.conf + regexp: '^Listen ' + insertafter: "# /etc/apache2/sites-enabled/000-default.conf" + line: Listen {{ port }} + when: ansible_os_family == "Debian" + notify: restart apache2 diff --git a/setup-apache/vars/main.yml b/setup-apache/vars/main.yml new file mode 100644 index 0000000..4f60583 --- /dev/null +++ b/setup-apache/vars/main.yml @@ -0,0 +1,3 @@ +--- +# vars file for setup-apache +#port: 8082 diff --git a/setup-apache_backup.yml b/setup-apache_backup.yml new file mode 100644 index 0000000..36e1164 --- /dev/null +++ b/setup-apache_backup.yml @@ -0,0 +1,65 @@ +--- +- name: this playbook install httpd + hosts: all + become: true + vars: + port: 8082 + tasks: + - name: install package + yum: + name: httpd + state: installed + when: ansible_os_family == "RedHat" + notify: start apache + + - name: install apache2 + apt: + name: apache2 + state: present + when: ansible_os_family == "Debian" + notify: start apache2 + + - name: copy index.html + copy: + src: /opt/ansible/index.html + dest: /var/www/html + mode: 0666 + + - name: Ensure the default Apache port is {{ port }} + lineinfile: + path: /etc/httpd/conf/httpd.conf + regexp: '^Listen ' + insertafter: '^#Listen ' + line: Listen {{ port }} + when: ansible_os_family == "RedHat" + notify: restart apache + + - name: Ensure the default Apache port is {{ port }} on ubuntu + lineinfile: + path: /etc/apache2/ports.conf + regexp: '^Listen ' + insertafter: "# /etc/apache2/sites-enabled/000-default.conf" + line: Listen {{ port }} + when: ansible_os_family == "Debian" + notify: restart apache2 + + handlers: + - name: start apache + service: + name: httpd + state: started + + - name: start apache2 + service: + name: apache2 + state: started + + - name: restart apache + service: + name: httpd + state: restarted + + - name: restart apache2 + service: + name: apache2 + state: restarted diff --git a/setup-tomcat.yml b/setup-tomcat.yml new file mode 100644 index 0000000..cb067b0 --- /dev/null +++ b/setup-tomcat.yml @@ -0,0 +1,37 @@ +--- +- name: setup tomcat + hosts: all + become: true + tasks: + - name: install java + yum: + name: java + state: installed + when: ansible_os_family == "RedHat" + + - name: install java on ubuntu + apt: + name: default-jdk + state: present + when: ansible_os_family == "Debian" + + - name: download tomcat packages + get_url: + url: http://mirrors.estointernet.in/apache/tomcat/tomcat-8/v8.5.50/bin/apache-tomcat-8.5.50.tar.gz + dest: /opt + + - name: untar apache packages + unarchive: + src: /opt/apache-tomcat-8.5.50.tar.gz + dest: /opt + remote_src: yes + + - name: add execution permissions on startup.sh file + file: + path: /opt/apache-tomcat-8.5.50/bin/startup.sh + mode: 0777 + + - name: start tomcat services + shell: nohup ./startup.sh + args: + chdir: /opt/apache-tomcat-8.5.50/bin diff --git a/uninstall_httpd.yml b/uninstall_httpd.yml new file mode 100644 index 0000000..74e055e --- /dev/null +++ b/uninstall_httpd.yml @@ -0,0 +1,29 @@ +--- +- name: this playbook uninstall httpd + hosts: all + become: true + tasks: + - name: stop httpd service + service: + name: httpd + state: stopped + when: ansible_os_family == "RedHat" + + - name: uninstall httpd + yum: + name: httpd + state: removed + when: ansible_os_family == "RedHat" + + - name: stop apache2 services + service: + name: apache2 + state: stopped + when: ansible_os_family == "Debian" + + - name: uninstall apache2 + apt: + name: apache2 + state: absent + when: ansible_os_family == "Debian" + diff --git a/user.yml b/user.yml new file mode 100644 index 0000000..e8ed071 --- /dev/null +++ b/user.yml @@ -0,0 +1 @@ +user: tom diff --git a/vault b/vault new file mode 160000 index 0000000..d4c8846 --- /dev/null +++ b/vault @@ -0,0 +1 @@ +Subproject commit d4c88465de791e3926b928438c25e857792f6907 diff --git a/vault-pass.yml b/vault-pass.yml new file mode 100644 index 0000000..410c403 --- /dev/null +++ b/vault-pass.yml @@ -0,0 +1,6 @@ +$ANSIBLE_VAULT;1.1;AES256 +66643163656339343464623063636162383861333966656532323661626437393038333966626133 +6261336161623931636137336631336536666338626631610a663030646665636665346533636433 +65366234656264306262383838393632326230366465623035333036653736316539303363366131 +6633626631336431610a653032326236653062373137626433376631373664663836376236353134 +30633032323865666264613734393430613839386337343931336561363738346561