熟练运用ansible(一)

熟练运用ansible(一)

什么是Ansible?

ansible是一款开源自动化的工具,能够自动执行配置管理、部署编排等许多手动流程。ansible的好处,简单易读,功能强大,无代理,跨平台支持,如linux,Windows,Unix和网络设备。

在这里插入图片描述

Ansible产品特色

  • 基于SSH架构
  • 模块丰富
  • 社区活跃
  • 支持自定义模块
  • 支持异构IT架构
  • 部署简单,容易上手

Ansible工作原理

把我们执行的命令翻译为shell命令,通过openssh拷贝到/root/.ansible/tmp/下执行,执行完成后删除tmp文件Ansible默认是不在yum仓库中的,需要下载epel-release镜像源才能安装ansible,但是2015年ansible被红帽收购后,ansible镜像源就融合进默认yum仓库了,这边要是没有ansible默认yum仓库,链接:https://pan.baidu.com/s/18uN4F2Vtg4SBLTdqhYX2tw ,提取码:3e3n,下载。

部署环境

在这里插入图片描述

  • 准备六台主机,需要配置主机名、IP地址、YUM。关闭selinux和防火墙
  • 控制主机需要:
  • 配置名称解析,能够通过名字访问所有节点
  • 配置可以通过ssh到所有被控主机免密登陆
  • 通过yum源安装ansible,或者通过分享的链接压缩包ansible到控制主机上,并解压安装。
# 配置名称解析
[root@control ~]# echo -e  "192.168.88.253\tcontrol" >> /etc/hosts
[root@control ~]# for i in {1..5}
> do
> echo -e  "192.168.88.1$i\tbeikongji$i"  >>  /etc/hosts
> done
[root@control ~]# tail  -6  /etc/hosts
192.168.88.253	control
192.168.88.11	beikongji1
192.168.88.12	beikongji2
192.168.88.13	beikongji3
192.168.88.14	beikongji4
192.168.88.15	beikongji5

# 配置免密登陆
[root@control ~]# ssh-keygen           # 三个问题都回车,使用默认值
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? 
[root@control ~]# for i  in  beikongji{1..5}   
> do
> ssh-copy-id $i
> done
 #: 回答yes和密码
 
#:装包,这边我直接把ansible压缩包放再家目录
[root@control ~]# ls
anaconda-ks.cfg  ansible_soft.tar.gz
[root@control ~]# tar  -xf  ansible_soft.tar.gz 
[root@control ~]# ls
anaconda-ks.cfg  ansible_soft  ansible_soft.tar.gz
[root@control ~]# cd  ansible_soft/
[root@control ansible_soft]# ls
ansible-2.8.5-2.el8.noarch.rpm           python3-paramiko-2.4.3-1.el8.noarch.rpm
libsodium-1.0.18-2.el8.x86_64.rpm        python3-pynacl-1.3.0-5.el8.x86_64.rpm
python3-bcrypt-3.1.6-2.el8.1.x86_64.rpm  sshpass-1.06-9.el8.x86_64.rpm
[root@control ansible_soft]# yum   install  -y  *.rpm

配置ansible管理

  • 因为要管理的远程主机可能不一样,有可能是云上的设备也有可能是物理设备,所以具有相同管理方式的配置放到一个目录下。
#:创建ansible工作目录,目录名自己定义,不是固定的。
[root@control ~]# mkdir  ansible
[root@control ~]# cd  ansible


#:创建配置文件。默认的配置文件是/etc/ansible/ansible.cfg,但是一般不使用它,而是再工作目录下创建自己的配置文件
[root@control ansible]# vim  ansible.cfg   #:文件名必须是ansible.cfg
[defaults]
inventory = hosts                 #:管理的主机,配置再当前目录的hosts文件中,hosts名是自定义的。=等号两边空格可有可无。



#: 创建主机清单文件。写在[]里的是组名,[]下面的是组内的主机
[root@control ansible]# vim  hosts
[test]
beikongji1


[proxy]
beikongji2


[webserver]
beikongji[3:4]      #:beikongji3和beikongji4,简单化表示3到4.


[database]
beikongji5


#:cluster是组名,自定义的;:children是固定写法,表示下面的组名是cluster的子组。
[cluster:children]
webserver
database
       

