вводное
cd ~ansible - зашли в домашний каталог ansible
mkdir roles - создали каталог roles
cd roles - зашли в каталог roles
ansible-galaxy init deploy_apache_web_site - создание роли, а точнее в каталоге roles, будут созданы каталоги и файлы
---------------------------------------
└── deploy_apache_web_site
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml
---------------------------------------
Наш playbook.yml который мы будем распихивать в роль deploy_apache_web_site
---
- name: Install Apache and Upload my Web Page
hosts: all
become: yes
vars:
source_folder: ./website
destin_folder: /var/www/html
tasks:
- name: Cheack and Print Linux Version
debug: var=ansible_os_family
- block: # === BLOCK REDHAT ====
- name: Install Apache Web Server for RedHat
yum: name=httpd state=present
- name: Start Apache and Enable it on every boot
service: name=httpd state=started enabled=yes
when: ansible_os_family == "RedHat"
- block: # === BLOCK DEBIAN ====
- name: Install Apache Web Server for Debian
apt: name=apache2 state=present
- name: Start Apache and Enable it on every boot
service: name=apache2 state=started enabled=yes
when: ansible_os_family == "Debian"
- name: Generate INDEX.HTML file
template: src={{ source_folder }}/index.j2 dest={{ destin_folder }}/index.html mode=555
notify:
- Restart Apache RedHat
- Restart Apache Debian
- name: Copy folder to web Servers
copy: src={{ source_folder }}/{{ item }} dest={{ destin_folder }} mode=0555
loop:
- "file0.txt"
- "file1.txt"
- "file2.txt"
notify:
- Restart Apache RedHat
- Restart Apache Debian
handlers:
- name: Restart Apache RedHat
service: name=httpd state=restarted
when: ansible_os_family == "RedHat"
- name: Restart Apache Debian
service: name=apache2 state=restarted
when: ansible_os_family == "Debian"
...
Планируем распил….. playbook.yml …
---
- name: Install Apache and Upload my Web Page
hosts: all
become: yes
vars:
source_folder: ./website --- это директива нам будет не нужна (так как есть спец каталог для файлов)
destin_folder: /var/www/html --- эту запись добавим в defaults\main.yml
tasks: --- все таски добавим в tasks\main.yml
- name: Cheack and Print Linux Version
debug: var=ansible_os_family
- block: # === BLOCK REDHAT ====
- name: Install Apache Web Server for RedHat
yum: name=httpd state=present
- name: Start Apache and Enable it on every boot
service: name=httpd state=started enabled=yes
when: ansible_os_family == "RedHat"
- block: # === BLOCK DEBIAN ====
- name: Install Apache Web Server for Debian
apt: name=apache2 state=present
- name: Start Apache and Enable it on every boot
service: name=apache2 state=started enabled=yes
when: ansible_os_family == "Debian"
- name: Generate INDEX.HTML file
template: src={{ source_folder }}/index.j2 dest={{ destin_folder }}/index.html mode=555 --- тут надо будет кое что поправить см ниже
notify:
- Restart Apache RedHat
- Restart Apache Debian
- name: Copy folder to web Servers
copy: src={{ source_folder }}/{{ item }} dest={{ destin_folder }} mode=0555 --- тут надо будет кое что поправить см ниже
loop:
- "file0.txt"
- "file1.txt"
- "file2.txt"
notify:
- Restart Apache RedHat
- Restart Apache Debian
handlers: - все хендлеры положим в файл handlers\main.yml
- name: Restart Apache RedHat
service: name=httpd state=restarted
when: ansible_os_family == "RedHat"
- name: Restart Apache Debian
service: name=apache2 state=restarted
when: ansible_os_family == "Debian"
...
Файл website/index.j2 копулируем в каталог templates
Остальные файлы из каталога website копируем в каталог deploy_apache_web_site/files
С файлами получится вот так:
└── deploy_apache_web_site
├── defaults
│ └── main.yml
├── files
│ ├── file0.txt
│ ├── file1.txt
│ ├── file2.txt
│ ├── file3.txt
│ ├── file4.txt
│ ├── file5.txt
│ └── index.css
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
│ └── index.j2
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml
Редактируем deploy_apache_web_site\defaults\main.yml
--- # defaults file for deploy_apache_web_site destin_folder: /var/www/html
Редактируем deploy_apache_web_site\hendlers\main.yml
--- # handlers file for deploy_apache_web_site - name: Restart Apache RedHat service: name=httpd state=restarted when: ansible_os_family == "RedHat" - name: Restart Apache Debian service: name=apache2 state=restarted when: ansible_os_family == "Debian"
Редактируем deploy_apache_web_site\tasks\main.yml
---
# tasks file for deploy_apache_web_site
- name: Cheack and Print Linux Version
debug: var=ansible_os_family
- block: # === BLOCK REDHAT ====
- name: Install Apache Web Server for RedHat
yum: name=httpd state=present
- name: Start Apache and Enable it on every boot
service: name=httpd state=started enabled=yes
when: ansible_os_family == "RedHat"
- block: # === BLOCK DEBIAN ====
- name: Install Apache Web Server for Debian
apt: name=apache2 state=present
- name: Start Apache and Enable it on every boot
service: name=apache2 state=started enabled=yes
when: ansible_os_family == "Debian"
- name: Generate INDEX.HTML file
template: src=index.j2 dest={{ destin_folder }}/index.html mode=555
notify:
- Restart Apache RedHat
- Restart Apache Debian
- name: Copy folder to web Servers
copy: src={{ item }} dest={{ destin_folder }} mode=0555
loop:
- "file0.txt"
- "file1.txt"
- "file2.txt"
notify:
- Restart Apache RedHat
- Restart Apache Debian
Создаем файл playbook.yml для установки ролей
---
- name: Install Apache and Upload my Web Page
hosts: all
become: yes
roles:
- deploy_apache_web_site
- deploy_db
- deploy_vpn
- deploy_xxx
...
Создаем файл playbook.yml для установки ролей с условием если ос linux
---
- name: Install Apache and Upload my Web Page
hosts: all
become: yes
roles:
- { role: deploy_apache_web_site, when: ansible_system == 'Linux' }
...
ansible-playbook playbook.yml - ну собственно запуск установки роли
