ansible--roles
role類似於salt-stack裏面的state,state有一定的組織架構。
而role則是ansible中playbook的目錄組織架構,如果把所有內容都寫到playbooks裏,可能會導致playbooks臃腫,難讀。而模塊化之後,有效解決了上述的問題。
目錄結構示例:
[[email protected] 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‘]} ##只執行角色中定義的標簽段
本文出自 “Gavin” 博客,請務必保留此出處http://guopeng7216.blog.51cto.com/9377374/1945305
ansible--roles