ansible

1 安装

安装比较简单 配置yum源进行安装

yum -y install ansible

使用ping模块来检测主机的连通性  分别对单主机及组进行ping操作。

需要控制端 与 被控制端机器配置 公钥认证,否则需要添加-k 参数 输密码  ansible testserver -m  ping -k

[root@jinkangldap ~]# cat  /etc/ansible/hosts
[testserver]
192.168.16.105
127.0.0.1


[root@jinkangldap ~]# ansible testserver -m  ping 
127.0.0.1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.16.105 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

如何进行目标匹配

ansible testserver -m service -a "name=nginx  state=restarted"
组名 模块名 参数
ansible testserver -m command -a "uptime"

模块的介绍

1  远程命令的模块

 模块包括 command  script  shell 都可以实现远程 shell命令运行

command 作为 ansible 的默认模块 可以运行远程权限范围所有的shell命令 

script  功能是在远程主机上 执行主控端存储的shell脚本 相当于scp + shell组合

shell  功能是执行远程主机的shell文件。

ansible testserver -m shell   -a "/home/test.sh"
ansible testserver -m script -a  "/home/test.sh"

copy 模块  实现主控端向目标主机拷贝文件  类似于scp 的功能。

ansible testserver -m  copy   -a "src=/home/test.sh dest=/tmp"

stat 模块 获取远程文件状态信息  包括 atime ctime  mtime  md5  uid  gid 等

ansible testserver -m stat   -a "path=/home/test.sh"

get_url 模块

实现在远程主机下载 指定的URL 到本地,支持sha256sum校验。

ansible testserver -m get_url   -a "url=https://www.baidu.com/favicon.ico dest=/tmp/test.ico "

yum 模块

ansible testserver -m  yum   -a "name=vim state=latest"

cron 模块

ansible testserver -m cron   -a "name='echo info' minute=* job='echo hello >> /home/log 2>&1'"

 取消定时

ansible testserver -m cron   -a "name='echo info' minute=* job='echo hello >> /home/log 2>&1' state=absent"
[root@jinkangldap home]# ansible testserver -m cron   -a "name='echo info' minute=* job='echo hello >> /home/log 2>&1'" 
192.168.16.105 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "echo info"
    ]
}
[root@jinkangldap home]# ansible testserver  -a "crontab -l"
192.168.16.105 | SUCCESS | rc=0 >>
#Ansible: echo info
* * * * * echo hello >> /home/log 2>&1

[root@jinkangldap home]# ansible testserver -m cron   -a "name='echo info' minute=* job='echo hello >> /home/log 2>&1' state=absent"
192.168.16.105 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}
[root@jinkangldap home]# ansible testserver  -a "crontab -l"
192.168.16.105 | SUCCESS | rc=0 >>

 service 模块

ansible testserver -m  service -a 'name=nginx state=restarted'

还有一些其他 参数

enabled 设置开机自启动  state 有四种状态 started 启动服务 stopped 停止服务 restarted 重启服务 reloaded 重载服务。

setup模块

该模块主要用于收集信息,是通过调用facts组件来实现的。
  facts组件是Ansible用于采集被管机器设备信息的一个功能,我们可以使用setup模块查机器的所有facts信息,可以使用filter来查看指定信息。整个facts信息被包装在一个JSON格式的数据结构中,ansible_facts是最上层的值。
  facts就是变量,内建变量 。每个主机的各种信息,cpu颗数、内存大小等。会存在facts中的某个变量中。调用后返回很多对应主机的信息,在后面的操作中可以根据不同的信息来做不同的操作。如redhat系列用yum安装,而debian系列用apt来安装软件。

[root@jinkangldap home]# ansible testserver -m  setup -a 'filter="*mem*"'
192.168.16.105 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 732, 
        "ansible_memory_mb": {
            "nocache": {
                "free": 2102, 
                "used": 1535
            }, 
            "real": {
                "free": 732, 
                "total": 3637, 
                "used": 2905
            }, 
            "swap": {
                "cached": 0, 
                "free": 3839, 
                "total": 3839, 
                "used": 0
            }
        }, 
        "ansible_memtotal_mb": 3637
    }, 
    "changed": false
}

也可以把setup信息保存到主机上,通过主机上的文件查看系统信息

ansible testserver -m  setup -a 'filter="*mem*"' --tree /tmp/facts

[root@jinkangldap home]# ls /tmp/facts/
192.168.16.105

[root@jinkangldap home]# cat /tmp/facts/192.168.16.105
{"ansible_facts": {"ansible_memfree_mb": 817, "ansible_memory_mb": {"nocache": {"free": 2188, "used": 1449}, "real": {"free": 817, "total": 3637, "used": 2820}, "swap": {"cached": 0, "free": 3839, "total": 3839, "used": 0}}, "ansible_memtotal_mb": 3637}, "changed": false}

原文地址:https://www.cnblogs.com/jkklearn/p/11885524.html