ansible 入门

ansible 入门
ansible是一款简单的运维自动化工具,只需ssh协议连接即可进行系统管理,自动化执行命令,部署等
特点:
1、不需要单独安装客户端,不需启动任何服务
2、是python中一套完整的自动化执行任务模块
3、playbook采用yaml配置,对于自动化任务执行一目了然
组成结构:
a、ansible,命令工具,核心执行工具,一次性或临时执行的操作都是通过该命令
b、ansible playbook 任务剧本,编排定义ansible任务集的配置文件,顺序依次执行,yaml格式
c、inventory 管理主机的清单,默认/etc/ansible/hosts
d、modules 执行命令的功能模块
e、plugins 插件
f、api 提供给第三方程序调用的应用程序编程接口

环境:
server:192.168.10.193
client:192.168.10.191 192.168.10.192 192.168.10.194 192.168.10.195 192.168.10.196
centos7.6

1、配置epol源并安装ansible
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
yum -y install ansible
ansible --version

2、ansible inventory文件
#inventory通常用于定义要管理的主机的认证信息,如ssh登录用户名密码及key等相关信息.可以同时操作一个组的多台主机,组和主机组之间的关系都是通过inventory文件配置,/etc/ansible/hosts

2.1、基于密码连接
cat /etc/ansible/hosts
[web1]
192.168.10.191 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.10.192 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
[web2]
192.168.10.19[4:6] ansible_ssh_user=root ansible_ssh_pass="123456"

[web3]
192.168.10.19[5:7]
[web3:vars]
ansible_ssh_user=root ansible_ssh_pass="123456" ansible_ssh_port=22

2.2、基于密钥连接
主机开启密钥认证方式
ssh-keygen
for i in {2,4,5}; do ssh-copy-id 192.168.10.19$i;done

[web4]
192.168.10.19[4:7]:22

[web5]
pay1 ansible_ssh_host=192.168.10.191 ansible_ssh_port=22
pay2 ansible_ssh_host=192.168.10.192 ansible_ssh_port=22
pay4 ansible_ssh_host=192.168.10.194 ansible_ssh_port=22

2.3、主机组的使用
#主机组变量名+主机组
[web3]
192.168.10.19[5:7]
[web3:vars]
ansible_ssh_user=root ansible_ssh_pass="123456" ansible_ssh_port=22

#主机组子群组
[webserver:children]
web4
web5

2.4、临时指定inventory
#定义主机清单
cat /etc/dockers
[dockers]
192.168.10.192 ansible_ssh_pass='123456'

#指定主机清单执行命令
ansible dockers -m ping -i /etc/dockers -o
192.168.10.192 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
ansible 入门
ansible是一款简单的运维自动化工具,只需ssh协议连接即可进行系统管理,自动化执行命令,部署等
特点:
1、不需要单独安装客户端,不需启动任何服务
2、是python中一套完整的自动化执行任务模块
3、playbook采用yaml配置,对于自动化任务执行一目了然
组成结构:
a、ansible,命令工具,核心执行工具,一次性或临时执行的操作都是通过该命令
b、ansible playbook 任务剧本,编排定义ansible任务集的配置文件,顺序依次执行,yaml格式
c、inventory 管理主机的清单,默认/etc/ansible/hosts
d、modules 执行命令的功能模块
e、plugins 插件
f、api 提供给第三方程序调用的应用程序编程接口

环境:
server:192.168.10.193
client:192.168.10.191 192.168.10.192 192.168.10.194 192.168.10.195 192.168.10.196
centos7.6

1、配置epol源并安装ansible
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
yum -y install ansible
ansible --version

2、ansible inventory文件
#inventory通常用于定义要管理的主机的认证信息,如ssh登录用户名密码及key等相关信息.可以同时操作一个组的多台主机,组和主机组之间的关系都是通过inventory文件配置,/etc/ansible/hosts

2.1、基于密码连接
cat /etc/ansible/hosts
[web1]
192.168.10.191 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.10.192 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
[web2]
192.168.10.19[4:6] ansible_ssh_user=root ansible_ssh_pass="123456"

[web3]
192.168.10.19[5:7]
[web3:vars]
ansible_ssh_user=root ansible_ssh_pass="123456" ansible_ssh_port=22

2.2、基于密钥连接
主机开启密钥认证方式
ssh-keygen
for i in {2,4,5}; do ssh-copy-id 192.168.10.19$i;done

[web4]
192.168.10.19[4:7]:22

[web5]
pay1 ansible_ssh_host=192.168.10.191 ansible_ssh_port=22
pay2 ansible_ssh_host=192.168.10.192 ansible_ssh_port=22
pay4 ansible_ssh_host=192.168.10.194 ansible_ssh_port=22

2.3、主机组的使用
#主机组变量名+主机组
[web3]
192.168.10.19[5:7]
[web3:vars]
ansible_ssh_user=root ansible_ssh_pass="123456" ansible_ssh_port=22

