ansible学习笔记1

来源:互联网 发布:冰点还原精灵类似软件 编辑:程序博客网 时间:2024/04/30 12:54

1 安装说明

运行环境:Ansible能够安装到 LinuxBSDMacOS X 等平台,Python版本最低要求为 2.6

管理节点运行环境依赖:

Python 2.6 或更高版本

Paramiko(Python的SSH模块)

PyYAML(Python的YAML解析器)

Jinja2(Python的模板引擎)

受控节点

如果是Python 2.4 或 Python 2.5 ,则需额外安装 Simplejson 模块。到Python的2.6或以上版本,就则内置了Simplejson模块,不需要额外安装任何其它依赖

 

2 Installingansible on CentOS 6.5

#yum install ansible

#whereis ansible

ansible: /usr/bin/ansible /etc/ansible/usr/share/ansible/usr/share/man/man1/ansible.1.gz

 

由于RHEL官网yum源还没有得到ansible的安装包支持,需要安装EPEL作为部署Ansibleyum

RHELCENTOS5版本:rpm -ivhhttp://mirrors.sohu.com/fedora-epel/5/x86_64/epel-release-5-4.noarch.rpm

RHELCENTOS6版本:rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm-

#yum install ansible

#whereis ansible

ansible:/usr/bin/ansible /etc/ansible/usr/share/ansible/usr/share/man/man1/ansible.1.gz

 

如果需要自定义module或者想阅读源码、使用最新版本,可以去github里下载源码

git clone https://github.com/ansible/ansible.gi

3 ssh密钥

#useradd ansible -s /bin/bash -m //服务端客户端都创建用户ansible

#su - ansible //服务端生成ssh-key并分发到所有客户端

$ssh-keygen -t rsa //一直回车键到完成

$ssh-copy-id -i ~/.ssh/id_rsa.pub ansible@192.168.0.10//也可scp等熟悉的方法拷贝过去

$cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys //服务器本机测试用

$ssh -p 2221 ansible@192.168.24.15 //测试ssh可否

4 修改配置文件

hosts文件配置的几种格式:

单纯的写主机名或ip,所属组为all

 

#blue.example.com 

#192.168.100.1 

定义一个组

#[webservers]

#alpha.example.org

#beta.example.org

#192.168.1.100

组成员可以使用通配符来匹配www[001:006].example.com

如果你没有使用公钥,想要使用密码,你也可以这样写(适用于第一次登陆控制)

格式:【主机名】【主机地址】【主机密码】默认是root用户来进行的

[keepalived]

keepalived1ansible_ssh_host=192.168.146.136 ansible_ssh_pass="test"

keepalived2ansible_ssh_host=192.168.146.137 ansible_ssh_pass="test"

 

5.测试ansible的使用

ansible all -m ping     //使用ping模块测试,以下显示成功,可以继续其他操作

127.0.0.1 | success >> {

 

  "changed": false,

 

 "ping": "pong"

}

192.168.0.10 | success >> {

 

  "changed": false,

 

 "ping": "pong"

6.模块测试

ansible命令

 

语法ansible <host-pattern> [options]

ansible <pattern_goes_here>-m<module_name> -a <arguments>

选项

-i 设备列表路径,可制定一些动态路径

-f 并行任务数

private-key 私钥路径

-m 模块名

-M 模块夹在路径

-a 参数

-k 登陆密码

-K sudo密码

-t 输出结果保存路径

-B 后台运行超时时间

-P 调查后台程序时间

-u 执行用户

-U sudo用户

-l 限制设备范围

-s是此用户sudo无需输入密码

 

2、体验命令操作

1)用户类操作

$ ansible webtest -m group-a"name=jjgame state=present" -s

$ ansible webtest -m user-a"name=utest groups=jjgame state=present" -s

$ ansible webtest -m user-a"name=utest state=absent remove=yes" -s

 

2)服务安装管理

$ ansible local -a 'sudo apt-get -yinstallnginx'

$ ansible local -a"sudo/etc/init.d/nginx stop"

$ ansible local -a"sudo/etc/init.d/nginx start"

$ ansible webtest -m service -a"name=nginx state=running" -s

3)其他测试过的

$ ansible datacenter -a 'ls -l /root' -s

$ ansible datacenter -a 'mv/root/old.tar.gz/root/new.tar.gz' -s

$ ansible datacenter -m file-a'path=/root/new.tar.gz' -s //查看文件属性

$ ansible datacenter -a 'rm -rf/root/directory'-s //删除目录

$ ansible all -m command -a"/bin/echohello" --ask-pass

$ ansible all -m ping --ask-pass -u root

$ ansible all -m script-a"/root/hequan/shell/t.sh" -k

$ ansible all -m copy-a"src=/root/hequan/shell/t.sh dest=/tmp/ mode=755 owner=rootgroup=root"-k -u root

$ ansible all -m file-a"dest=/tmp/t.sh mode=755 owner=root group=root" -k -u root

$ ansible all -i /etc/ansible/serverlist-mshell -a "/tmp/t.sh" -k -u root

$ ansible webtest -m get_url-a"url=http://192.168.0.8/sa/ sh.tar.gz dest=/tmp/" -s

$ ansible webtest -a "sudo tarzxvf/tmp/ sh.tar.gz -C /tmp" -s //不指定解压目录,会解压到/home/ansible目录下

$ ansible webtest -a"sudo/tmp/lansa_sh/t.sh"s

 

 

0 0
原创粉丝点击