role类似于salt-stack里面的state,state有一定的组织架构。而role则是ansible中playbook的目录组织架构,如果把所有内容都写到playbooks里,可能会导致playbo
role类似于salt-stack里面的state,state有一定的组织架构。
而role则是ansible中playbook的目录组织架构,如果把所有内容都写到playbooks里,可能会导致playbooks臃肿,难读。而模块化之后,有效解决了上述的问题。
目录结构示例:
[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
第一级目录下有俩文件夹,俩文件
group_vars这里面存的组变量,定义规则等同于/etc/ansible/group_vars里面的组变量
group_vars下的salt文件里的变量只对salt组有效,如果文件名为all,则对所有主机组有效,而相对于roles这里面的变量则是全局的。
web]# cat group_vars/salt
http_port: 80
hosts存放主机及组信息:
web]# cat hosts
[salt]
192.168.137.130
roles下有两个role,分别为mysql,webserver
mysql和webserver目录下可以有下面这些目录:
files:存文件的,文件放此目录,ansible默认就会到这个目录去找文件,对应task里面的copy模块
tasks:显然是存放tasks的
handlers:存放handlers
templates:存放模板,对应task里面的模块template
vars:这里面定义的变量,只对当前role有作用
meta:定义role和role直接的依赖关系。
查看webserver目录下文件内容:
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
5
6 - name: keep httpd running
7 service: name=httpd state=started
8 tags: install
9
10 - name: transfer index file
11 copy: src=/index.html dest=/var/www/index.html ##到file中查找
12 tags: install
13
14 - name: wait for httpd to start
15 wait_for: port=`http_port` ##http_port为group_vars/salt中全局变量
16 tags: install
17
18 - name:transfer httpd configure file
19 template: src=httpd.conf dest=/etc/httpd/httpd.conf ##httpd.conf模板中查找
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定义依赖关系,webserver运行前,必须先运行mysql这个role,并传递变量echo_vars给mysql
查看mysql目录下文件内容:
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
7
8 - name: keep mysql is running
9 service: name=mysql state=started
10 tags: install
11
12 - name: echo_vars
13 shell: echo '`echo_vars`' ##webserver的meta传过来的变量
14 reGISter: result
15 - debug: msg='`result`.`stdout`'
16 tags: install
17
18 - include: configure.yml ##include进来
handlers
handlers/main.yml
1 ---
2 - name: restart mysql
3 service: name=mysql state=restarted
vars
vars/main.yml
1 ---
2 software: ##role内变量
3 - mysql-server
4 - lrzsz
site.yml,我们要调用的文件。
web]# cat site.yml
---
- hosts: salt
remote_user: '`uservar`'
roles:
- webserver
执行结果:
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', u'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" ##webserver传给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
如何在roles里调用tags标签:
在命令行中可用参数调用tags,
--tags="test1,test3" :只执行test1,test3标签段
--skip-tags="test2" :跳过test2,执行其它tags为非test2标签的字段
web]# cat site.yml
---
- hosts: salt
remote_user: '`uservar`'
roles:
- {role: webserver,tags: ['install']} ##只执行角色中定义的标签段
--结束END--
本文标题: ansible--roles
本文链接: https://lsjlt.com/news/34752.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0