坏气十足 发表于 2018-7-29 11:05:10

Ansible 七(ad hoc任务)

  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]
查看完整版本: Ansible 七(ad hoc任务)