ansible

ansible
git安装
git clone git://github.com/ansible/ansible.git --recursive
cd ./ansbile
source ./hacking/env-setup

rpm包安装
yum install ansbile

编译安装
yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto
tar xf ansible.tar.gz
cd ansbile
python setup.py build
python setup.py install
mkdir /etc/ansbile
cp -r examples/* /etc/ansible

yum install python-pip python-devel
yum -y install gcc glibc-devel zibl-devel rpm-bulid openssl-devel
pip install --upgrade pip
pip install ansible --upgrade
ansible --version 检查ansible是否安装

配置文件
/etc/ansible/hosts    #操作的主机列表
/etc/ansible/ansible.cfg    #主配置文件
/etc/ansible/roles    #存放playbook的目录

/usr/bin/ansbile    #主程序,执行命令的工具
/usr/bin/ansbile-doc     #查看模块的帮助信息
/usr/bin/ansbile-galaxy        #使用roles模块官网写好的playbook
/usr/bin/ansbile-playbook    #指定自定制的playbook
/usr/bin/ansbile-pull    #远程执行命令的工具
/usr/bin/ansbile-vault    #文件加密工具
/usr/bin/ansbile-console    #基于console界面与用户交互的执行工具


主机清单inventory
ansible的主要功能用于批量主机操作,为了便捷的使用其中的部分主机,尅在inventory中将主机分组
默认使用的主机清单是/etc/ansbile/hosts
inventory文件可以有多个,也可以通过Dynamic Inventory来动态生成

ansible配置文件(一般保持默认)
#inventory      = /etc/ansible/hosts    #主机清单文件
#library        = /usr/share/my_modules/    #库文件存放目录
#module_utils   = /usr/share/my_module_utils/
#remote_tmp     = ~/.ansible/tmp    #临时python命令文件存放的远程主机目录
#local_tmp      = ~/.ansible/tmp    #本地临时python命令目录
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks          = 5    #默认并发数
#poll_interval  = 15    #间隔时间
#sudo_user      = root    #默认sudo用户
#ask_sudo_pass = True    #每次执行ansible命令是否询问ssh密码
#ask_pass      = True
#transport      = smart
#remote_port    = 22
#module_lang    = C
#module_set_locale = False
#host_key_checking = False    #检查对应服务器的host_key,建议取消注释
#log_path = /var/log/ansible.log    #日志文件


echo export EDITOR=vim >> /etc/profile.d/env.sh #为vim添加颜色


ansible "web:&db" -m ping    #在组db中并且也在组web中的ip(逻辑与)
ansible 'web:!db' -m ping    #在组db中并且不在组web中的ip(逻辑非)
ansible 'web:db:&app:!ftp' -m ping    (综合逻辑)

ansible "web:&db" -m ping    
ansbile "~(web|db).*\.magedu\.com" -m ping (正则表达式)

ansible命令执行过程
1、加载默认的配置文件/etc/ansbile/ansbile.cfg
2、加载使用的模块
3、ansible生成临时的命令py文件,并将该文件发送到远程服务器对应的用户$HOME/.ansbile/tmp/ansible-tmp-xxx文件
4、文件授权+x
5、执行并返回结果
6、删除临时文件并退出

命令返回的状态
绿色:执行成功不需要做改变的操作
黄色:执行成功并对目标主机做更改操作
红色:执行失败


getent passwd root

常用模块
copy:复制本地文件到其它机器
ansible test  -m copy -a 'src=/root/test.sh dest=/tmp/test.sh owner=root mode=0655 backup=yes'    #如果目标存在,先备份再复制
ansible test -m copy -a "content='test page\n' dest=/tmp/test.txt"    #利用content生成test文件内容

fetch:从目标机器复制文件到本地
ansible test -m fetch -a 'src=/root/test.sh dest=/data/test'    #dest会生成目录

file:设置文件属性
ansible test -m file -a 'path=/root/test.sh owner=sys mode=0655'
ansible test -m file -a 'src=/tmp/data.txt dest=/tmp/data-link state=link' #创建软链接

hostname:管理主机名
ansible test -m hostname -a 'name=node1'

cron:计划任务
ansible test -m cron -a 'name="select dir" minute=* weekday=2,4,6 job="/usr/bin/wall FBI warning"' #设置报警
ansible test -m cron -a 'name="select dir" minute=* weekday=2,4,6 job="/usr/bin/wall FBI warning" disabled=true'    #禁止任务
ansible test -m cron -a 'name="select dir" state=absent'    #删除报警

yum:管理包
ansible test -m yum -a 'name=httpd'    #安装包
ansible test -m yum -a 'name=httpd state=latest' #安装最新的包
ansible test -m yum -a 'name=httpd state=absent'    #删除包

service:服务管理
ansible test -m service -a 'name=httpd state=started'    #启动服务
ansible test -m service -a 'name=httpd state=restarted'    #重启服务
ansible test -m service -a 'name=httpd state=stopped'    #停止服务
ansible test -m shell -a 'netstat -tunlp|grep httpd'    #查看服务

user:用户管理
ansible test -m user -a 'name=test1 comment="test user" group=root'    #创建用户并添加到root组
ansible test -m shell -a 'getent passwd test1'    #查看用户信息
ansible test -m user -a 'name=test2 comment="test user" system=yes'    #创建系统用户
ansible test -m user -a 'name=test1 state=absent'    #仅仅删除用户
ansible test -m user -a 'name=test2 state=absent remove=yes'    #删除用户以及家目录
 
group:组管理
ansible test -m group -a 'name=group_name system=yes'    #创建系统组
ansbile test -m group -a 'name=group_name state=absent'    #删除组

yaml官网
www.yaml.org


yaml语法
1、首行---:标记
2、次行:playbook内容,一般建议写明playbook的功能
3、注释使用#号开头
4、缩进必须是统一的,不能空格和tab混用
5、缩进的级别必须是一致的,同样的缩进表示同样的级别,程序通过缩进的级别判断配置级别
6、yaml文件内容和linux系统大小写判断方式保持一致,区分大小写,k/v的值均大小写敏感
7、k/v的值可以同行也可以换行写,同行使用:分隔
8、v可以是字符串,也可以是一个列表
9、一个完整的代码块最少需要包括name: task
10、一个name只能包括一个task
11、yaml文件扩展名通常为yml或yaml


如果写一个列表,开头都用'-'
字典:(同行写)
name: Example Developer
job: Developer
skil: Elite

换行写
{name: Example Developer,job: Developer,skil: Elite}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值