ansible自动化运维工具

文章目录

  • ansible简介
    • ansible 任务执行模式
    • ansible 命令执行过程
    • ansible的安装配置
    • 创建文件以及sudo
  • Inventory文件
    • 用户的隔离
    • 拷贝模块
  • Ansible Ad-Hoc命令集

ansible简介

ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。主要包括:
(1)、连接插件connection plugins:负责和被监控端实现通信;
(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
(3)、各种模块核心模块、command模块、自定义模块;
(4)、借助于插件完成记录日志邮件等功能;
(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。

Ansible:Ansible核心程序。
HostInventory:记录由Ansible管理的主机信息,包括端口、密码、ip等。
Playbooks:“剧本”YAML格式文件,多个任务定义在一个文件中,定义主机需要调用哪些模块来完成的功能。
CoreModules:核心模块,主要操作是通过调用核心模块来完成管理任务。
CustomModules:自定义模块,完成核心模块无法完成的功能,支持多种语言。
ConnectionPlugins:连接插件,Ansible和Host通信使用

ansible 任务执行模式

Ansible 系统由控制主机对被管节点的操作方式可分为两类,即adhoc和playbook:

ad-hoc模式(点对点模式)
  使用单个模块,支持批量执行单条命令。ad-hoc 命令是一种可以快速输入的命令,而且不需要保存起来的命令。就相当于bash中的一句话shell。
playbook模式(剧本模式)
  是Ansible主要管理方式,也是Ansible功能强大的关键所在。playbook通过多个task集合完成一类功能,如Web服务的安装部署、数据库服务器的批量备份等。可以简单地把playbook理解为通过组合多条ad-hoc操作的配置文件。

ansible 命令执行过程

加载自己的配置文件,默认/etc/ansible/ansible.cfg;
查找对应的主机配置文件,找到要执行的主机或者组;
加载自己对应的模块文件,如 command;
通过ansible将模块或命令生成对应的临时py文件(python脚本), 并将该文件传输至远程服务器;
对应执行用户的家目录的.ansible/tmp/XXX/XXX.PY文件;
给文件 +x 执行权限;
执行并返回结果;
删除临时py文件,sleep 0退出;

ansible的安装配置

1,首先配好yum源

[epel]
name=epel
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/
gpgcheck=0

2,

yum install ansible -y  只需要在主节点安装
安装完成之后会生成默认目录/etc/ansible/
[root@server1 ansible]# ls
ansible.cfg  hosts  roles
ansible --version   #可以查看ansible的版本

3,修改hosts文件添加主机,有解析添加IP或者添加主机名都可以,然后还需要做免密还需要给节点创建ansible用户

ansible自动化运维工具_第1张图片

[root@server1 ansible]# vim hosts 
[root@server1 ansible]# ansible all --list-hosts
  hosts (2):
    server2
    server3
[root@server1 ansible]# ansible test --list-hosts
  hosts (1):
    server2
[root@server1 ansible]# ansible test1 --list-hosts
  hosts (1):
    server3

创建用户做免密:

server2和server3创建用户
useradd ansibel
echo redhat |passdwd --stdin ansible
做免密:
ssh-keygen
ssh-copy-id server2
ssh-copy-id server3
ssh-copy-id ansible@server2  #用户的免密
ssh-copy-id ansible@server3


[root@server1 ansible]# ansible all -m ping
server3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
server2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

创建文件以及sudo

使用ansible用户创建文件

[root@server1 ansible]#  ansible all -a "touch /tmp/testfile" -u ansible
server2 | CHANGED | rc=0 >>


server3 | CHANGED | rc=0 >>

[root@server1 ansible]# ansible all -a "ls  -l  /tmp/testfile"
server3 | CHANGED | rc=0 >>
-rw-rw-r--. 1 ansible ansible 0 Mar 10 03:15 /tmp/testfile

server2 | CHANGED | rc=0 >>
-rw-rw-r--. 1 ansible ansible 0 Mar 10 03:15 /tmp/testfile

创建成功

在mnt下创建则失败
[root@server1 ansible]#  ansible all -a "touch /mnt/testfile" -u ansible
server3 | FAILED | rc=1 >>
touch: cannot touch ‘/mnt/testfile’: Permission deniednon-zero return code

server2 | FAILED | rc=1 >>
touch: cannot touch ‘/mnt/testfile’: Permission deniednon-zero return code

原因是在mnt下ansible用户是没用权利的需要sudo所以需要加入-b

[root@server1 ansible]#  ansible all -a "touch /mnt/testfile" -u ansible -b -k
SSH password: 
server2 | CHANGED | rc=0 >>


server3 | CHANGED | rc=0 >>


[root@server1 ansible]#  ansible all -a "ls -l  /mnt/testfile" -u ansible -b -k
SSH password: 
server2 | CHANGED | rc=0 >>
-rw-r--r--. 1 root root 0 Mar 10 03:24 /mnt/testfile

server3 | CHANGED | rc=0 >>
-rw-r--r--. 1 root root 0 Mar 10 03:24 /mnt/testfile

也可以做免密:

server2和server3都需要
visudo
root    ALL=(ALL)       ALL
ansible ALL=(ALL)       NOPASSWD: ALL


 ansible all -a "touch /mnt/testfile" -u ansible -b
server2 | CHANGED | rc=0 >>


server3 | CHANGED | rc=0 >>

Inventory文件

Inventory就是hosts文件,只不过需要用-i来指令,这样更方便管理,

vim /etc/ansible/inventory
[tset2]
server2

[root@server1 ansible]#  ansible all -i inventory -m ping 
server2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

组的嵌套:就是可以调用一个或者几个组

vim hosts

[test]
server2

[test1]
server3

[webserver:children]
test
test1

[root@server1 ansible]# ansible webserver --list-hosts
  hosts (2):
    server2
    server3

用户的隔离

/etc/ansible/ansible.cfg是全局生效
就是让我们普通用户来管理ansible,首先在server1创建用户ansible,再拷贝/etc/ansible/ansible.cfg到ansible的家目录,进行隐藏。然后创建目录拷贝/etc/ansible/ansible.cfg对文件进行修改。

ansible读取顺序,先读取当前目录下的文件,没有的话就读取家目录下的文件,在没有就读取etc/ansible/ansible.cfg

 vim .ansible.cfg 
[defaults]
inventory        = ~/hosts   #读取的文件
command_warnings = False

[privilege_escalation]   #全局的定义默认-b 
#become=True                  
#become_method=sudo
#become_user=root
#become_ask_pass=False


[ansible@server1 ~]$ ansible all --list-hosts
  hosts (1):
    server2

拷贝模块

[ansible@server1 ~]$ ansible test3 -m  copy -a "src=hosts dest=/tmp"
server2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "b9e68d31db2ee2befd4059628918eb9710861863", 
    "dest": "/tmp/hosts", 
    "gid": 1001, 
    "group": "ansible", 
    "md5sum": "9e476f46381c0b3ad28587b3d9bb35d9", 
    "mode": "0664", 
    "owner": "ansible", 
    "secontext": "unconfined_u:object_r:user_home_t:s0", 
    "size": 16, 
    "src": "/home/ansible/.ansible/tmp/ansible-tmp-1583841876.7-157682373214467/source", 
    "state": "file", 
    "uid": 1001
}
[ansible@server1 ~]$ ansible test3 -a "ls -l /tmp"
server2 | CHANGED | rc=0 >>
total 4
drwx------. 2 ansible ansible 41 Mar 10 05:05 ansible_command_payload_iL9HWB
-rw-rw-r--. 1 ansible ansible 16 Mar 10 05:04 hosts
drwx------. 3 root    root    17 Mar 10 02:51 systemd-private-1ccb35bfdd1e45aea87e1351bb437651-vmtoolsd.service-Ei7pVF
drwx------. 3 root    root    17 Mar  9 05:20 systemd-private-3d9a4922cb4c434f8b1a67c00bcc4675-vmtoolsd.service-NClX9S
drwx------. 3 root    root    17 Mar  9 01:19 systemd-private-cbdd91cf13ee47ad883533544e6c8898-vmtoolsd.service-fi9lMn
-rw-rw-r--. 1 ansible ansible  0 Mar 10 03:15 testfile

如果需要拷贝到mnt里面则需要认证需要sudo加-b也可以在配置文件里面修改

[defaults]
inventory        = ~/hosts
command_warnings = False

[privilege_escalation]  #去掉注释
become=True
become_method=sudo
become_user=root
become_ask_pass=False

        
[ansible@server1 ~]$ ansible test3 -m  copy -a "src=hosts dest=/mnt"
server2 | FAILED! => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "checksum": "b9e68d31db2ee2befd4059628918eb9710861863", 
    "msg": "Destination /mnt not writable"
}
[ansible@server1 ~]$ vim .ansible.cfg 
[ansible@server1 ~]$ ansible test3 -m  copy -a "src=hosts dest=/mnt"
server2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "b9e68d31db2ee2befd4059628918eb9710861863", 
    "dest": "/mnt/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "9e476f46381c0b3ad28587b3d9bb35d9", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "system_u:object_r:mnt_t:s0", 
    "size": 16, 
    "src": "/home/ansible/.ansible/tmp/ansible-tmp-1583842296.04-74730694953965/source", 
    "state": "file", 
    "uid": 0
}

Ansible Ad-Hoc命令集

ad-hoc模式(点对点模式)
  使用单个模块,支持批量执行单条命令。ad-hoc 命令是一种可以快速输入的命令,而且不需要保存起来的命令。就相当于bash中的一句话shell。更注重于解决一些简单或者平时工作中临时遇到的任务,
  Ansible和Ansible-playbook默认会fork 5个线程并发执行命令,如果同时操作的主机数比较多的话,可以调整到一个更大的值。
  Ansible的模块非常之多,Ansible也提供了类似于man功能的help说明工具ansible-doc。

 ansible test3 -m yum -a "name=httpd state=prsent" #test3安装httpd服务
 ansible teat3 -m service -a "name=httpd enabled=yes"  #服务开机自启动
 ansible teat3 -m service -a "name=fierwalld state=started"  #开启防火墙
 ansible test3 -m firewalld -a "service=http permanent=yes state=enabled immediate=yes" #配置httpd服务并立即生效
 
# ansible webservers -m copy -a "src=/etc/hosts   dest=/tmp/hosts"
file模块
# ansible webservers -m file -a "dest=/tmp/hosts mode=600 owner=root group=root"			#修改文件权限和属性
# ansible webservers -m file -a "dest=/tmp/dir1/dir2 mode=755 owner=root group=root state=directory"    #递归创建
# ansible webservers -m file -a "dest=/tmp/dir1/dir2 state=absent"
yum模块
# ansible webservers -m yum -a "name=httpd state=present"
# ansible server3 -m yum -a "name=http://172.25.0.250/rhel7.3/x86_64/dvd/Packages/vsftpd-3.0.2-21.el7.x86_64.rpm state=present"	#在线安装
# ansible server3 -m yum -a "name=/mnt/vsftpd-3.0.2-21.el7.x86_64.rpm state=present"			#本地安装
# ansible server3 -m yum -a "name=httpd state=absent"								#卸载软件
service模块
# ansible webservers -m service -a "name=httpd state=started"
# ansible webservers -m service -a "name=httpd state=restarted"
# ansible webservers -m service -a "name=httpd state=stopped"
user模块
# ansible all -m user -a "name=user1 password=<加密密码>"
# ansible all -m user -a "name=user1 state=absent remove=yes"
# ansibledb -m user -a "name=user1 shell=/bin/bash groups=users,wheel append=yes state=present" 
mysql_user模块
# ansible server3 -m mysql_user -a "name=ykx password=testpass priv=*.*:select host='%' state=present"
远程主机需要安装MySQL-python

你可能感兴趣的:(ansible自动化运维工具)