ansible基本使用


1. ansible的安装

  1. ansible的特性和优点
 
1.1 特性
  • no agent:不需要在被管控主机上安装客户端,ansible的客户端就是sshd服务
  • on server:无需服务端,使用时直接在命令行执行命令
  • modules in any languages:基于模块工作,模块可使用任意语言开发
  • yaml, not code:使用yaml格式定制playbook
  • ssh by default:基于ssh协议工作
  • strong mulit-tier soloution:可实现多级指挥
 
1.2 优点
  • 轻量级,无需安装agent
  • 批量任务执行可以写成脚本,而且不用分发到远程就可以执行
  • 支持sudo

 

2. 安装

[root@localhost ~]# yum install -y epel-release
[root@localhost ~]# yum install -y ansible

 

3. ansible的配置文件

ansible的安装目录是/etc/ansible

[root@localhost ansible]# ll
总用量 28
-rw-r--r--. 1 root root 19236 2月  10 21:25 ansible.cfg             //配置文件
-rw-r--r--. 1 root root  1031 2月  10 21:19 hosts                   //inventory,ansible需要连接的主机列表,可以填ip或者域名
drwxr-xr-x. 2 root root  4096 1月  30 04:15 roles

 

4. ansible的连接优化

  • 4.1 开启ssh长连接
ansible使用ssh协议和被管控主机通信,开启长连接后会有一个established的连接
openssh5.6以后的版本支持了multiplexing,如果管控机命令行执行ssh -V得到的版本号大于5.6就可以设置长连接
[root@localhost ansible]# vim ansible.cfg
[ssh_connection]
echo "ssh_args = -C -o ControlMaster=auto -o ControlPersist=5d" >> /etc/ansible/ansible.cfg

ControlPersist=5d 这个参数是设置保持长连接的时间

 

4.2 取消ssh第一次登陆的交互

[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# cat config
UserKnownHostsFile /dev/null
ConnectTimeout 15
StrictHostKeyChecking no

如果是命令行登陆,取消第一次交互:ssh -o StrictHostKeyChecking=no 192.168.123.107

 

4.3 去除ansible第一次执行命令而没有known_hosts文件报错限制

第一次执行ansible命令,但是管控机从来没有登录过被管控机,会报如下错误

[root@localhost ~]# ansible all -i test.txt -m ping -k
SSH password:
192.168.123.107 | FAILED! => {
    "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."
}

 

解决方法:

[root@localhost ~]# vim /etc/ansible/ansible.cfg
.......
# uncomment this to disable SSH key host checking
host_key_checking = False  

 

 

ansible的模块

大部分模块以前都学过,不再重复做笔记,只补充以前没学过的
 
ansible执行命令的格式:ansible ip/group -m module -a arg
 
1. ping模块
[root@localhost ansible]# ansible all -m ping
192.168.123.107 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

 

2. 使用指定的IP列表文件

[root@localhost ~]# cat test.txt
192.168.123.107

[root@localhost ~]# ansible all -i test.txt -m ping      //如果自定义的文件没有分组,调用文件时必须写上all              
192.168.123.107 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

 

3. ansible使用密码登陆被管控主机

ansible执行命令前,管控机需要配置密钥登陆被管控机,使用密码则省去了这一步
有两种方式实现密码登陆,第一种是ansible -k 选项
[root@localhost ~]# ansible -h |grep ask-pass
    -k, --ask-pass      ask for connection password

[root@localhost ~]# ansible all -i test.txt -m ping -k
SSH password:
192.168.123.107 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

 

第二种是在inventory里,ip 后面直接把密码写上

[root@localhost ~]# cat test.txt
192.168.123.107 ansible_ssh_pass=123456                         //直接在ip后面写上密码

[root@localhost ~]# ansible all -i test.txt -m ping
192.168.123.107 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

 

4. 收集被管控机的信息(类似saltstack的grains)

[root@localhost ~]# ansible all -i test.txt -m setup             //调用setup模块

[root@localhost ~]# ansible all -i test.txt -m setup -a 'filter=ansible_default_ipv4'             //使用filter关键字对setup的信息过滤提取
192.168.123.107 | SUCCESS => {
    "ansible_facts": {
        "ansible_default_ipv4": {
            "address": "192.168.123.107",
            "alias": "ens33",
            "broadcast": "192.168.123.255",
            "gateway": "192.168.123.1",
            "interface": "ens33",
            "macaddress": "00:0c:29:66:54:78",
            "mtu": 1500,
            "netmask": "255.255.255.0",
            "network": "192.168.123.0",
            "type": "ether"
        }
    },
    "changed": false
}

 

5. 从inventory中取ip地址

[root@localhost ~]# ansible all -i test.txt -m shell -a 'echo {{ inventory_hostname}}'       //inventory_hostname是一个内置变量,返回当前的inventory
192.168.123.107 | SUCCESS | rc=0 >>
192.168.123.107

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM