python ansible

1.简介

 

Ansible:Ansible的核心程序

Host Lnventory:记录了每一个由Ansible管理的主机信息,信息包括ssh端口,root帐号密码,ip地址等等。可以通过file来加载,可以通过CMDB加载

Playbooks:YAML格式文件,多个任务定义在一个文件中,使用时可以统一调用,“剧本”用来定义那些主机需要调用那些模块来完成的功能.

Core Modules:Ansible执行任何管理任务都不是由Ansible自己完成,而是由核心模块完成;Ansible管理主机之前,先调用core Modules中的模块,然后指明管理Host Lnventory中的主机,就可以完成管理主机。

Custom Modules:自定义模块,完成Ansible核心模块无法完成的功能,此模块支持任何语言编写。

Connection Plugins:连接插件,Ansible和Host通信使用

 

特性

(1)、no agents:不需要在被管控主机上安装任何客户端;
(2)、no server:无服务器端,使用时直接运行命令即可;
(3)、modules in any languages:基于模块工作,可使用任意语言开发模块;
(4)、yaml,not code:使用yaml语言定制剧本playbook;
(5)、ssh by default:基于SSH工作;
(6)、strong multi-tier solution:可实现多级指挥。
 

优点

(1)、轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;
(2)、批量任务执行可以写成脚本,而且不用分发到远程就可以执行;
(3)、使用python编写,维护更简单,ruby语法过于复杂;
(4)、支持sudo。

 

Ansible读取配置文件的顺序:

ansible.cfg(位于当前位置)
ANSIBLE_CONFIG          一个环境变量
.ansible.cfg                    位于家目录下
/etc/ansible/ansible.cfg

2.Ansible的优化

1. 开启ssh长连接:
ansible模式是使用ssh和被管机器进行通信的,所以ansible对ssh的依赖非常强,那么我们就从ssh入手来优化ansible 在openssh5.6以后的版本就支持了Multiplexing,如果我们中控机中的ssh -v版本大于5.6那么我们可以直接在ansible.cfg文件中设置ssh长连接即可
ssh_args = -C -o ControlMaster=auto -o ControlPersist=5d
ControlPersisit=5d 这个参数是设置整个长连接保持时间这里设置为5天,如果开启后,通过ssh链接过的设备都会在当前ansible/cp目录下面生成一个socket文件,也可以通过netstat命令查看,会发现有一个ESTABLISHED状态的连接一直与远端设备进行着TCP连接

3.Ansible的模块

 

使用:ansible + 主机组名称 + -m + 模块名称 + -a + 参数
    主机组名称,即hosts中定义的主机组名称 
    -m 指使用模块,后加指定的模块名称 
    -a 指传给模块的参数
    在不指定模块时,默认调用command模块。 

查看所有主机 :ansible all --list-host

(1)ping模块

ansible all -m ping
 

(2)command模块

 ansible 192.168.48.129  -m command -a "w“

 

 

(3)shell模块

 ansible –i 1.txt all  -m shell  -a "w" 

Command模块和shell模块都是远程执行命令的模块,但是推荐使用shell模块,当我们需要在字符串中使用特殊字符的时候,使用shell就不需要转义

 

(4)file设置文件属性

查看模块参数:
[root@sixgod ansible]# ansible-doc -s file
- name: Sets attributes of files
action: file
force                  # 需要在两种情况下创建软连接,一种是源文件不存在,但之后建立的情况下;另一种是目标软连接已存在,需要先取消之前的软链,然后创建新的软链:有两个选项:yes|no
group                  # 定义文件/目录的所属组
mode                   # 定义文件/目录的权限
owner                  # 定义文件/目录的所属主
path=                  # 必选项,定义文件/目录的路径
recurse                # 递归设置文件的属性,只对目录有效
src                    # 要被链接的源文件路径,只应用于state=link的情况有效
state                  # 状态
        directory        #如果目录不存在则创建目录
        file                #即使文件不存在,也不会被创建
        link                #创建软连接
        hard                #创建硬链接
        touch               #如果文件不存在,则会创建一个新的文件;如果文件或目录存在,则更新其最后的修改时间
        absent              #删除目录、文件或者取消链接文件
(END)

将远程主机/etc/passwd 文件链接到 /root 目录下:

