ansible-playbook之roles实现

ansible-playbook之roles实现

一、目标

两台主机:192.168.50.138(安装ansible)、192.168.50.139

[webservers]
192.168.50.138 httpd_point=80
192.168.50.139 httpd_point=8080

实现在两台主机上同时安装apache,提供配置文件,并启动。在139主机上提供mysql服务。

- hosts: all
  remote_user: root
  roles:
  - apache

- hosts: 192.168.50.139
  remote_user: root
  roles:
  - apache
  - mysql

二、roles之apache

1、创建目录

 mkdir -pv /ansible_playbooks/roles/{apache,mysql}/{tasks,files,templates,meta,handlers,vars}

2、复制一份httpd配置文件作为template

cp /etc/httpd/conf/httpd.conf /ansible_playbooks/roles/apache/templates/

编辑此配置文件做变量引用

Listen {{ httpd_point }}

3、编写tasks任务

[root@node1 apache]# vim tasks/main.yml 

- name: install httpd
  yum: name=httpd state=latest
- name: install config
  template: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
  notify:
  - restart apache
- name: start apache
  service: name=httpd state=started

4、编写handlers

[root@node1 apache]# vim handlers/main.yml 

- name: restart apache
  service: name=httpd state=restarted

三、roles之mysql

1、复制一份mysql配置文件到files文件中

cp /etc/my.cnf /ansible_playbooks/roles/mysql/files/
2、编写tasks任务
[root@node1 mysql]# vim /ansible_playbooks/roles/mysql/tasks/main.yml 

- name: install mysql
  yum: name=mariadb state=latest
- name: install config-mysql
  copy: src=my.cnf dest=/etc/my.cnf
- name: start mariadb
  service: name=mariadb state=started

四、编写剧本

[root@node1 mysql]# vim /ansible_playbooks/site.yml 

- hosts: all
  remote_user: root
  roles:
  - apache

- hosts: 192.168.50.139
  remote_user: root
  roles:
  - apache
  - mysql

五、执行剧本

[root@node1 handlers]# ansible-playbook /ansible_playbooks/site.yml 

PLAY [all] *******************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
ok: [192.168.50.139]
ok: [192.168.50.138]

TASK [apache : install httpd] ************************************************************************************************************************************************************************************************************************************************
ok: [192.168.50.138]
ok: [192.168.50.139]

TASK [apache : install config] ***********************************************************************************************************************************************************************************************************************************************
ok: [192.168.50.139]
ok: [192.168.50.138]

TASK [apache : start apache] *************************************************************************************************************************************************************************************************************************************************
ok: [192.168.50.139]
ok: [192.168.50.138]

PLAY [192.168.50.139] ********************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
ok: [192.168.50.139]

TASK [apache : install httpd] ************************************************************************************************************************************************************************************************************************************************
ok: [192.168.50.139]

TASK [apache : install config] ***********************************************************************************************************************************************************************************************************************************************
ok: [192.168.50.139]

TASK [apache : start apache] *************************************************************************************************************************************************************************************************************************************************
ok: [192.168.50.139]

TASK [mysql : install mysql] *************************************************************************************************************************************************************************************************************************************************
ok: [192.168.50.139]

TASK [mysql : install config-mysql] ******************************************************************************************************************************************************************************************************************************************
changed: [192.168.50.139]

TASK [mysql : start mariadb] *************************************************************************************************************************************************************************************************************************************************
changed: [192.168.50.139]

PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
192.168.50.138             : ok=4    changed=0    unreachable=0    failed=0   
192.168.50.139             : ok=11   changed=2    unreachable=0    failed=0   

成功!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值