wewqeqw 发表于 2017-1-18 13:58:24

ansible安装配置和基本使用

一、ansible安装
ansible依赖于Python 2.6或更高的版本、paramiko、PyYAML及Jinja2。
1.1 编译安装
解决依赖关系

1
# yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto




解压安装包

1
2
# tar xf ansible-1.5.4.tar.gz
# cd ansible-1.5.4




编译安装

1
2
# python setup.py build
# python setup.py install




拷贝配置文件

1
2
# mkdir /etc/ansible
# cp -r examples/* /etc/ansible





1.2 rpm包安装
使用阿里云镜像源即可,这里为了方便使用,就直接使用yum安装了。

1
2
3
Fedora 用户可直接安装Ansible, 但RHEL或CentOS用户,需要 配置 EPEL
# yum install -y epel-release
# yum install -y ansible




注意:不同版本的ansible的功能差异可能较大。
二、配置
配置文件介绍:

配置文件:/etc/ansible/ansible.cfg
1
2
3
4
hostfile=/etc/ansible/hosts #指定默认hosts配置的位置
host_key_checking = False   #不进行host_key检查,省去目标key发生变化时输入(yes/no)的步骤
ask_pass=True               # 每次执行ansible命令是否询问ssh密码
ask_sudo_pass=True          # 每次执行ansible命令时是否询问sudo密码




主机清单:/etc/ansible/hosts
主程序:ansible、ansible paly-book、ansible-doc1.将要管理的主机纳入/etc/ansible/hosts配置文件中,可以填写IP或是主机名
1
2
3

10.10.10.3
10.10.10.4




2.基于ssh的方式与被管理的主机进行通信,在管理的主机上(部署ansible的主机上)生成一对非对称密钥,将公钥发给被管理的主机。(1)生成一对密钥:ssh-keygen -t rsa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# ssh-keygen -t rsa    # 默认存放的地方为/root/.ssh目录下
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):   
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
e8:60:67:4d:29:90:d2:4d:7d:52:2f:c7:e4:87:a4:80 root@wlm
The key's randomart image is:
+--[ RSA 2048]----+
|   ..+.o... o    |
|. o.E oo.B .   |
|   .. ooo * .|
|       =   o .   |
|    o + S      |
|   . =         |
|      .          |
|               |
|               |
+-----------------+




(2)将公钥发给要管理的主机:ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.10.10.3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.10.10.4
The authenticity of host '10.10.10.4 (10.10.10.4)' can't be established.
RSA key fingerprint is 43:8a:cc:2c:6a:07:0e:16:17:04:b5:dd:2c:4a:9a:41.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.10.10.4's password:
Permission denied, please try again.
root@10.10.10.4's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@10.10.10.4'"
and check to make sure that only the key(s) you wanted were added.




3.开始使用我们的第一条ansible命令,进行验证:
1
# ansible all -m ping





1
2
3
4
5
6
7
8
9
# ansible all -m ping
10.10.10.4 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
10.10.10.3 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}




命令介绍:

1
2
3
简单实用格式:ansible <host-pattern> [-m module_name] [-a args]
ansible all -m ping:
all 代表所有被管理的主机都进行操作-m ping 指定使用ping模块,ping通了返回的结果为pong




三、基本使用
1.常用命令


1
2
3
ansible-doc命令:获取模块列表,及模块使用格式;
ansible-doc -l :获取列表
ansible-doc -smodule_name :获取指定模块的使用信息





2.ansible 命令格式

1
ansible<host-pattern>[-f forks] [-m module_name][-a args]





<host-pattern>指明管控主机,以模式形式表示或者直接给定 IP ,必须事先定义在文件中; all 设置所有
[-f forks]指明每批管控多少主机,默认为 5 个主机一批次
[-m module_name]使用何种模块管理操作,所有的操作都需要通过模块来指定
[-a args]指明模块专用参数; args 一般为 key=value 格式注意:command模块的参数非为kv格式,而是直接给出要执行的命令即可;
注意: <host-pattern> 默认读取 /etc/ansible/hosts ,也可以指明自定义文件路径-iPATH, --inventory=PATH:指明使用的host inventory文件路径;3.常用模块介绍(module_name)
1)command模块:远程主机上运行命令
例如:

1
ansible WebServers -m command -a "ls /tmp"




command模块可以省略,此命令可以直接写为:

1
ansible WebServers -a "ls /tmp"





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# ansible WebServers -m command -a "ls /tmp"
# WebServers组里的所有主机执行ls /tmp操作
10.10.10.4 | SUCCESS | rc=0 >>
ansible_1DyyqF
keyring-e9DiYC
keyring-teKcdb
orbit-gdm
pulse-bcZcwuv9QjKR
pulse-qnyrpnncnwcb

10.10.10.3 | SUCCESS | rc=0 >>
ansible_itWaHf
systemd-private-6d81b379511c48219f27f97e16064052-colord.service-RK8yfy
systemd-private-6d81b379511c48219f27f97e16064052-cups.service-wm4x9x
systemd-private-6d81b379511c48219f27f97e16064052-httpd.service-UXyj9P
systemd-private-6d81b379511c48219f27f97e16064052-named.service-Nw7Sew
systemd-private-6d81b379511c48219f27f97e16064052-rtkit-daemon.service-QM4tRC
systemd-private-6d81b379511c48219f27f97e16064052-vmtoolsd.service-p58UCc




给远程主机添加用户,并设置密码:

1
2
3
ansible WebServers -a "useradd ansible1"
ansible WebServers -a "echo dtsdts | passwd --stdin ansible1"
# 直接这样使用不会成功,不支持管道,下面介绍shell模块





1
2
3
4
5
6
7
8
9
10
11
12
# ansible WebServers -a "useradd ansible1"
10.10.10.4 | SUCCESS | rc=0 >>


10.10.10.3 | SUCCESS | rc=0 >>

# ansible WebServers -a "echo dtsdts | passwd --stdin ansible1"
10.10.10.4 | SUCCESS | rc=0 >>
dtsdts | passwd -stdin ansible1    # 只显示了“dtsdts | passwd --stdin ansible1”内容

10.10.10.3 | SUCCESS | rc=0 >>
dtsdts | passwd -stdin ansible1




2)shell模块:远程主机在shell进程下运行命令,支持shell特性,当然也支持管道
给用户添加密码:


1
ansible WebServers -m shell -a "echo dtsdts | passwd --stdin ansible1"





1
2
3
4
5
6
7
8
# ansible WebServers -m shell -a "echo dtsdts | passwd --stdin ansible1"
10.10.10.4 | SUCCESS | rc=0 >>
更改用户 ansible1 的密码 。
passwd: 所有的身份验证令牌已经成功更新。

10.10.10.3 | SUCCESS | rc=0 >>
更改用户 ansible1 的密码 。
passwd:所有的身份验证令牌已经成功更新。




3) copy模块:把当前主机文件复制到远程主机位置,可以指定mode(权限)、own(所属主)、group(所属组)

1
2
ansible all -m copy -a "src=/tmp/abc.txt dest=/root/mode=644 owner=ansible1 group=root"
# src="本地主机文件" dest="远程主机文件"





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# ansible all -m copy -a "src=/tmp/abc.txt dest=/root/"
10.10.10.4 | SUCCESS => {
    "changed": true,
    "checksum": "325287cee456533bf76025312e5d05e842cb43a9",
    "dest": "/root/abc.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "1c6d47c6e4d59c630751b47fff140b89",
    "mode": "0644",
    "owner": "root",
    "size": 15,
    "src": "/root/.ansible/tmp/ansible-tmp-1484639082.19-114656107854348/source",
    "state": "file",
    "uid": 0
}
10.10.10.3 | SUCCESS => {
    "changed": true,
    "checksum": "325287cee456533bf76025312e5d05e842cb43a9",
    "dest": "/root/abc.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "1c6d47c6e4d59c630751b47fff140b89",
    "mode": "0644",
    "owner": "root",
    "size": 15,
    "src": "/root/.ansible/tmp/ansible-tmp-1484639082.46-26533455703361/source",
    "state": "file",
    "uid": 0
}




