вводное
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
--- - 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 …
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
--- - 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 |
С файлами получится вот так:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
└── 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
1 2 3 |
--- # defaults file for deploy_apache_web_site destin_folder: /var/www/html |
Редактируем deploy_apache_web_site\hendlers\main.yml
1 2 3 4 5 6 7 8 9 10 |
--- # 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
--- # 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 для установки ролей
1 2 3 4 5 6 7 8 9 10 11 12 |
--- - 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
1 2 3 4 5 6 7 8 9 10 11 12 |
--- - 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 - ну собственно запуск установки роли |