Публикация

Курс Ansible - урок 14 - Роли

Курс Ansible - урок 14 - Роли

Курс Ansible - урок 14 - Роли

Ansible — это невероятная утилита для управления конфигурацией и подготовки к работе, которая позволяет автоматизировать все процессы. В этой серии вы узнаете всё, что нужно знать, чтобы использовать Ansible для повседневных административных задач.

site.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
 ---
 
 - hosts: all
   become: true
   pre_tasks:
 
   - name: update repository index (CentOS)
     tags: always
     dnf:
       update_cache: yes
     changed_when: false
     when: ansible_distribution == "CentOS"
 
   - name: update repository index (Ubuntu)
     tags: always
     apt:
       update_cache: yes
     changed_when: false
     when: ansible_distribution == "Ubuntu"
 
 - hosts: all
   become: true
   roles:
     - base
    
 - hosts: workstations
   become: true
   roles:
     - workstations
 
 - hosts: web_servers
   become: true
   roles:
     - web_servers
 
 - hosts: db_servers
   become: true
   roles:
     - db_servers
 
 - hosts: file_servers
   become: true
   roles:
     - file_servers

Создаем каталог ролей

1
 mkdir roles

Создаем каталог для каждой роли, которую хотим добавить:

1
2
3
4
5
6
 cd roles
 mkdir base
 mkdir db_servers
 mkdir file_servers
 mkdir web_servers
 mkdir workstations

Внутри каталога каждой роли создаем каталог задач

1
2
 cd <role_name>
 mkdir tasks

main.yml (основная роль)

Используйте свой ключ в последней строчке, взамен указанного здесь

1
2
3
4
 - name: add ssh key for simone
   authorized_key:
     user: simone
     key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAe7/ofWLNBq3+fRn3UmgAizdicLs9vcS4Oj8VSOD1S/ ansible"

Создадим необходимые файлы/каталоги для роли db_servers

1
2
3
4
5
6
7
 cd ..
 cd ..
 mkdir db_servers
 cd db_servers
 mkdir tasks
 cd tasks
 vim main.yml

main.yml (роль db_servers)

1
2
3
4
5
6
7
8
9
10
11
12
13
 - name: install mariadb server package (CentOS)
   tags: centos,db,mariadb
   dnf:
     name: mariadb
     state: latest
   when: ansible_distribution == "CentOS"
 
 - name: install mariadb server
   tags: db,mariadb,ubuntu
   apt:
     name: mariadb-server
     state: latest
   when: ansible_distribution == "Ubuntu"

main.yml (роль file_servers)

1
2
3
4
5
 - name: install samba package
   tags: samba
   package:
     name: samba
     state: latest

main.yml (роль workstations)

1
2
3
4
5
6
7
8
9
10
11
12
 - name: install unzip
   package:
     name: unzip
 
 - name: install terraform
   unarchive:
     src: https://releases.hashicorp.com/terraform/0.12.28/terraform_0.12.28_linux_amd64.zip
     dest: /usr/local/bin
     remote_src: yes
     mode: 0755
     owner: root
     group: root

main.yml (роль web_servers)

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
 - name: install httpd package (CentOS)
   tags: apache,centos,httpd
   dnf:
     name:
       - httpd
       - php
     state: latest
   when: ansible_distribution == "CentOS"
 
 - name: start and enable httpd (CentOS)
   tags: apache,centos,httpd
   service:
     name: httpd
     state: started
     enabled: yes
   when: ansible_distribution == "CentOS"
 
 - name: install apache2 package (Ubuntu)
   tags: apache,apache2,ubuntu
   apt:
     name:
       - apache2
       - libapache2-mod-php
     state: latest
   when: ansible_distribution == "Ubuntu"
 
 - name: change e-mail address for admin
   tags: apache,centos,httpd
   lineinfile:
     path: /etc/httpd/conf/httpd.conf
     regexp: '^ServerAdmin'
     line: ServerAdmin somebody@somewhere.net
   when: ansible_distribution == "CentOS"
   register: httpd
 
 - name: restart httpd (CentOS)
   tags: apache,centos,httpd
   service:
     name: httpd
     state: restarted
   when: httpd.changed    
 
 - name: copy html file for site
   tags: apache,apache,apache2,httpd
   copy:
     src: default_site.html
     dest: /var/www/html/index.html
     owner: root
     group: root
     mode: 0644

Запускаем новый плейбук

1
 ansible-playbook site.yml

Оригинал

Публикация защищена лицензией CC BY 4.0 .