ansible–roles


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/salthttp_port: 80
hosts存放主机及组信息:web]# cat hosts[salt]192.168.137.130
roles下有两个role,分别为mysql,webservermysql和webserver目录下可以有下面这些目录:files:存文件的,文件放此目录,ansible默认就会到这个目录去找文件,对应task里面的copy模块tasks:显然是存放tasks的handlers:存放handlerstemplates:存放模板,对应task里面的模块templatevars:这里面定义的变量,只对当前role有作用meta:定义role和role直接的依赖关系。
查看webserver目录下文件内容:webserver]# for dir in {‘tasks’,’handlers’,’meta’};do echo -e “33[31m${dir}33[0m”;for file in `ls ${dir}/*`;do echo -e “33[32m${file}33[0m”;cat -n ${file};done;donetaskstasks/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 httpdhandlershandlers/main.yml 1 — 2 – name: restart httpd 3 service: name=httpd state=restartedmetameta/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 “33[31m${dir}33[0m”;for file in `ls ${dir}/*`;do echo -e “33[32m${file}33[0m”;cat -n ${file};done;donetaskstasks/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: configuretasks/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进来handlershandlers/main.yml 1 — 2 – name: restart mysql 3 service: name=mysql state=restartedvarsvars/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’]} ##只执行角色中定义的标签段

相关推荐: Centos7.6下MySQL和Redis的详细安装教程

下面一起来了解下Centos7.6开发云主机域名下MySQL和Redis的详细安装教程,相信大家看完肯定会受益匪浅,文字在精不在多,希望Centos7.6下MySQL和Redis的详细安装教程这篇短内容是你想要的。 1、下载mysql,地址:http://de…

免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。

(0)
打赏 微信扫一扫 微信扫一扫
上一篇 06/08 09:18
下一篇 06/08 09:19

相关推荐