Skip to content

Instantly share code, notes, and snippets.

@icasimpan
Created October 29, 2017 13:54
Show Gist options
  • Save icasimpan/803955c7d43e847ce12ff9422c1cbbc4 to your computer and use it in GitHub Desktop.
Save icasimpan/803955c7d43e847ce12ff9422c1cbbc4 to your computer and use it in GitHub Desktop.
Sample ansible playbook to install nginx with sample page on CentOS7
## Credits to John Lieske - https://www.ansible.com/blog/getting-started-writing-your-first-playbook
---
- name: Install nginx
hosts: host.name.ip
become: true
tasks:
- name: Add epel-release repo
yum:
name: epel-release
state: present
- name: Install nginx
yum:
name: nginx
state: present
- name: Insert Index Page
template:
src: index.html
dest: /usr/share/nginx/html/index.html
- name: Start NGiNX
service:
name: nginx
state: started
@Gouravkumar0234
Copy link

Thanks for this tutorials, its really very helpful, now I want complete nginx playbook with mange the all sites. Like: site-available, site-enabled. i mentioned in below example file for Ubuntu, but i need it for CentOS-7.

Example:-

  • name: Set up nginx config directory
    file: path=/etc/nginx/{{ item }}
    state=directory
    with_items:
    - sites-available
    - sites-enabled

    • name: Generate the nginx configuration file
      copy: src=files/nginx.conf
      dest=/etc/nginx/nginx.conf

    • name: Deactivate the default nginx site
      file: path=/etc/nginx/sites-enabled/default
      state=absent

    • name: Generate the stub_status site config
      copy: src=files/nginx.status.conf
      dest=/etc/nginx/sites-available/status

    • name: Activate the stub_status site
      file: src=/etc/nginx/sites-available/status
      dest=/etc/nginx/sites-enabled/status
      state=link

    • name: Add site for the app under development
      when: app is defined
      template: src=templates/nginx.site.conf.j2
      dest=/etc/nginx/sites-available/site

    • name: Activate the app
      when: app is defined
      file: src=/etc/nginx/sites-available/site
      dest=/etc/nginx/sites-enabled/site
      state=link

    • name: Configure log rotation
      copy: src=files/nginx.logrotate
      dest=/etc/logrotate.d/nginx

    • name: Copy nginx init script
      sudo: yes
      copy: src=files/nginx.upstart
      dest=/etc/init/nginx.conf
      mode=755

- name: Add ufw rule

sudo: yes

command: iptables allow 80,443/tcp

# Note: ufw isn't enabled at this point

  • name: Start NGiNX
    service:
    name: nginx
    state: started

@saviour123
Copy link

Can we have the ubuntu version

@dev-ops-biznet
Copy link

thanks is works

@moshemizrachi
Copy link

Hi, what should i change in the playbook to modify HTTP listening port to 8080 ?

@hvdkooij
Copy link

hvdkooij commented Feb 15, 2021

Hi, what should i change in the playbook to modify HTTP listening port to 8080 ?

Nothing.

However you must change the template files the playbook is referring to.

@OlanCloudEngineer
Copy link

@Saviour
For Ubuntu. just change the yum to apt

@Puneethmandimutt
Copy link

what if we want to run on multiple inventory

@Majid-Mahzarnia
Copy link

Majid-Mahzarnia commented May 27, 2024

Thank you very much
to start nginx automatically after restarting the server I think we need the folowing:

- name: Start NGiNX service: name: nginx enabled: yes state: started

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment