Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Ansible--roles

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Role is similar to state,state in salt-stack, which has a certain organizational structure.

Role is the directory organization structure of playbook in ansible. If you write everything into playbooks, it may make playbooks bloated and difficult to read. After modularization, the above problems are solved effectively.

Example of directory structure:

[root@web02 web] # tree

.

├── group_vars

│ └── salt

├── hosts

├── roles

│ ├── mysql

│ │ ├── handlers

│ └── main.yml

│ │ ├── tasks

│ ├── configure.yml

│ └── main.yml

│ │ ├── templates

│ └── my.cnf

│ │ └── vars

│ │ └── main.yml

│ └── webserver

│ ├── files

│ │ └── index.html

│ ├── handlers

│ │ └── main.yml

│ ├── meta

│ │ └── main.yml

│ ├── tasks

│ │ └── main.yml

│ └── templates

│ └── httpd.conf

└── site.yml

There are two folders and two files under the first level directory.

The group variables stored in group_vars. The definition rules are equivalent to the group variables in / etc/ansible/group_vars.

The variables in the salt file under group_vars are only valid for the salt group, and if the file name is all, it is valid for all host groups, while the variables in the file are global relative to roles.

Web] # cat group_vars/salt

Http_port: 80

Hosts stores host and group information:

Web] # cat hosts

[salt]

192.168.137.130

There are two role under roles, one is mysql,webserver

You can have the following directories under the mysql and webserver directories:

Files: if you save files, put them in this directory. By default, ansible will go to this directory to find files, corresponding to the copy module in task.

Tasks: obviously it stores tasks.

Handlers: store handlers

Templates: stores the template, corresponding to the module template in the task

Vars: the variables defined here only work on the current role

Meta: defines the direct dependency between role and role.

View the contents of the file in the webserver directory:

Webserver] # for dir in {'tasks','handlers','meta'}; do echo-e "\ 033 [31m ${dir}\ 033 [0m"; for file in `ls ${dir} / * `; do echo-e "\ 033 [32m ${file}\ 033 [0m"; cat-n ${file}; done;done

Tasks

Tasks/main.yml

1--

2-name: installed httpd

3 yum: name=httpd state=latest

4 tags: install

five

6-name: keep httpd running

7 service: name=httpd state=started

8 tags: install

nine

10-name: transfer index file

11 copy: src=/index.html dest=/var/www/index.html # # search in file

12 tags: install

thirteen

14-name: wait for httpd to start

15 wait_for: port= `http _ port` # # http_port is a global variable in group_vars/salt

16 tags: install

seventeen

18-name:transfer httpd configure file

19 template: find in the src=httpd.conf dest=/etc/httpd/httpd.conf # # httpd.conf template

20 tags: conf

21 notify:

22-restart httpd

Handlers

Handlers/main.yml

1--

2-name: restart httpd

3 service: name=httpd state=restarted

Meta

Meta/main.yml

1--

2 dependencies:

3-{role: mysql,echo_vars: hello mysql}

# # meta defines dependencies. Before webserver runs, you must run the role of mysql and pass the variable echo_vars to mysql

View the contents of the file in the mysql directory:

Mysql] # for dir in {'tasks','handlers','vars'}; do echo-e "\ 033 [31m ${dir}\ 033 [0m"; for file in `ls ${dir} / * `; do echo-e "\ 033 [32m ${file}\ 033 [0m"; cat-n ${file}; done;done

Tasks

Tasks/configure.yml

1--

2-name: transfer mysql configure file

3 template: src=my.cnf dest=/etc/mysql/my.cnf

4 notify:

5-restart mysql

6 tags: configure

Tasks/main.yml

1--

2-name: install mysql-server

3 yum: name= `item` state=latest

4 with_items:

5-`software``

6 tags: install

seven

8-name: keep mysql is running

9 service: name=mysql state=started

10 tags: install

eleven

12-name: echo_vars

13 shell: echo '`echo_ vars`' # # variable passed by meta of webserver

14 register: result

15-debug: msg=' `result`.`stdout``

16 tags: install

seventeen

18-include: configure.yml # # include come in

Handlers

Handlers/main.yml

1--

2-name: restart mysql

3 service: name=mysql state=restarted

Vars

Vars/main.yml

1--

2 software: # # variables in role

3-mysql-server

4-lrzsz

Site.yml, the file we are going to call.

Web] # cat site.yml

-

-hosts: salt

Remote_user: '`uservar`'

Roles:

-webserver

Execution result:

Web] # ansible-playbook-I hosts site.yml-e 'uservar=root'

PLAY [salt] *

TASK [Gathering Facts] *

Ok: [192.168.137.146]

TASK [mysql: install mysql-server] *

Ok: [192.168.137.146] = > (item= [u 'mysql-server', uplift lrzsz'])

TASK [mysql: keep mysql is running] *

Ok: [192.168.137.146]

TASK [mysql: echo_vars] *

Changed: [192.168.137.146]

TASK [mysql: debug] *

Ok: [192.168.137.146] = > {

"msg": "hello mysql" # # variables passed from webserver to mysql

}

TASK [mysql: transfer mysql configure file] * *

Ok: [192.168.137.146]

TASK [webserver: installed httpd] *

Ok: [192.168.137.146]

TASK [webserver: keep httpd running] *

Ok: [192.168.137.146]

TASK [webserver: transfer index file] *

Changed: [192.168.137.146]

TASK [webserver: wait for httpd to start] * *

Ok: [192.168.137.146]

TASK [webserver: transfer httpd configure file] * *

Changed: [192.168.137.146]

RUNNING HANDLER [webserver: restart httpd] * *

Changed: [192.168.137.146]

PLAY RECAP *

192.168.137.146: ok=12 changed=4 unreachable=0 failed=0

How to call the tags tag in roles:

Tags can be called with parameters on the command line

-- tags= "test1,test3": only test1,test3 tag segments are executed

-- skip-tags= "test2": skip test2 and execute other fields whose tags is not test2 tags

Web] # cat site.yml

-

-hosts: salt

Remote_user: '`uservar`'

Roles:

-{role: webserver,tags: ['install']} # # only execute the tag segments defined in the role

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report