From d9daa4228281d57463809e2c7e30137961c7b569 Mon Sep 17 00:00:00 2001 From: Sanh Doan Date: Fri, 4 Jun 2021 21:24:00 +0700 Subject: [PATCH 1/3] sanhdp - ch4 role exercise --- ch4-role/sanhdp/ansible.cfg | 3 ++ .../sanhdp/inventory/group_vars/centos.yaml | 1 + .../sanhdp/inventory/group_vars/ubuntu.yaml | 2 + ch4-role/sanhdp/inventory/hosts | 13 +++++ ch4-role/sanhdp/roles/apache2/.travis.yml | 29 +++++++++++ ch4-role/sanhdp/roles/apache2/README.md | 38 ++++++++++++++ .../sanhdp/roles/apache2/defaults/main.yml | 3 ++ .../sanhdp/roles/apache2/handlers/main.yml | 7 +++ ch4-role/sanhdp/roles/apache2/meta/main.yml | 52 +++++++++++++++++++ ch4-role/sanhdp/roles/apache2/tasks/main.yml | 11 ++++ .../roles/apache2/templates/index.html.j2 | 1 + ch4-role/sanhdp/roles/apache2/tests/inventory | 2 + ch4-role/sanhdp/roles/apache2/tests/test.yml | 5 ++ ch4-role/sanhdp/roles/apache2/vars/main.yml | 2 + ch4-role/sanhdp/site.yaml | 18 +++++++ 15 files changed, 187 insertions(+) create mode 100644 ch4-role/sanhdp/ansible.cfg create mode 100644 ch4-role/sanhdp/inventory/group_vars/centos.yaml create mode 100644 ch4-role/sanhdp/inventory/group_vars/ubuntu.yaml create mode 100644 ch4-role/sanhdp/inventory/hosts create mode 100644 ch4-role/sanhdp/roles/apache2/.travis.yml create mode 100644 ch4-role/sanhdp/roles/apache2/README.md create mode 100644 ch4-role/sanhdp/roles/apache2/defaults/main.yml create mode 100644 ch4-role/sanhdp/roles/apache2/handlers/main.yml create mode 100644 ch4-role/sanhdp/roles/apache2/meta/main.yml create mode 100644 ch4-role/sanhdp/roles/apache2/tasks/main.yml create mode 100644 ch4-role/sanhdp/roles/apache2/templates/index.html.j2 create mode 100644 ch4-role/sanhdp/roles/apache2/tests/inventory create mode 100644 ch4-role/sanhdp/roles/apache2/tests/test.yml create mode 100644 ch4-role/sanhdp/roles/apache2/vars/main.yml create mode 100644 ch4-role/sanhdp/site.yaml diff --git a/ch4-role/sanhdp/ansible.cfg b/ch4-role/sanhdp/ansible.cfg new file mode 100644 index 0000000..9119ec0 --- /dev/null +++ b/ch4-role/sanhdp/ansible.cfg @@ -0,0 +1,3 @@ +[defaults] +host_key_checking = False +inventory = inventory \ No newline at end of file diff --git a/ch4-role/sanhdp/inventory/group_vars/centos.yaml b/ch4-role/sanhdp/inventory/group_vars/centos.yaml new file mode 100644 index 0000000..abed043 --- /dev/null +++ b/ch4-role/sanhdp/inventory/group_vars/centos.yaml @@ -0,0 +1 @@ +apache: httpd \ No newline at end of file diff --git a/ch4-role/sanhdp/inventory/group_vars/ubuntu.yaml b/ch4-role/sanhdp/inventory/group_vars/ubuntu.yaml new file mode 100644 index 0000000..c77a412 --- /dev/null +++ b/ch4-role/sanhdp/inventory/group_vars/ubuntu.yaml @@ -0,0 +1,2 @@ +ansible_python_interpreter: /usr/bin/python3 +apache: apache2 \ No newline at end of file diff --git a/ch4-role/sanhdp/inventory/hosts b/ch4-role/sanhdp/inventory/hosts new file mode 100644 index 0000000..e02db0e --- /dev/null +++ b/ch4-role/sanhdp/inventory/hosts @@ -0,0 +1,13 @@ +[ubuntu] +ubuntu11 ansible_host=192.168.100.11 +ubuntu12 ansible_host=192.168.100.12 + +[centos] +centos21 ansible_host=192.168.100.21 +centos22 ansible_host=192.168.100.22 + + +[linux:children] +ubuntu +centos + diff --git a/ch4-role/sanhdp/roles/apache2/.travis.yml b/ch4-role/sanhdp/roles/apache2/.travis.yml new file mode 100644 index 0000000..36bbf62 --- /dev/null +++ b/ch4-role/sanhdp/roles/apache2/.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/ch4-role/sanhdp/roles/apache2/README.md b/ch4-role/sanhdp/roles/apache2/README.md new file mode 100644 index 0000000..225dd44 --- /dev/null +++ b/ch4-role/sanhdp/roles/apache2/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/ch4-role/sanhdp/roles/apache2/defaults/main.yml b/ch4-role/sanhdp/roles/apache2/defaults/main.yml new file mode 100644 index 0000000..163aa41 --- /dev/null +++ b/ch4-role/sanhdp/roles/apache2/defaults/main.yml @@ -0,0 +1,3 @@ +--- +# defaults file for apache2 +welcome_user: sanhdp \ No newline at end of file diff --git a/ch4-role/sanhdp/roles/apache2/handlers/main.yml b/ch4-role/sanhdp/roles/apache2/handlers/main.yml new file mode 100644 index 0000000..8d7a3a0 --- /dev/null +++ b/ch4-role/sanhdp/roles/apache2/handlers/main.yml @@ -0,0 +1,7 @@ +--- +# handlers file for apache2 +- name: Restart Apache + service: + name: "{{ apache }}" + state: restarted + enabled: yes \ No newline at end of file diff --git a/ch4-role/sanhdp/roles/apache2/meta/main.yml b/ch4-role/sanhdp/roles/apache2/meta/main.yml new file mode 100644 index 0000000..6656eaa --- /dev/null +++ b/ch4-role/sanhdp/roles/apache2/meta/main.yml @@ -0,0 +1,52 @@ +galaxy_info: + author: Sanh Doan + description: your role description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.1 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # + # Provide a list of supported platforms, and for each platform a list of versions. + # If you don't wish to enumerate all versions for a particular platform, use 'all'. + # To view available platforms and versions (or releases), visit: + # https://galaxy.ansible.com/api/v1/platforms/ + # + # platforms: + # - name: Fedora + # versions: + # - all + # - 25 + # - name: SomePlatform + # versions: + # - all + # - 1.0 + # - 7 + # - 99.99 + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. diff --git a/ch4-role/sanhdp/roles/apache2/tasks/main.yml b/ch4-role/sanhdp/roles/apache2/tasks/main.yml new file mode 100644 index 0000000..f5da3db --- /dev/null +++ b/ch4-role/sanhdp/roles/apache2/tasks/main.yml @@ -0,0 +1,11 @@ +--- +# tasks file for apache2 +- name: Install Apache + package: + name: "{{ apache }}" + +- name: Copy welcome page + template: + src: "templates/index.html.j2" + dest: "/var/www/html/index.html" + notify: Restart Apache \ No newline at end of file diff --git a/ch4-role/sanhdp/roles/apache2/templates/index.html.j2 b/ch4-role/sanhdp/roles/apache2/templates/index.html.j2 new file mode 100644 index 0000000..086c866 --- /dev/null +++ b/ch4-role/sanhdp/roles/apache2/templates/index.html.j2 @@ -0,0 +1 @@ +Hello {{ welcome_user }} \ No newline at end of file diff --git a/ch4-role/sanhdp/roles/apache2/tests/inventory b/ch4-role/sanhdp/roles/apache2/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/ch4-role/sanhdp/roles/apache2/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/ch4-role/sanhdp/roles/apache2/tests/test.yml b/ch4-role/sanhdp/roles/apache2/tests/test.yml new file mode 100644 index 0000000..7d2d6da --- /dev/null +++ b/ch4-role/sanhdp/roles/apache2/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - apache2 diff --git a/ch4-role/sanhdp/roles/apache2/vars/main.yml b/ch4-role/sanhdp/roles/apache2/vars/main.yml new file mode 100644 index 0000000..5d23ceb --- /dev/null +++ b/ch4-role/sanhdp/roles/apache2/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for apache2 diff --git a/ch4-role/sanhdp/site.yaml b/ch4-role/sanhdp/site.yaml new file mode 100644 index 0000000..fb83d8d --- /dev/null +++ b/ch4-role/sanhdp/site.yaml @@ -0,0 +1,18 @@ +- name: Setup Apache web server + hosts: all + gather_facts: yes + become: yes + + tasks: + - name: Setup Apache using role + import_role: + name: apache2 + + - name: Check the welcome page + shell: cat /var/www/html/index.html + register: result + + - name: Display welcome page + debug: + msg: "{{ result.stdout }}" + \ No newline at end of file From 2c6099de95cfc2fc19ec7c189ae583c6aa1d1719 Mon Sep 17 00:00:00 2001 From: Sanh Doan Date: Fri, 4 Jun 2021 21:57:30 +0700 Subject: [PATCH 2/3] fix comment --- ch4-role/sanhdp/roles/apache2/meta/main.yml | 22 ++++++++------------ ch4-role/sanhdp/roles/apache2/tasks/main.yml | 3 ++- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/ch4-role/sanhdp/roles/apache2/meta/main.yml b/ch4-role/sanhdp/roles/apache2/meta/main.yml index 6656eaa..35eae5d 100644 --- a/ch4-role/sanhdp/roles/apache2/meta/main.yml +++ b/ch4-role/sanhdp/roles/apache2/meta/main.yml @@ -16,7 +16,7 @@ galaxy_info: # - CC-BY-4.0 license: license (GPL-2.0-or-later, MIT, etc) - min_ansible_version: 2.1 + min_ansible_version: "2.11" # If this a Container Enabled role, provide the minimum Ansible Container version. # min_ansible_container_version: @@ -26,18 +26,14 @@ galaxy_info: # If you don't wish to enumerate all versions for a particular platform, use 'all'. # To view available platforms and versions (or releases), visit: # https://galaxy.ansible.com/api/v1/platforms/ - # - # platforms: - # - name: Fedora - # versions: - # - all - # - 25 - # - name: SomePlatform - # versions: - # - all - # - 1.0 - # - 7 - # - 99.99 + + platforms: + - name: EL + versions: + - "7" + - name: Ubuntu + versions: + - all galaxy_tags: [] # List tags for your role here, one per line. A tag is a keyword that describes diff --git a/ch4-role/sanhdp/roles/apache2/tasks/main.yml b/ch4-role/sanhdp/roles/apache2/tasks/main.yml index f5da3db..0ddac06 100644 --- a/ch4-role/sanhdp/roles/apache2/tasks/main.yml +++ b/ch4-role/sanhdp/roles/apache2/tasks/main.yml @@ -8,4 +8,5 @@ template: src: "templates/index.html.j2" dest: "/var/www/html/index.html" - notify: Restart Apache \ No newline at end of file + notify: + - Restart Apache \ No newline at end of file From bf9505938fe82460327ca390c3df23bed324dbf0 Mon Sep 17 00:00:00 2001 From: Sanh Doan Date: Mon, 7 Jun 2021 21:09:59 +0700 Subject: [PATCH 3/3] fix comment --- ch4-role/sanhdp/roles/apache2/meta/main.yml | 2 +- ch4-role/sanhdp/roles/apache2/tasks/main.yml | 1 + ch4-role/sanhdp/roles/apache2/vars/main.yml | 2 -- ch4-role/sanhdp/site.yaml | 2 +- 4 files changed, 3 insertions(+), 4 deletions(-) delete mode 100644 ch4-role/sanhdp/roles/apache2/vars/main.yml diff --git a/ch4-role/sanhdp/roles/apache2/meta/main.yml b/ch4-role/sanhdp/roles/apache2/meta/main.yml index 35eae5d..f49419d 100644 --- a/ch4-role/sanhdp/roles/apache2/meta/main.yml +++ b/ch4-role/sanhdp/roles/apache2/meta/main.yml @@ -33,7 +33,7 @@ galaxy_info: - "7" - name: Ubuntu versions: - - all + - "bionic" galaxy_tags: [] # List tags for your role here, one per line. A tag is a keyword that describes diff --git a/ch4-role/sanhdp/roles/apache2/tasks/main.yml b/ch4-role/sanhdp/roles/apache2/tasks/main.yml index 0ddac06..1a2348b 100644 --- a/ch4-role/sanhdp/roles/apache2/tasks/main.yml +++ b/ch4-role/sanhdp/roles/apache2/tasks/main.yml @@ -3,6 +3,7 @@ - name: Install Apache package: name: "{{ apache }}" + state: present - name: Copy welcome page template: diff --git a/ch4-role/sanhdp/roles/apache2/vars/main.yml b/ch4-role/sanhdp/roles/apache2/vars/main.yml deleted file mode 100644 index 5d23ceb..0000000 --- a/ch4-role/sanhdp/roles/apache2/vars/main.yml +++ /dev/null @@ -1,2 +0,0 @@ ---- -# vars file for apache2 diff --git a/ch4-role/sanhdp/site.yaml b/ch4-role/sanhdp/site.yaml index fb83d8d..0533b5c 100644 --- a/ch4-role/sanhdp/site.yaml +++ b/ch4-role/sanhdp/site.yaml @@ -9,7 +9,7 @@ name: apache2 - name: Check the welcome page - shell: cat /var/www/html/index.html + shell: curl "{{ inventory_hostname }}" register: result - name: Display welcome page