#:查看被管理的所有的主机。注意,一定在工作目录下执行命令。
[root@control ansible]# ansible  all --list-hosts
  hosts (5):
    beikongji1
    beikongji2
    beikongji3
    beikongji4
    beikongji5
    
    
#:查看webserver组中所有的主机
[root@control ansible]# ansible  webserver  --list-hosts
  hosts (2):
    beikongji3
    beikongji4

ansible管理

  • ansible进行远程管理的两个方法:

    • adhoc临时命令。就是在命令上执行管理命令。
    • playbook剧本。把管理任务用特定格式(YAML语法ansible第二章有讲)写到文件中。
  • 无论哪种方式,都是通过模块加参数进行管理。

adhoc临时命令

  • 语法
ansible 主机或组列表  -m  模块  “参数”  # -a是可选的
  • 测试到远程主机的连通性
[root@control ansible]# ansible  all  -m  ping
beikongji2 | SUCCESS => {                          #:看到SUCCESS说明连通成功
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
beikongji5 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
beikongji3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
beikongji4 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
beikongji1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

shell模块

  • 与command模块类似,但是支持shell特性,如管道、重定向。
#:查看beikongji1的root目录。
[root@control ansible]# ansible  beikongji1  -m  shell  -a  "ls  /root"
beikongji1 | CHANGED | rc=0 >>         #:CHANGED显示有改动
anaconda-ks.cfg

script模块

  • 用于远程主机上执行脚本
#:在控制端创建脚本
[root@control ansible]# vim  test.sh
#!/bin/bash
#:create file

touch  xh.txt
echo  "创建成功"


#:在test组的主机上执行脚本
[root@control ansible]# ansible test  -m  script  -a  "test.sh"
beikongji1 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to beikongji1 closed.\r\n",
    "stderr_lines": [
        "Shared connection to beikongji1 closed."
    ],
    "stdout": "创建成功\r\n",
    "stdout_lines": [
        "创建成功"
    ]
}

file模块

  • 可以创建文件、目录、链接等,还可以修改权限、属性等

  • 常用的选项:

    • path:指定文件路径

    • owner:设置文件所有者

    • group: 设置文件所属组

    • state:状态。touch表示创建文件,directory表示创建目录,link表示创建软链接,absent表示删除

    • mode:设置权限

    • src:source的简写,源

    • dest: destination的简写,目标

#:查看使用帮助
[root@control ansible]# ansible-doc  file
EXAMPLES:

- name: Change file ownership, group and permissions 
  file:                      #:模块名字。以下是它的各种参数
    path: /etc/foo.conf      #:要修改的文件的路径
    owner: foo                #:文件所有者
    group: foo                #:文件的属组
    mode: '0644'              #:权限
。。。 。。。。
# 根据上面的example,-m file -a的内容就是doc中把各参数的冒号换成=号


# 在test主机上创建/tmp/file.txt
[root@control ansible]# ansible test -m file -a "path=/tmp/file.txt state=touch"   
# touch是指如果文件不存在,则创建

# 在test主机上创建/tmp/demo目录
[root@control ansible]# ansible test -m file -a "path=/tmp/demo state=directory"




# 将test主机上/tmp/file.txt的属主改为sshd,属组改为adm,权限改为0777
[root@control ansible]# ansible test -m file -a "path=/tmp/file.txt owner=sshd group=adm mode='0777'"
[root@control ansible]# ansible test -a "ls -l /tmp/file.txt"



# 删除test主机上/tmp/file.txt
[root@control ansible]# ansible test -m file -a "path=/tmp/file.txt state=absent"  
# absent英文缺席的、不存在的



# 删除test主机上/tmp/demo
[root@control ansible]# ansible test -m file -a "path=/tmp/demo state=absent"



# 在test主机上创建/etc/hosts的软链接,目标是/tmp/hosts.txt
[root@control ansible]# ansible test -m file -a "src=/etc/hosts dest=/tmp/hosts.txt state=link"

查看ansible模块方法

  • 模块基本信息查看
#:列出ansible的所有模块
[root@control ansible]# ansible-doc -l

#:查看与file相关的模块
[root@control ansible]# ansible-doc -l | grep  file

#:查看file模块的使用说明,主要查看下方的EXAMPLE例子
[root@control ansible]# ansible-doc  file
  • 学习模块,主要知道实现某种功能,需要哪个模块。
  • 模块的使用方式都一样。主要是查看该模块有哪些参数。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值