一看必会系列:ansible 部署lamp

来源:本站原创 Linux 超过167 views围观 0条评论

 

————目录规划
[root@ansible_master roles]# tree
.
├── apache
│   ├── tasks
│   │   └── main.yaml
│   └── templates
│       └── index.php

├── lamp.yaml
├── mysql
│   └── tasks
│       ├── main.yaml
│       └── main.yaml~

————mysql/main.yaml

[root@ansible_master roles]# cat mysql/tasks/main.yaml
– name: install MySQL-python
  yum: name=MySQL-python state=installed
– name: install mariadb-server
  yum: name={{ item }} state=installed
  with_items:
        – mariadb-server
  notify:
        – restart mysql
– name: Start mariadb
  service: name=mariadb state=restarted enabled=yes
#增加用户方式1 
– name: user
  mysql_user: name=xx password=12345 priv=*.*:ALL state=present  
– name: Create db
  mysql_db:
    login_host: "127.0.0.1"
    login_user: "root"
    login_password: ""
    login_port: "3306"
    name: "cisco_db"
    encoding: "utf8"
    state: "present"
#增加用户方式2
– name: create a user
  mysql_user:
      login_host: "127.0.0.1"
      login_user: "root"
      login_password: ""
      login_port: "3306"
      name: "cisco"
      password: "1234"
      host: "192.168.142.%"
      priv: "*.*:all"
      state: "present"
#collation,config_file,connect_timeout,encoding,login_host,login_password,login_port,login_unix_socket,login_user,name,quick,single_transaction,ssl_ca,ssl_cert,ssl_key,state,target

———-apache/main.yaml
[root@ansible_master roles]# cat apache/tasks/main.yaml
– name: remove old version php
  yum: name=php* state=absent
– name: remove old version mysql
  yum: name=MySQL-server-5.6.35-1.el7.x86_64 state=absent
– name: install apache and php
  yum: name={{ item }} state=present
  with_items:
        – httpd
        – php
        – php-mysql
– name: copy index.php.j2
  template: src=index.php dest=/var/www/html/index.php
– name: http service state
  service: name=httpd state=restarted
– name: http service enable
  shell: systemctl enable httpd
[root@ansible_master roles]#

———lamp.yaml
[root@ansible_master roles]# cat lamp.yaml
– name: hosts
  hosts: all
  remote_user: root
– name: install omysql
  hosts: mysql
  roles:
    – mysql
– name: install apache and php
  hosts: apache
  roles:
    – apache

———-验证–
[root@ansible_master roles]# ansible apache -m shell -a "curl localhost/index.php |grep ansible"
[WARNING]: Consider using get_url or uri module rather than running curl

192.168.142.103 | SUCCESS | rc=0 >>
<tr><td class="e">System </td><td class="v">Linux ansible_03 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 </td></tr>  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 47382    0 47382    0     0  3157k      0 –:–:– –:–:– –:–:– 3305k

192.168.142.102 | SUCCESS | rc=0 >>
<tr><td class="e">System </td><td class="v">Linux ansible_02 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 </td></tr>  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 47382    0 47382    0     0  3667k      0 –:–:– –:–:– –:–:– 3855k

[root@ansible_master roles]#

 

———执行结果—
ansible-playbook lamp.yaml

PLAY [hosts] ********************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************
ok: [192.168.142.104]
ok: [192.168.142.102]
ok: [192.168.142.103]

PLAY [install omysql] ***********************************************************************************************************

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

TASK [mysql : install MySQL-python] *********************************************************************************************
ok: [192.168.142.104]

TASK [mysql : install mariadb-server] *******************************************************************************************
ok: [192.168.142.104] => (item=[u’mariadb-server’])

TASK [mysql : Start mariadb] ****************************************************************************************************
changed: [192.168.142.104]

TASK [mysql : user] *************************************************************************************************************
ok: [192.168.142.104]

TASK [mysql : Create db] ********************************************************************************************************
ok: [192.168.142.104]

TASK [mysql : create a user] ****************************************************************************************************
ok: [192.168.142.104]

PLAY [install apache and php] ***************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************
ok: [192.168.142.102]
ok: [192.168.142.103]

TASK [apache : remove old version php] ******************************************************************************************
changed: [192.168.142.103]
changed: [192.168.142.102]

TASK [apache : remove old version mysql] ****************************************************************************************
ok: [192.168.142.103]
ok: [192.168.142.102]

TASK [apache : install apache and php] ******************************************************************************************
changed: [192.168.142.103] => (item=[u’httpd’, u’php’, u’php-mysql’])
changed: [192.168.142.102] => (item=[u’httpd’, u’php’, u’php-mysql’])

TASK [apache : copy index.php.j2] ***********************************************************************************************
changed: [192.168.142.103]
changed: [192.168.142.102]

TASK [apache : http service state] **********************************************************************************************
changed: [192.168.142.103]
changed: [192.168.142.102]

TASK [apache : http service enable] *********************************************************************************************
changed: [192.168.142.103]
changed: [192.168.142.102]

PLAY RECAP **********************************************************************************************************************
192.168.142.102            : ok=8    changed=5    unreachable=0    failed=0  
192.168.142.103            : ok=8    changed=5    unreachable=0    failed=0  
192.168.142.104            : ok=8    changed=1    unreachable=0    failed=0 

———-之前安装mysql报错

TASK [mysql : user] ***************************************************************************************************************
task path: /etc/ansible/roles/mysql/tasks/main.yaml:11
fatal: [192.168.142.104]: FAILED! => {"changed": false, "msg": "The MySQL-python module is required."}
    to retry, use: –limit @/etc/ansible/roles/lamp.retry

解决方法:mysqlserver 需要安装MySQL-python

文章出自:CCIE那点事 http://www.jdccie.com/ 版权所有。本站文章除注明出处外,皆为作者原创文章,可自由引用,但请注明来源。 禁止全文转载。
本文链接:http://www.jdccie.com/?p=3708转载请注明转自CCIE那点事
如果喜欢:点此订阅本站
  • 相关文章
  • 为您推荐
  • 各种观点