Ansible学习之基础(2)
1. 安装apt-get install python-pip
apt-get install ansible
apt-get install sshpass
test:
$ echo "127.0.0.1" > ~/ansible_hosts
$ export ANSIBLE_HOSTS=~/ansible_hosts
$ ansible all -m ping --ask-pass
2.SSH连接配置
ansible默认使用ssh连接远程机。
2.1使用password
--ask-pass 交互式的输入password
--ask-sudo-pass 输入sudo的password
2.2使用keys登录
$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa
例子:
# as bruce
$ ansible all -m ping -u bruce
# as bruce, sudoing to root
$ ansible all -m ping -u bruce --sudo
# as bruce, sudoing to batman
$ ansible all -m ping -u bruce --sudo --sudo-user batman
tips:当远程机器被rebuild以后, ‘known_hosts’里面会存在不同的key,导致ssh连接失败。在ansible里面设置host_key_checking可以防止这个错误
编辑etc/ansible/ansible.cfg 或者 ~/.ansible.cfg
host_key_checking = False
3.配置inventory
3.1 hosts
在inventory中的/etc/ansible/hosts是INI格式的文件
vi /etc/ansible/hosts
mail.example.com
###中括号中是group名
foo.example.com
bar.example.com
one.example.com
two.example.com
three.example.com ###机器名一个机器可以属于不同的group
我们还可以指定ip,port,以及连接用户,密码:
10.79.43.72 ansible_ssh_user=root ansible_ssh_pass=cisco123
jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50定义host范围:
www.example.com
db-.example.com
localhost ansible_connection=local
other1.example.com ansible_connection=ssh ansible_ssh_user=mpdehaan
other2.example.com ansible_connection=ssh ansible_ssh_user=mdehaan
3.2 定义变量注意:hosts和groups变量只能被playbook使用,不能被ansible直接调用host变量:
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909group变量:
host1
host2
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
groups和hosts都可以作为变量,可以成为其他group的一部分
host1
host2
host2
host3
atlanta
raleigh
some_server=foo.southeast.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2
southeast
northeast
southwest
northwest为了管理方便,通常不会将host和groups定义在inventory主文件中/etc/ansible/hosts,而是定义在独立的YAML文件中例如:inventory主目录文件为:/etc/ansible/hosts有一个host为foosball,有两个group,可以定义如下:/etc/ansible/group_vars/raleigh
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosballgourp raleigh内容如下:vi /etc/ansible/group_vars/raleigh---
ntp_server: acme.example.org
database_server: storage.example.org
还可以定义多级目录:/etc/ansible/group_vars/raleigh/db_settings
/etc/ansible/group_vars/raleigh/cluster_settings注意:如果playbook和inventory中都定义了groups或者hosts,playbook将覆盖inventory中的定义。可以编写inventory脚本支持从openstack nova自动的获取相应的清单,实现自动化。
Inventory一些常用变量
ansible_ssh_host:远程主机
ansible_ssh_port:ssh连接端口
ansible_ssh_user:ssh连接用户
ansible_ssh_pass:ssh连接密码
ansible_connection:ssh连接方式
ansible_python_interpreter:目标主机python路径
ansible\_\*\_interpreter:Ruby或者Perl或者其他环境的路径
4.指定ansible运行目标Ansible命令的基本语法格式如下:
ansible <pattern_goes_here> -m <module_name> -a <arguments>例如:ansible webservers -m service -a "name=httpd state=restarted"主机参数的配置方法有如下几种
所有的主机
all*
指定的主机组
webserverswebservers:dbservers
排除指定的主机组
webservers:!phoenix
指定同时存在于两个组的主机
webservers:&staging
复合条件
webservers:dbservers:&staging:!phoenix
采用表达式指定主机
one*.com:dbservers~(web|db).*\.example\.com
5.执行命令
执行Shell命令
重启主机
ansible all -a "/sbin/reboot" -f 10
使用不同的用户执行命令
ansible all -a "/usr/bin/foo" -u demo
使用Shell模块
ansible all -m shell -a 'echo $TERM'
文件操作模块
下发文件
ansible all -m copy -a "src=/etc/hosts dest=/tmp/hosts"
下发文件后指定用户及权限
ansible all -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"
创建文件夹
ansible all -m file -a "dest=/path/to/c mode=644 owner=mdehaan group=mdehaan state=directory"
删除文件
ansible all -m file -a "dest=/path/to/c state=absent"
包管理模块
安装包
ansible webservers -m yum -a "name=acme state=installed"
安装指定版本的安装包
ansible webservers -m yum -a "name=acme-1.5 state=installed"
安装最新版本的安装包
ansible webservers -m yum -a "name=acme state=latest"
卸载安装包
ansible webservers -m yum -a "name=acme state=removed"
用户模块
新增用户
ansible all -m user -a "name=foo password=<crypted password here>"
删除用户
ansible all -m user -a "name=foo state=absent"
版本管理工具模块
ansible webservers -m git -a "repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD"
服务管理模块
启动服务
ansible webservers -m service -a "name=httpd state=started"
重启服务
ansible webservers -m service -a "name=httpd state=restarted"
停止服务
ansible webservers -m service -a "name=httpd state=stopped"
后台管理模块
启动一个执行3600秒的后台服务
ansible all -B 3600 -a "/usr/bin/long_running_operation --do-stuff"
检查作业状态
ansible all -m async_status
-a “jid=1311″
后台运行并定期检查
ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff"#后台运行1800秒,每60秒检查一次
资产盘点模块
盘点资产
ansible all -m setup
参考:http://www.kiratechblog.com/?p=420http://docs.ansible.com
页:
[1]