ansible test -m file -a "src=/etc/passwd dest=/root/passwd state=link"                
192.168.1.132 | SUCCESS => {
    "changed": true, 
    "dest": "/root/passwd", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "size": 11, 
    "src": "/etc/passwd", 
    "state": "link", 
    "uid": 0
}

 删除创建的软连接

[root@sixgod ansible]# ansible all -m file -a 'path=/root/passwd state=absent' 
192.168.1.132 | SUCCESS => {
    "changed": true, 
    "path": "/root/passwd", 
    "state": "absent"
}

创建文件

[root@sixgod ansible]# ansible all -m file -a 'path=/root/1 state=touch'
192.168.1.132 | SUCCESS => {
    "changed": true, 
    "dest": "/root/1", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "size": 0, 
    "state": "file", 
    "uid": 0

}

(5)cope模块 将文件copy至远程主机

先在本地创建一个文件:[root@sixgod ansible]# echo 123 >./a.txt

将文件传到远程主机上:[root@sixgod ansible]# ansible all -m copy -a "src=/etc/ansible/a.txt dest=/opt/a.txt owner=root group=root mode=0755"

src:指定要传的文件    dest:指定远程主机的位置         后面是权限,如果不指定则保持不变。

(6)cron模块 定时任务模块

 

制定任务:

[root@sixgod ansible]# ansible all -m cron -a "name='sixgod' user=root job='touch /a.txt' minute=*/1 hour=* day=* month=* weekday=*"

192.168.1.132 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "sixgod"
    ]
}

name:给此定时任务设置名字

user:给哪个用户设置任务

job:任务内容

查看:root@sixgod ansible]# ansible all -m shell -a "crontab -l"
192.168.1.132 | SUCCESS | rc=0 >>
#Ansible: sixgod
*/1 * * * * touch /a.txt

删除定时任务:[root@sixgod ansible]# ansible all -m cron -a "name='sixgod' user=root state=absent"
192.168.1.132 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}
查看:[root@sixgod ansible]# ansible all -m shell -a "crontab -l"
192.168.1.132 | SUCCESS | rc=0 >>

(7)yum模块

 

安装包,name后面直接写需要安装的名字。()

[root@sixgod ansible]# ansible all -m yum -a "name=lrzsz"

192.168.1.132 | SUCCESS => {
    "changed": false, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "lrzsz-0.12.20-27.1.el6.x86_64 providing lrzsz is already installed"
    ]
}

(8)在返回结果中使用远程主机的ip, 可以使用 {{inventory_hostname}}

[root@sixgod ansible]# ansible all -m shell -a "echo {{inventory_hostname}}"
192.168.1.132 | SUCCESS | rc=0 >>
192.168.1.132

 

inventory_hostname 表示客户端的IP

(9)setup模块

使用setup获取远程主机的详细信息,可使用filter过滤

如:获取远程主机的时间信息。

[root@sixgod ansible]# ansible all -m setup -a "filter=ansible_date_time"
192.168.1.132 | SUCCESS => {
    "ansible_facts": {
        "ansible_date_time": {
            "date": "2018-07-06", 
            "day": "06", 
            "epoch": "1530851394", 
            "hour": "12", 
            "iso8601": "2018-07-06T04:29:54Z", 
            "iso8601_basic": "20180706T122954227648", 
            "iso8601_basic_short": "20180706T122954", 
            "iso8601_micro": "2018-07-06T04:29:54.227813Z", 
            "minute": "29", 
            "month": "07", 
            "second": "54", 
            "time": "12:29:54", 
            "tz": "CST", 
            "tz_offset": "+0800", 
            "weekday": "星期五", 
            "weekday_number": "5", 
            "weeknumber": "27", 
            "year": "2018"
        }
    }, 
    "changed": false

}

(10)script模块

远程执行ansible本地脚本

[root@sixgod ansible]# cat a.sh 
ip=`ifconfig |grep -w "inet addr"|head -1`

echo $ip

 

[root@sixgod ansible]# ansible all -m script -a "./a.sh"
192.168.1.132 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.1.132 closed.\r\n", 
    "stdout": "inet addr:192.168.1.132 Bcast:192.168.1.255 Mask:255.255.255.0\r\n", 
    "stdout_lines": [
        "inet addr:192.168.1.132 Bcast:192.168.1.255 Mask:255.255.255.0"
    ]
}

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值