Skip to content

Latest commit

 

History

History
105 lines (79 loc) · 2.77 KB

Host-Variables.md

File metadata and controls

105 lines (79 loc) · 2.77 KB

LAB: Host Variables

This scenario shows:

  • how to create host variables

Prerequisite

Steps

  • Create 'host_vars' directory
mkdir host_vars

image

  • If the nodes in the inventory are defined with IPs, create file with 'IP.yml' (if they are defined with domain name, files are 'domain_name.yml')

  • Add following variable into the file:

apache_package_name: apache2
apache_service: apache2
php_package_name: libapache2-mod-php

image

  • We have host.yml files and host_vars in the yml file

image

  • Update 'roles/web_servers/tasks/main.yml' by adding variables (with quotation mark ") that are defined in files in host_vars directory.
  • For each node, defined host variables are used for variables.
- name: install apache and php
  tags: ubuntu,apache,apache2
  apt:
    name:
      - "{{ apache_package_name }}"
      - "{{ php_package_name }}"
    state: latest

- name: start apache
  tags: ubuntu,apache,apache2
  service:
    name: "{{ apache_service }}"
    state: started
    enabled: yes

- name: change email address for admin (Ubuntu)
  tags: ubuntu,apache,apache2
  lineinfile:
    path: /etc/apache2/sites-available/000-default.conf
    regexp: 'ServerAdmin webmaster@localhost'
    line: '       ServerAdmin [email protected]'
  when: ansible_distribution == "Ubuntu"
  register: apache2_service

- name: restart apache (Ubuntu)
  tags: ubuntu,apache,apache2
  service:
    name: "{{ apache_service }}"
    state: restarted
  when: apache2_service.changed

- name: copy default (index) html file for site
  tags: apache,apache2,httpd
  copy:
    src: default_site.html
    dest: /var/www/html/index.html
    owner: root
    group: root
    mode: 0644

- name: install unzip
  package:
    name: unzip
    
- name: install terraform
  unarchive:
    src: https://releases.hashicorp.com/terraform/1.3.4/terraform_1.3.4_linux_amd64.zip
    dest: /usr/local/bin
    remote_src: yes
    owner: root
    group: root
    mode: 0755    

image

Run:

ansible-playbook site.yml

image