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

[经验分享] Ansible 七(ad hoc任务)

[复制链接]

尚未签到

发表于 2018-7-29 11:05:10 | 显示全部楼层 |阅读模式
  Ansible 七(ad hoc任务)
  ansible任务
  ad hoc任务就是执行shell命令、或shell脚本。
  ansible ad-hoc命令
  可以执行一些简单的命令,不需要将这些执行的命令特别保存下来。
  适合执行简单的命令。
  ansible playbook
  可以解决比较复杂的任务,可以将命令保存下来。
  适合执行配置管理或部署客户机。
  并行性和shell命令
  
  重启webservers主机组里的所以机器,每次重启10台
ansible webservers -a "/sbin/reboot" -f 10  以test01用户身份在webservers组的所以主机运行foo命令
ansible webservers -a "/usr/bin/foo" -u test01  以test01用户身份sudo执行foo(如果有sudo密码加上--ask-sudo-pass(-k))
ansible webservers -a "/usr/bin/foo" -u test01 --sudo [--ask-sudo-pass]  也可以sudo到其他用户执行命令非root
ansible webservers -a "/usr/bin/foo" -u username -U otheruser [--ask-sudo-pass]  前面命令用到的-f 10选项表示使用10个并行的进程。
  这个选项也可以在ansible的配置文件中设置,默认是5。
  如果主机数大于设置的并发数,ansible会自行协调,花的时间稍微长一点。
  ansible有许多模块,默认是命令模块:“command” ,这个模块不支持shell变量和管道等。
  我们可以通过-m选项来指定不同的模块。例如shell模块
  模块相关了解:http://www.ansible.com.cn/docs/modules.html
  
  使用shell模块在客户主机上执行命令
ansible webservers -m shell -a "echo $TERM"  #查看当前系统是linux  文件传输(file transfer)
  拷贝本地的/etc/hosts文件到webservers主机组所以主机的/tmp/hosts;
ansible webservers -m copy -a "src=/etc/hosts dest=/tmp/hosts"  若使用playbooks,则可以利用template模块来做到跟进一步的事情:(请参见 module 和 playbook 的文档)
  使用file模块修改文件的属主和权限(在这里可替换为copy模块是等效的)
ansible webservers -m file -a "dest=/tmp/hosts mode=600"  
ansible webservers -m file -a "dest=/tmp/hosts mode=600 owner=test01 group=test01"
  #修改远程客户机/tmp/hosts权限为600,属主和属组都为test01
  使用file模块创建目录,与执行mkdir -p 效果类似
ansible webservers -m file -a "dest=/tmp/abc mode=755 owner=test01 group=test01 state=directory"  #在webservers组里的所以主机上创建abc目录,权限为755,属主和属组都为test01
  使用file模块删除目录(递归的删除)和删除文件
ansible webservers -m file -a "dest=/tmp/abc state=absent"  管理软件包(managing packages)
  ansible提供对yum和apt的支持,这里是关于yum的示例。
  确认httpd软件包安装,不更新(如果已安装不更新)
ansible webservers -m yum -a "name=httpd state=present"  

  
返回如下:
  
1.1.1.2 | SUCCESS => {
  
    "changed": false,
  
    "msg": "",
  
    "rc": 0,
  
    "results": [
  
        "httpd-2.4.6-45.el7.centos.4.x86_64 providing httpd is already installed"
  
    ]
  
}
  
1.1.1.3 | SUCCESS => {
  
    "changed": false,
  
    "msg": "",
  
    "rc": 0,
  
    "results": [
  
        "httpd-2.4.6-45.el7.centos.4.x86_64 providing httpd is already installed"
  
    ]
  
}
  确认httpd软件包的版本
ansible webservers -m yum -a "name=httpd-2.4* state=present"  确认httpd软件包更新到最新版本
ansible webservers -m yum -a "name=httpd state=latest"  
返回如下:
  
1.1.1.2 | SUCCESS => {
  
    "changed": false,
  
    "msg": "",
  
    "rc": 0,
  
    "results": [
  
        "All packages providing httpd are up to date",
  
        ""
  
    ]
  
}
  
1.1.1.3 | SUCCESS => {
  
    "changed": false,
  
    "msg": "",
  
    "rc": 0,
  
    "results": [
  
        "All packages providing httpd are up to date",  #所有的软件包提供的服务器是最新的
  
        ""
  
    ]
  
}
  确认httpd软件包还没有安装(卸载httpd)
ansible webservers -m yum -a "name=httpd state=absent"  用户和组(user and groups)
  使用user模块可以方便的创建用户、删除用户、或管理现有的用户
  #创建用户test02
ansible all -m user -a "name=test02 password=123456789"  #删除用户test02
ansible all -m user -a "name=test02 state=absent"  源码部署
  直接使用 git 部署 webapp:
  ansible模块能够通知变更,当代码更新时,可以告诉ansible做一些特定的任务。
  比如从git部署代码然后重启apache服务等
ansible webservers -m git -a "repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD"  服务管理(managing services)
  安装httpd服务
ansible all -m yum -a "name=httpd state=present"  启动httpd服务
ansible all -m service -a "name=httpd state=started"  重启httpd服务
ansible all -m service -a "name=httpd state=restarted"  关闭httpd服务
ansible all -m service -a "name=httpd state=stopped"  卸载httpd服务
ansible webservers -m yum -a "name=httpd state=absent"  后台运行(需要长时间运行的命令Time Limited Background Operations)
  后台执行命令3600s,-B表示后台执行的时间
ansible all -B 3600 -a "/usr/bin/long_running_operation --do-stuff"  检查任务的状态
ansible all -m async_status -a "jid=123456789"  后台执行命令最大时间是1800s 即30 分钟,-P 每60s 检查下状态默认15s
ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff"  搜集系统信息(gathering facts)
  搜集主机的所以系统信息
ansible webservers -m setup  搜集系统信息并以主机名为文件名分别保存在/tmp/facts目录
ansible webservers -m setup --tree /tmp/facts  #搜集系统版本信息
ansible webservers -m setup -a "filter=ansible_distribution*"  搜集和内存相关的信息
ansible webservers -m setup -a "filter=ansible_*_mb"  搜集和cpu相关的信息
ansible webservers -m setup -a "filter=ansible_processor*"  搜集网卡信息
ansible webservers -m setup -a "filter=ansible_eth*"

运维网声明 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-542949-1-1.html 上篇帖子: Ansible 六(通配模式patterns) 下篇帖子: RHEL 6.6下安装ansible-HUNT1574
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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