1:yum模块---安装数据库
1:安装数据库
[root@srever4 ~]# ansible server5 -m yum -a "name=mariadb-server state=present"
2:开启数据库
[root@srever4 ~]# ansible server5 -m service -a "name=mariadb state=started"
3:给数据库添加用户管理数据库
[root@srever4 ~]# ansible server5 -m mysql_user -a "name=yz password=westos priv=test.*:ALL state=present" ###用户名为yz,密码为westos,对test库拥有所有权限
server5 | FAILED! => {
"changed": false,
"msg": "The PyMySQL (Python 2.7 and Python 3.X) or MySQL-python (Python 2.X) module is required."
} ##发现需要安装MySQL-python
[root@srever4 ~]# ansible server5 -m yum -a "name=MySQL-python state=present"
再次执行:
[root@srever4 ~]# ansible server5 -m mysql_user -a "name=yz password=westos priv=test.*:ALL state=present" ##成功
server5 | CHANGED => {
"changed": true,
"user": "yz"
server5主机上进行登陆:
[root@server5 tmp]# mysql -uyz -pwestos test
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [test]>
登陆mysql库,查看用户
[root@server5 tmp]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.ansible的yum模块state选项的意思
Your MariaDB connection id is 4
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use mysql;
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 [mysql]> select * from user;
有我们创建的yz用户,密码为加密的形式
| localhost | yz | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96
二:Playbook
Playbook 简介:
Playbook主要有以下四部分构成:
target section:定义将要执行playbook的远程主机组
variable section:定义playbook运行时需要使用的变量
task section:定义将要在远程主机上执行的任务列表
handler section:定义task执行完成以后需要调用的任务
而Playbook对应的目录层有五个,分别如下:
一般所需的目录层有:(视情况可变化)
vars 变量层
tasks 任务层
handlers 触发条件
files 文件
template 模板
playbook中的每一个play的目的都是为了让某个或某些主机以某个指定的用户身份执行任务。
hosts:用于指定要执行指定任务的主机其可以是一个或多个,由逗号为分隔符分隔主机组
remote_user:用于指定远程主机上的执行任务的用户。不过remote_user也可用于各tasks中。也可以通过指定其通过sudo的方式在远程主机上执行任务其可用于play全局或某任务。此外甚至可以在sudo时使用sudo_user指定sudo时切换的用户
user:与remote_user相同
sudo:如果设置为yes,执行该任务组的用户在执行任务的时候,获取root权限
sudo_user:如果设置user为bob,sudo为yes,sudo_user为tom时,则bob用户在执行任务时会获得tom用户的权限
connection:通过什么方式连接到远程主机,默认为ssh
gather_facts:除非明确说明不需要在远程主机上执行setup模块,否则默认自动执行。如果确实不需要setup模块传递过来的变量,则可以将该选项设置为False
说明:
参数remote_user以前写做user,在Ansible 1.4以后才改为remote_user。主要为了不跟user模块混淆(user模块用于在远程系统上创建用户)
如果我们需要在使用sudo时指定密码,可在运行ansible-playbook命令时加上选项 --ask-sudo-pass (-K)。如果使用sudo时,playbook疑似被挂起,可能是在sudo prompt处被卡住,这时可执行 Control-C(正文结束字符)杀死卡住的任务,再重新运行一次。
当使用sudo_user切换到非root用户时,模块的参数会暂时写入/tmp目录下的一个随机临时文件。当命令执行结束后。临时文件立即删除。这种情况发生在普通用户的切换时,比如从“bob”切换到“tom”,切换到root账户时,不会发生,如从“bob”切换到 “root”,直接以普通用户或root身份登录也不会发生。如果不希望这些数据在短暂的时间内可以被读取(不可写),请避免在sudo_user中传递未加密的密码。其它情况下,“/tmp” 目录不被使用,这种情况不会发生。Ansible 也有意识的在日志中不记录密码参数
Playbooks与Ad-Hoc相比,是一种完全不同的运用Ansible的方式,而且是非常之强大的;也是系统ansible命令的集合,其利用yaml语言编写,运行过程,ansbile-playbook命令根据自上而下的顺序依次执行。
简单来说,Playbooks 是一种简单的配置管理系统与多机器部署系统的基础。与现有的其他系统有不同之处,且非常适合于复杂应用的部署。
同时,Playbooks开创了很多特性,它可以允许你传输某个命令的状态到后面的指令,如你可以从一台机器的文件中抓取内容并附为变量,然后在另一台机器中使用,这使得你可以实现一些复杂的部署机制,这是ansible命令无法实现的。
Playbooks可用于声明配置,更强大的地方在于,在Playbooks中可以编排有序的执行过程,甚至于做到在多组机器间,来回有序的执行特别指定的步骤。并且可以同步或异步的发起任务。
我们使用Ad-Hoc时,主要是使用 /usr/bin/ansible 程序执行任务.而使用Playbooks时,更多是将之放入源码控制之中,用之推送你的配置或是用于确认你的远程系统的配置是否符合配置规范。
三:使用playbooks自动化部署httpd服务
1:创建需要的目录
[root@srever4 ansible]# pwd
/etc/ansible
[root@srever4 ansible]# ls
ansible.cfg hosts roles
[root@srever4 ansible]# mkdir playbooks
[root@srever4 ansible]# cd playbooks/
[root@srever4 playbooks]# mkdir httpd
[root@srever4 playbooks]# cd httpd/
[root@srever4 httpd]# ls
2:编写 ./yml文件
---
- hosts: server5
remote_user: root
tasks:
- name: install httpd ##安装httpd服务
yum: name=httpd state=present
- name: config httpd
copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart httpd
##此处为什么要在ansible目录下有httpd.conf文件呢?
这样方便我们对配置文件进行修改,就只需要在主机上进行修改就可以,下面有新的触发restart httpd
- name: start httpd
service: name=httpd state=started
handlers: 定义task执行完成以后需要调用的任务
- name: restart httpd
service: name=httpd state=restarted
3:获取httpd.conf配置文件到/etc/ansible目录下
[root@server5 conf]# scp httpd.conf server4:/etc/ansible/playbooks/httpd
The authenticity of host 'server4 (172.25.60.4)' can't be established.
ECDSA key fingerprint is 0a:e9:f9:09:98:14:7c:73:5c:7c:f2:1b:cf:f5:d7:8b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'server4,172.25.60.4' (ECDSA) to the list of known hosts.
root@server4's password:
httpd.conf 100% 11KB 11.5KB/s 00:00
4:检测.yml文件是否有语法错误
[root@srever4 httpd]# ansible-playbook httpd.yml --syntax-check
playbook: httpd.yml
5:查看tast
[root@srever4 httpd]# ansible-playbook httpd.yml --list-task
playbook: httpd.yml
play #1 (server5): server5 TAGS: []
tasks:
install httpd TAGS: []
config httpd TAGS: []
start httpd TAGS: []
6:执行./yml文件
[root@srever4 httpd]# ansible-playbook httpd.yml
PLAY [server5] *****************************************************************
TASK [Gathering Facts] *********************************************************
ok: [server5]
TASK [install httpd] ***********************************************************
ok: [server5]
TASK [config httpd] ************************************************************
ok: [server5]
TASK [start httpd] *************************************************************
ok: [server5]
PLAY RECAP *********************************************************************
server5 : ok=4 changed=0 unreachable=0 failed=0
7:server5主机上进行查看
[root@server5 conf]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since 三 2019-03-27 18:05:27 CST; 15min ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 2810 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─2810 /usr/sbin/httpd -DFOREGROUND
├─2811 /usr/sbin/httpd -DFOREGROUND
├─2812 /usr/sbin/httpd -DFOREGROUND
├─2813 /usr/sbin/httpd -DFOREGROUND
├─2814 /usr/sbin/httpd -DFOREGROUND
└─2815 /usr/sbin/httpd -DFOREGROUND
3月 27 18:05:26 server5 systemd[1]: Starting The Apache HTTP Server...
3月 27 18:05:27 server5 httpd[2810]: AH00558: httpd: Could not reliably de...ge
3月 27 18:05:27 server5 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
成功
8:加入之前的server5上已经安装了httpd服务,也可以直接从start httpd开始
[root@srever4 httpd]# ansible-playbook httpd.yml --start-at-task="start httpd"
PLAY [server5] *****************************************************************
TASK [Gathering Facts] *********************************************************
ok: [server5]
TASK [start httpd] *************************************************************
ok: [server5]
PLAY RECAP *********************************************************************
server5 : ok=2 changed=0 unreachable=0 failed=0
9:查看server4和server5上httpd.conf文件的md5sum,刚开始时两者应该时一样的
[root@srever4 httpd]# md5sum httpd.conf
f5e7449c0f17bc856e86011cb5d152ba httpd.conf
[root@server5 conf]# md5sum httpd.conf
f5e7449c0f17bc856e86011cb5d152ba httpd.conf
此时在server4上修改配置文件,修改端口为8080
此时在server5主机上修改httpd服务的配置文件时没有用的,因为server4主机上的文件会将server5上的给覆盖掉
[root@srever4 httpd]# vim httpd.conf
[root@srever4 httpd]# md5sum httpd.conf
04e9239e7bd5d5b9b85864226d60eee5 httpd.conf ###再次查看server4上的已经发生改变
此时在执行./yml文件
[root@srever4 httpd]# ansible-playbook httpd.yml
PLAY [server5] *****************************************************************
TASK [Gathering Facts] *********************************************************
ok: [server5]
TASK [install httpd] ***********************************************************
ok: [server5]
TASK [config httpd] ************************************************************
changed: [server5] ##此时会显示changed,因为我们刚刚修改了配置文件
TASK [start httpd] *************************************************************
ok: [server5]
RUNNING HANDLER [restart httpd] ************************************************
changed: [server5]
PLAY RECAP *********************************************************************
server5 : ok=5 changed=2 unreachable=0 failed=0
server5上查看httpd服务的端口号:已经变为8080
tcp6 0 0 :::8080 :::* LISTEN 4496/httpd
四:template模块
基于模板方式生成一个文件复制到远程主机(template使用Jinjia2格式作为文件模版,进行文档内变量的替换的模块。它的每次使用都会被ansible标记为”changed”状态。)
– backup: 如果原目标文件存在,则先备份目标文件
– src:在ansible控制器上的Jinja2格式化模板的路径。 这可以是相对或绝对的路径。
– dest:将模板渲染到远程机器上的位置。
force:是否强制覆盖,默认为yes
– owner:目标文件属主
– group:目标文件属组
– mode:目标文件的权限模式,模式可以被指定为符号模式(例如,u + rwx或u = rw,g = r,o = r)
1:文件重命名,必须是.j2结尾
[root@srever4 httpd]# mv httpd.conf httpd.conf.j2
[root@srever4 httpd]# vim httpd.conf.j2 ##修改文件
Listen {{ http_port }} ##使用变量代替,之前修改httpd服务的端口号,必须修改httpd.conf配置文件,此时不需要了
只需要在httpd.yml文件中进行修改就好
[root@srever4 httpd]# vim httpd.yml
---
- hosts: server5
vars:
http_port: 80 ##定义变量http_port,要和.j2文件中写的要一样
remote_user: root
tasks:
- name: install httpd
yum: name=httpd state=present
- name: config httpd
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf ##将copy模块换成 template模块,src=httpd.conf.j2
notify: restart httpd
- name: start httpd
service: name=httpd state=started
handlers:
- name: restart httpd
service: name=httpd state=restarted
执行./yml文件
[root@srever4 httpd]# ansible-playbook httpd.yml
PLAY [server5] *****************************************************************
TASK [Gathering Facts] *********************************************************
ok: [server5]
TASK [install httpd] ***********************************************************
ok: [server5]
TASK [config httpd] ************************************************************
changed: [server5] ###会显示changed
TASK [start httpd] *************************************************************
changed: [server5]
RUNNING HANDLER [restart httpd] ************************************************
changed: [server5]
PLAY RECAP *********************************************************************
server5 : ok=5 changed=3 unreachable=0 failed=0
server5主机上端口是否修改为80(之前为8080)
tcp6 0 0 :::80 :::* LISTEN 5615/httpd
如果中间出错会生成一个.retry 文件,删除就好
[root@srever4 httpd]# ls
httpd.conf.j2 httpd.retry httpd.yml
1:在定义主机的配置文件中,添加server4主机httpd服务的端口
[root@srever4 httpd]# vim /etc/ansible/hosts
[web]
server4 http_port=8080
[db]
server5
[root@srever4 httpd]# vim httpd.yml
---
- hosts: all ##将host主机修改为all
vars:
http_port: 80 ##此时端口变量设置的是80,host文件中设置的是http_port=8080,执行.yml文件,看server4主机的httpd服务的端口号是80还是8080
remote_user: root
tasks:
- name: install httpd
yum: name=httpd state=present
- name: config httpd
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify: restart httpd
- name: start httpd
service: name=httpd state=started
handlers:
- name: restart httpd
service: name=httpd state=restarted
[root@srever4 httpd]# ansible-playbook httpd.yml ##执行.yml文件
[root@srever4 httpd]# ansible-playbook httpd.yml
PLAY [all] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [server5]
ok: [server4]
TASK [install httpd] ***********************************************************
ok: [server5]
changed: [server4]
TASK [config httpd] ************************************************************
ok: [server4]
ok: [server5]
TASK [start httpd] *************************************************************
ok: [server5]
changed: [server4]
PLAY RECAP *********************************************************************
server4 : ok=4 changed=2 unreachable=0 failed=0
server5 : ok=4 changed=0 unreachable=0 failed=0
查看端口:发现是80,说明.yml文件的优先级别更高
[root@srever4 httpd]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1365/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1998/master
tcp 0 0 172.25.60.4:39398 172.25.60.4:22 TIME_WAIT -
tcp 0 0 172.25.60.4:22 172.25.60.250:44794 ESTABLISHED 2155/sshd: root@pts
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::80 :::* LISTEN 3904/httpd
tcp6 0 0 :::22 :::* LISTEN 1365/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1998/master
五:使用变量获取端口ip
1:修改文件,不同的主机对httpd服务的端口定义不同
[root@srever4 httpd]# vim /etc/ansible/hosts
[web]
server4 http_port=8080
[db]
server5 http_port=80
2:将./yml文件中对端口的定义注释
[root@srever4 httpd]# vim httpd.yml
# vars:
# http_port: 80
3:执行.yml文件
[root@srever4 httpd]# ansible-playbook httpd.yml
PLAY [all] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [server5]
ok: [server4]
TASK [install httpd] ***********************************************************
ok: [server5]
ok: [server4]
TASK [config httpd] ************************************************************
ok: [server5]
changed: [server4]
TASK [start httpd] *************************************************************
ok: [server5]
ok: [server4]
RUNNING HANDLER [restart httpd] ************************************************
changed: [server4]
PLAY RECAP *********************************************************************
server4 : ok=5 changed=2 unreachable=0 failed=0
server5 : ok=4 changed=0 unreachable=0 failed=0
4:查看端口
[root@srever4 httpd]# netstat -antlp ##server4上为8080
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1365/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1998/master
tcp 0 0 172.25.60.4:22 172.25.60.250:46668 ESTABLISHED 3977/sshd: root@pts
tcp 0 0 172.25.60.4:22 172.25.60.250:44794 ESTABLISHED 2155/sshd: root@pts
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::8080 :::* LISTEN 4509/httpd
tcp6 0 0 :::22 :::* LISTEN 1365/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1998/master
[root@server5 conf]# netstat -antlp ##server5上为80 发现只能看到端口,不能看到对应的主机ip地址
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1357/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2037/master
tcp 0 0 172.25.60.5:22 172.25.60.250:47080 ESTABLISHED 2163/sshd: root@pts
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::80 :::* LISTEN 5615/httpd
tcp6 0 0 :::22 :::* LISTEN 1357/sshd
tcp6 0 0 ::1:25 :::* LISTEN 2037/master
5:此时我想把端口对应的主机ip取出来
[root@srever4 httpd]# vim httpd.conf.j2 ##修改.j2文件
Listen {{ http_host }}:{{ http_port }}
6:修改hosts文件
[root@srever4 httpd]# vim /etc/ansible/hosts
[web]
server4 http_host=172.25.60.4
[db]
server5 http_host=172.25.60.5
[webserver:children] 或者:[all:var] ##也可以将all换成单个组
http_port=80
web
db
[webserver:vars]
http_port=80 ##将端口都修改为80
7:执行.yml文件
[root@srever4 httpd]# ansible-playbook httpd.yml
PLAY [all] *********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [server5]
ok: [server4]
TASK [install httpd] ***********************************************************
ok: [server5]
ok: [server4]
TASK [config httpd] ************************************************************
changed: [server5]
changed: [server4]
TASK [start httpd] *************************************************************
ok: [server5]
ok: [server4]
RUNNING HANDLER [restart httpd] ************************************************
changed: [server5]
changed: [server4]
PLAY RECAP *********************************************************************
server4 : ok=5 changed=2 unreachable=0 failed=0
server5 : ok=5 changed=2 unreachable=0 failed=0
8:查看端口信息
[root@srever4 httpd]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
##ip(172.25.60.4取出来了,并且端口为80
tcp 0 0 172.25.60.4:80 0.0.0.0:* LISTEN 5067/httpd
[root@server5 conf]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
##ip(172.25.60.5)取出来了,并且端口为80
tcp 0 0 172.25.60.5:80 0.0.0.0:* LISTEN 6881/httpd
该方法取出端口对应的ip地址,不是使用了变量的方法而是在 /etc/ansible/hosts文件中写死了
下面介绍使用变量的方法取出端口对应的ip
方法1:
[root@server1 httpd]# vim httpd.conf.j2
42 Listen {{ http_host }}:{{ http_port }}
[root@server4 httpd]# vim httpd.yml
3 - hosts: all # 更改所有节点
4 vars:
5 http_port: 8080
12 template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
[root@server1 httpd]# vim /etc/ansible/hosts
44 [web]
45 server4
46
47 [db]
48 server5
49
50 [all:vars] # 指定组
51 http_host={{ ansible_all_ipv4_addresses[0] }} # 指定http_host变量,取值为各个节点的ip 执行 ansible all -m setup -a "filter=ansible_all_ipv4_addresses" 时可以显示节点ip
[root@server4 httpd]# ansible-playbook httpd.yml # 推送
[root@server4 httpd]# netstat -antlp | grep httpd
tcp 0 0 172.25.60.4:8080 0.0.0.0:* LISTEN 16767/httpd # chenggong
[root@server5 conf]# netstat -antlp | grep httpd
tcp 0 0 172.25.60.5:8080 0.0.0.0:* LISTEN 16748/httpd
方法2:
[root@server4 httpd]# vim httpd.conf.j2
42 Listen {{ ansible_all_ipv4_addresses[0] }}:{{ http_port }} #使用变量
[root@server4 httpd]# vim httpd.yml
3 - hosts: all # 更改所有节点
4 vars:
5 http_port: 8080
12 template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
[root@server4 httpd]# ansible-playbook httpd.yml # 推送
[root@server4 httpd]# netstat -antlp | grep httpd
tcp 0 0 172.25.60:4:8080 0.0.0.0:* LISTEN 16767/httpd # chenggong
[root@server5 conf]# netstat -antlp | grep httpd
tcp 0 0 172.25.60.5:8080 0.0.0.0:* LISTEN 16748/httpd
此时在/etc/httpd/conf/httpd.conf 文件下已经能够看到了
Listen 172.25.60.4:80