4)cron模块:在远程主机制定crontab周期性计划任务

1
2
3
minute=hour= day= month= weekday= job= name=(必须填写) state=
例如:
ansible all -m cron -a "minute=*/10 job='/sbin/ntpdate 10.10.10.10 &> /dev/null' name=Synctime"




在被管理的主机上使用crontab -l查看

1
2
3
# crontab -l
#Ansible: Synctime
*/10 * * * * /sbin/ntpdate 10.10.10.10 &> /dev/null




在管理(ansible)的主机上,可以删除制定的计划任务

1
ansible all -m cron -a "state=absent name=Synctime"    # name="name"





1
2
3
4
5
6
7
8
9
10
11
# ansible all -m cron -a "state=absent name=Synctime"
10.10.10.4 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": []
}
10.10.10.3 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": []
}




5)fetch模块:和copy相反,从远程主机拷贝文件到本地主机

1
2
3
4
5
ansible WebServers -m fetch -a “src=/root/abc.txt dest=/root/kel/ flat=yes”
src="远程主机文件" dest="本地主机" 可以不要flat=yes参数
flat=yes作用:
当dest=/root/kel/,abc.txt会保存在/root/kel/目录下
当dest=/root/kel,会拷贝abc.txt文件,并命名为kel




6)file模块:file模块它包含了文件、文件夹、超级链接类的创立、拷贝、移动、删除操作

1
2
3
4
5
6
7
8
9
10
11
12
ansible WebServers -m file -a
# 修改文件的所有组、人、权限。
path=/etc/foo.conf owner=foo group=foo mode=0644
# 操作链接的案例
src=/file/to/link/to dest=/path/to/symlink owner=foo group=foo state=link
#参数化案例
src=/tmp/{{ item.path }} dest={{ item.dest }} state=linkwith_items:
    - { path: 'x', dest: 'y' }    - { path: 'z', dest: 'k' }
# 使用touch来创建一个空文件并定义权限
path=/etc/foo.conf state=touch mode="u=rw,g=r,o=r"
# touch一个空文件,并且修改权限
path=/etc/foo.conf state=touch mode="u+rw,g-wx,o-rwx"




7)yum模块:用于yum安装包安装和卸载等操作

1
2
# ansible WebServers -m yum -a “name=httpd”
# 在远程主机上使用yum安装htpd服务




8)service模块:服务管理,就是service命令

1
2
3
4
5
6
7
8
9
ansible all -m service -a ""
# 不管当前什么情况,启动apache
name=httpd state=started
# 不管当前什么情况,停止apache
name=httpd state=stopped
# 不管当前什么情况,重启apache
name=httpd state=restarted
# 系统重启后,启动apache
name=httpd enabled=yes




9) user/group模块:user模块,用于管理用户;group模块,用于管理group

1
2
3
4
5
6
7
8
9
10
# ansible all -m user -a "name=test01 group=root"
# 添加一个test01用户,所属组为root

# ansible all -m user -a "name=test01 state=absent remove=yes"
# 删除test01用户

# ansible all -m group -a "name=testgrp01"
# 添加一个testgrp01的组
# ansible all -m group -a "name=testgrp01 state=absent"
# 删除组testgrp01




四、Playbooks 剧本
playbooks 是 ansible 更强大的配置管理组件,实现基于文本文件编排执行的多个任务,且多次重复执行。playbook 组织格式为使用YAML 语言来进行编写。
playbook是由一个或多个”play”组成的列表。play的主要功能在于将事先归为一组的主机装扮成事先通过ansible中的task定义好的角色。从根本上来将,所谓的task无非是调用ansible的一个module。将多个paly组织在一个playbook中,即可以让他们联通起来按事先编排的机制同唱一台大戏。

YAML是用来写配置文件的,接下来的配置文件都是以yaml为后缀。



页: [1]
查看完整版本: ansible安装配置和基本使用