您好,登錄后才能下訂單哦!
role類似于salt-stack里面的state,state有一定的組織架構(gòu)。
而role則是ansible中playbook的目錄組織架構(gòu),如果把所有內(nèi)容都寫到playbooks里,可能會(huì)導(dǎo)致playbooks臃腫,難讀。而模塊化之后,有效解決了上述的問題。
目錄結(jié)構(gòu)示例:
[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
第一級(jí)目錄下有倆文件夾,倆文件
group_vars這里面存的組變量,定義規(guī)則等同于/etc/ansible/group_vars里面的組變量
group_vars下的salt文件里的變量只對(duì)salt組有效,如果文件名為all,則對(duì)所有主機(jī)組有效,而相對(duì)于roles這里面的變量則是全局的。
web]# cat group_vars/salt
http_port: 80
hosts存放主機(jī)及組信息:
web]# cat hosts
[salt]
192.168.137.130
roles下有兩個(gè)role,分別為mysql,webserver
mysql和webserver目錄下可以有下面這些目錄:
files:存文件的,文件放此目錄,ansible默認(rèn)就會(huì)到這個(gè)目錄去找文件,對(duì)應(yīng)task里面的copy模塊
tasks:顯然是存放tasks的
handlers:存放handlers
templates:存放模板,對(duì)應(yīng)task里面的模塊template
vars:這里面定義的變量,只對(duì)當(dāng)前role有作用
meta:定義role和role直接的依賴關(guān)系。
查看webserver目錄下文件內(nèi)容:
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定義依賴關(guān)系,webserver運(yùn)行前,必須先運(yùn)行mysql這個(gè)role,并傳遞變量echo_vars給mysql
查看mysql目錄下文件內(nèi)容:
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進(jìn)來
handlers
handlers/main.yml
1 ---
2 - name: restart mysql
3 service: name=mysql state=restarted
vars
vars/main.yml
1 ---
2 software: ##role內(nèi)變量
3 - mysql-server
4 - lrzsz
site.yml,我們要調(diào)用的文件。
web]# cat site.yml
---
- hosts: salt
remote_user: '`uservar`'
roles:
- webserver
執(zhí)行結(jié)果:
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里調(diào)用tags標(biāo)簽:
在命令行中可用參數(shù)調(diào)用tags,
--tags="test1,test3" :只執(zhí)行test1,test3標(biāo)簽段
--skip-tags="test2" :跳過test2,執(zhí)行其它tags為非test2標(biāo)簽的字段
web]# cat site.yml
---
- hosts: salt
remote_user: '`uservar`'
roles:
- {role: webserver,tags: ['install']} ##只執(zhí)行角色中定義的標(biāo)簽段
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。