#主机组子群组
[webserver:children]
web4
web5

2.4、临时指定inventory
#定义主机清单
cat /etc/dockers
[dockers]
192.168.10.192 ansible_ssh_pass='123456'

#指定主机清单执行命令
ansible dockers -m ping -i /etc/dockers -o
192.168.10.192 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
ansible 入门
ansible是一款简单的运维自动化工具,只需ssh协议连接即可进行系统管理,自动化执行命令,部署等
特点:
1、不需要单独安装客户端,不需启动任何服务
2、是python中一套完整的自动化执行任务模块
3、playbook采用yaml配置,对于自动化任务执行一目了然
组成结构:
a、ansible,命令工具,核心执行工具,一次性或临时执行的操作都是通过该命令
b、ansible playbook 任务剧本,编排定义ansible任务集的配置文件,顺序依次执行,yaml格式
c、inventory 管理主机的清单,默认/etc/ansible/hosts
d、modules 执行命令的功能模块
e、plugins 插件
f、api 提供给第三方程序调用的应用程序编程接口

环境:
server:192.168.10.193
client:192.168.10.191 192.168.10.192 192.168.10.194 192.168.10.195 192.168.10.196
centos7.6

1、配置epol源并安装ansible
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
yum -y install ansible
ansible --version

2、ansible inventory文件
#inventory通常用于定义要管理的主机的认证信息,如ssh登录用户名密码及key等相关信息.可以同时操作一个组的多台主机,组和主机组之间的关系都是通过inventory文件配置,/etc/ansible/hosts

2.1、基于密码连接
cat /etc/ansible/hosts
[web1]
192.168.10.191 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.10.192 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
[web2]
192.168.10.19[4:6] ansible_ssh_user=root ansible_ssh_pass="123456"

[web3]
192.168.10.19[5:7]
[web3:vars]
ansible_ssh_user=root ansible_ssh_pass="123456" ansible_ssh_port=22

2.2、基于密钥连接
主机开启密钥认证方式
ssh-keygen
for i in {2,4,5}; do ssh-copy-id 192.168.10.19$i;done

[web4]
192.168.10.19[4:7]:22

[web5]
pay1 ansible_ssh_host=192.168.10.191 ansible_ssh_port=22
pay2 ansible_ssh_host=192.168.10.192 ansible_ssh_port=22
pay4 ansible_ssh_host=192.168.10.194 ansible_ssh_port=22

2.3、主机组的使用
#主机组变量名+主机组
[web3]
192.168.10.19[5:7]
[web3:vars]
ansible_ssh_user=root

ansible_ssh_pass="123456"

ansible_ssh_port=22

#主机组子群组
[webserver:children]
web4
web5

2.4、临时指定inventory
#定义主机清单
cat /etc/dockers
[dockers]
192.168.10.192 ansible_ssh_pass='123456'

#指定主机清单执行命令
ansible dockers -m ping -i /etc/dockers -o
192.168.10.192 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

  #内置参数


3、ansible ad-hoc
#ad-hoc 临时的,指在ansible中快速执行,并且不需要保存的命令.就是执行简单的命令(一条命令),而复杂的命令则为playbook,类似于saltstack的state sls状态文件

3.1、命令格式
Usage: ansible <host-pattern> [options]
-a MODULE_ARGS   #模块参数
-C, --check  #检查语法
-f FORKS #并发
--list-hosts #列出主机列表
-m MODULE_NAME #模块名字
-o 使用精简的输出

ansible web1 -m shell -a 'uptime' -o
192.168.10.192 | CHANGED | rc=0 | (stdout)  15:49:55 up 7 days,  6:11,  2 users,  load average: 0.00, 0.01, 0.05
192.168.10.191 | CHANGED | rc=0 | (stdout)  15:49:55 up 7 days,  6:12,  3 users,  load average: 0.00, 0.01, 0.05
#ansible--需执行命令的目标主机或群组--选项指定用到模块--模块--选项指定模块中要用的参数--模块中的参数(shell命令)
command target option module option cmd
#不加-m,默认执行shell命令,-a必有,其它自取即用-o精简输出

3.2、host-pattern格式
目标主机,主机组匹配方式

--主机匹配---
ansible 192.168.10.191 -m ping
#多台主机用逗号隔开,所有主机用 all表示

--主机组匹配---
[root@pay3 ~]# ansible web1 --list  #查看组信息, 包含哪些主机
  hosts (2):
    192.168.10.191
    192.168.10.192

ansible 'web2:!web3' -m ping -o    #匹配web2中有,但web3组中没有的所有主机
ansible 'web2:&web4' -m ping -o    #匹配两个组都有的主机,并集
ansible 'web2:web3' -m ping -o    #匹配两个组所有的主机

posted @ 2020-01-08 13:44  一无是处谢  阅读(154)  评论(0编辑  收藏  举报