赞
踩
[root@m0 ~]# vim /etc/ansible/hosts s0 ansible_ssh_host=192.168.2.110 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=1 s1 ansible_ssh_host=192.168.2.111 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=1 s2 ansible_ssh_host=192.168.2.112 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=1 [s] s0 s1 s2
[root@m0 ~]# vim /etc/ansible/playbook/nginx.yml --- - hosts: s remote_user: root tasks: - name: 卸载httpd yum: name=httpd state=absent - name: 安装nginx yum: name=nginx state=present - name: 修改资源文件 shell: echo 'i am nginx,port is 80' > /usr/share/nginx/html/index.html - name: 修改端口 command: sed -i '/listen/s/80/8080/g' /etc/nginx/nginx.conf notify: - restart nginx - name: 启动服务 service: name=nginx state=started enabled=yes handlers: - name: restart nginx service: name=nginx state=restarted ... [root@m0 ~]# ansible-playbook /etc/ansible/playbook/nginx.yml
[root@m0 ~]# cd /etc/ansible/roles [root@m0 roles]# mkdir nginx [root@m0 roles]# ls nginx [root@m0 roles]# cd nginx/ [root@m0 nginx]# mkdir files [root@m0 nginx]# mkdir tasks [root@m0 nginx]# mkdir handlers/ [root@m0 nginx]# mkdir handlers [root@m0 nginx]# mkdir templates [root@m0 nginx]# mkdir vars [root@m0 nginx]# mkdir meta [root@m0 nginx]# tree . ├── files ├── handlers ├── meta ├── tasks ├── templates └── vars 6 directories, 0 files [root@m0 nginx]# touch files/main.yml [root@m0 nginx]# touch handlers/main.yml [root@m0 nginx]# touch tasks/main.yml [root@m0 nginx]# touch vars/main.yml [root@m0 nginx]# touch meta/main.yml [root@m0 nginx]# rm -rf files/main.yml [root@m0 nginx]# tree . ├── files ├── handlers │ └── main.yml ├── meta │ └── main.yml ├── tasks │ └── main.yml ├── templates └── vars └── main.yml 6 directories, 4 files [root@m0 nginx]# cd
[root@m0 ~]# vim /etc/ansible/playbook/test002.yml --- - hosts: s remote_user: root roles: - nginx ...
[root@m0 ~]# ansible-playbook /etc/ansible/playbook/test002.yml PLAY [s] *************************************************************************** TASK [Gathering Facts] ************************************************************* ok: [s1] ok: [s2] ok: [s0] PLAY RECAP ************************************************************************* s0 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 s1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 s2 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@m0 ~]# ansible s -m yum -a 'name=nginx state=absent'
[root@m0 ~]# vim /etc/ansible/roles/nginx/tasks/main.yml --- - name: 卸载httpd yum: name=httpd state=absent - name: 卸载nginx yum: name=nginx state=absent - name: 安装nginx yum: name=nginx state=present - name: 启动服务 service: name=nginx state=started enabled=yes ...
[root@m0 ~]# ansible-playbook /etc/ansible/playbook/test002.yml PLAY [s] *************************************************************************** TASK [Gathering Facts] ************************************************************* ok: [s2] ok: [s1] ok: [s0] TASK [nginx : 卸载httpd] ************************************************************* ok: [s1] ok: [s2] ok: [s0] TASK [卸载nginx] ********************************************************************* ok: [s0] ok: [s1] ok: [s2] TASK [安装nginx] ********************************************************************* changed: [s1] changed: [s2] changed: [s0] TASK [nginx : 启动服务] **************************************************************** changed: [s0] changed: [s1] changed: [s2] PLAY RECAP ************************************************************************* s0 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 s1 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 s2 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
1.nginx修改端口为8080
2.mariadb中创建eleme数据库
3.创建user表
字段 | 类型 | 约束 | 字段名称 |
---|---|---|---|
id | int | 主键约束、自增 | 编号 |
username | varchar | 非空主键 | 账号 |
password | varchar | 非空主键 | 密码 |
remark | varchar | 没有主键 | 账号权限说明 |
[root@m0 ~]# vim /etc/ansible/playbook/test003.yml --- - hosts: s remote_user: root roles: - mariadb ...
[root@m0 ~]# cd /etc/ansible/roles/ [root@m0 roles]# ls nginx [root@m0 roles]# cp -r nginx mariadb [root@m0 roles]# ls mariadb nginx [root@m0 roles]# cd mariadb/ [root@m0 mariadb]# ls files handlers meta tasks templates vars
[root@m0 mariadb]# cd .. [root@m0 roles]# mkdir script [root@m0 roles]# cd script/
[root@m0 script]# vim dbtable.sh #!/bin/bash mysql << EOF create database if not exists eleme charset utf8mb4; use eleme; create table user(id int primary key auto_increment,username varchar(45) not null, password varchar(45) not null,remark varchar(45)); quit EOF
[root@m0 mariadb]# vim /etc/ansible/roles/mariadb/tasks/main.yml --- - name: 卸载mariadb yum: name=mariadb,mariadb-server,mariadb-devel state=absent - name: 安装mariadb yum: name=mariadb,mariadb-server,mariadb-devel state=present - name: 启动mariadb服务 service: name=mariadb state=started enabled=yes - name: 执行脚本,创建数据库和数据表 script: /etc/ansible/roles/script/dbtable.sh ...
[root@m0 script]# cd [root@m0 ~]# ansible-playbook /etc/ansible/playbook/test003.yml PLAY [s] *************************************************************************** TASK [Gathering Facts] ************************************************************* ok: [s1] ok: [s0] ok: [s2] TASK [卸载mariadb] ******************************************************************* changed: [s2] changed: [s0] changed: [s1] TASK [安装mariadb] ******************************************************************* changed: [s0] changed: [s2] changed: [s1] TASK [启动mariadb服务] ***************************************************************** changed: [s0] changed: [s2] changed: [s1] TASK [mariadb : 执行脚本,创建数据库和数据表] **************************************************** changed: [s0] changed: [s2] changed: [s1] PLAY RECAP ************************************************************************* s0 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 s1 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 s2 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
# s0主机 [root@s0 ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 3 Server version: 5.5.68-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | eleme | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) MariaDB [(none)]> use eleme; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [eleme]> show tables; +-----------------+ | Tables_in_eleme | +-----------------+ | user | +-----------------+ 1 row in set (0.00 sec) MariaDB [eleme]> select * from user; Empty set (0.00 sec) # s1主机 [root@s1 ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 3 Server version: 5.5.68-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | eleme | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) MariaDB [(none)]> use eleme; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [eleme]> show tables; +-----------------+ | Tables_in_eleme | +-----------------+ | user | +-----------------+ 1 row in set (0.00 sec)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。