设为首页 收藏本站
查看: 1748|回复: 0

[经验分享] Ansible学习之基础(2)

[复制链接]

尚未签到

发表于 2015-11-26 10:40:11 | 显示全部楼层 |阅读模式
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
[defaults]
host_key_checking = False

3.配置inventory
3.1 hosts
在inventory中的/etc/ansible/hosts是INI格式的文件
vi /etc/ansible/hosts
mail.example.com
[webservers]  ###中括号中是group名
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com ###机器名一个机器可以属于不同的group
我们还可以指定ip,port,以及连接用户,密码:
[test]

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范围:[webservers]
www[01:50].example.com[databases]
db-[a:f].example.com[targets]
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变量:[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909group变量:[atlanta]
host1
host2
[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
groups和hosts都可以作为变量,可以成为其他group的一部分[atlanta]
host1
host2
[raleigh]
host2
host3
[southeast:children]
atlanta
raleigh
[southeast:vars]
some_server=foo.southeast.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2
[usa:children]
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 &quot;name=httpd state=restarted&quot;主机参数的配置方法有如下几种

所有的主机
all*
指定的主机组
webserverswebservers:dbservers
排除指定的主机组
webservers:!phoenix
指定同时存在于两个组的主机
webservers:&staging
复合条件
webservers:dbservers:&staging:!phoenix
采用表达式指定主机
one*.com:dbservers~(web|db).*\.example\.com

5.执行命令
执行Shell命令
重启主机

ansible all -a &quot;/sbin/reboot&quot; -f 10
使用不同的用户执行命令

ansible all -a &quot;/usr/bin/foo&quot; -u demo
使用Shell模块

ansible all -m shell -a 'echo $TERM'
文件操作模块
下发文件

ansible all -m copy -a &quot;src=/etc/hosts dest=/tmp/hosts&quot;
下发文件后指定用户及权限

ansible all -m file -a &quot;dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan&quot;
创建文件夹

ansible all -m file -a &quot;dest=/path/to/c mode=644 owner=mdehaan group=mdehaan state=directory&quot;
删除文件

ansible all -m file -a &quot;dest=/path/to/c state=absent&quot;
包管理模块
安装包

ansible webservers -m yum -a &quot;name=acme state=installed&quot;
安装指定版本的安装包

ansible webservers -m yum -a &quot;name=acme-1.5 state=installed&quot;
安装最新版本的安装包

ansible webservers -m yum -a &quot;name=acme state=latest&quot;
卸载安装包

ansible webservers -m yum -a &quot;name=acme state=removed&quot;
用户模块
新增用户

ansible all -m user -a &quot;name=foo password=<crypted password here>&quot;
删除用户

ansible all -m user -a &quot;name=foo state=absent&quot;
版本管理工具模块
ansible webservers -m git -a &quot;repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD&quot;
服务管理模块
启动服务

ansible webservers -m service -a &quot;name=httpd state=started&quot;
重启服务

ansible webservers -m service -a &quot;name=httpd state=restarted&quot;
停止服务
ansible webservers -m service -a &quot;name=httpd state=stopped&quot;
后台管理模块
启动一个执行3600秒的后台服务

ansible all -B 3600 -a &quot;/usr/bin/long_running_operation --do-stuff&quot;
检查作业状态
ansible all -m async_status
-a “jid=1311″

后台运行并定期检查

ansible all -B 1800 -P 60 -a &quot;/usr/bin/long_running_operation --do-stuff&quot;  #后台运行1800秒,每60秒检查一次
资产盘点模块
盘点资产

ansible all -m setup

参考:http://www.kiratechblog.com/?p=420http://docs.ansible.com

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-143784-1-1.html 上篇帖子: Ansible(14)wait_for模块 下篇帖子: Ansible学习之基础(1)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表