返回顶部

ansible安装

 

yum install gcc-c++   -y

#安装第三方epel源  #centos6
[root@ansible ~]# rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm

[root@ansible ~]# yum install ansible


[root@ansible home]# ansible nginx -m copy -a "src=/home/qqq dest=/home/hosts owner=root group=root mode=0644"
192.168.1.17 | FAILED! => {
    "changed": false, 
    "checksum": "4e1243bd22c66e76c2ba9eddc1f91394e57f9f83", 
    "failed": true, 
    "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
}
#注意这个报错需要安装yum install -y libselinux-python (被监控的机器也要装)

ansible test -m service -a "name=mysqld state=stopped  enabled=yes"
ansible test -m service -a "name=mysqld state=restarted  enabled=yes"
ansible test -m service -a "name=mysqld state=running  enabled=yes"


#file模块常用
[root@ansible home]# ansible nginx -m file -a "src=/home/qqq dest=/tmp/qqq  state=link"  创建/home/qqq  软连接
[root@ansible home]# ansible nginx -a 'ls  /tmp/qqq -lh'
192.168.1.17 | SUCCESS | rc=0 >>
lrwxrwxrwx. 1 root root 9 Apr 16 04:58 /tmp/qqq -> /home/qqq

[root@ansible home]# ansible nginx -m file -a 'path=/tmp/qqq state=absent'  #删除文件
192.168.1.17 | SUCCESS => {
    "changed": false, 
    "path": "/tmp/qqq", 
    "state": "absent"
}


[root@ansible home]# ansible nginx  -m file -a 'path=/home/file1 state=touch'     #创建一个文件
[root@ansible home]# ansible nginx -m file -a "path=/home/file1  state=absent"   #删除文件
[root@ansible home]# ansible nginx -m file -a 'path=/home/d1 state=directory owner=root group=root mode=777'  #创建一个文件夹


#copy
[root@ansible home]# ansible nginx -m copy -a 'src=/home/qaz dest=/home/file2 mode=755 owner=root group=root'
[root@ansible home]# ansible nginx -m copy -a 'src=/home/qaz dest=/home/file2 mode=755 owner=root group=root backup=yes'  #文件有修改,就backup

#command
[root@ansible home]# ansible nginx -a 'creates=/home/file3 ls /home'    #先看/home/file3是否存在, 不存在执行ls /home


[root@ansible home]# ansible nginx  -a 'chdir=/home  tar zcf bb.tar.gz file2'   chdir切换到指定目录
[root@ansible home]# ansible nginx -a '/sbin/reboot'  #重启操作

#shell  支持管道 或者raw
[root@ansible home]# ansible-doc -s shell   #查看shell的使用方法
[root@ansible home]# ansible nginx -m shell -a 'ps -ef | grep mysqld'

#service 
[root@ansible home]# ansible nginx -m service -a 'name=mysqld state=started enabled=yes'   #开机自动启动enabled=yes

#cron
backup 与copy backup一样
cron_file 如果指定该选项,则用该文件替换远程主机上的cron.d目录下的用户任务计划

day
hour
minute
month
weekday
name:任务名
special_time :指定什么时候执行
state:确认任务计划是创建还是删除   absent删除
user  哪个用户

[root@ansible home]# ansible nginx -m cron -a 'name="reboot" hour=2 user=root job="/sbin/reboot"'     
[root@ansible home]# ansible nginx -m cron -a 'name="reboot" hour=2 user=root job="/sbin/reboot" state=absent'  删除这个计划任务
[root@ansible home]# ansible nginx -m cron -a 'name="check home directory" minute=*/3 job="ls -lh /home"'
[root@ansible home]# ansible nginx -m cron -a 'name="check home directory" special_time=reboot job="echo reboot"'  当重启时候,执行echo reboot

#filesystems 在块设备上创建文件系统

#yum 
选项:
config_file: yum的配置文件
disable_gpg_check 


#playbook

-hosts: mfs_node  #组
user:root
vars:  #变量
    
tasks: #执行任务表


handlers: #定义执行完tasks    要调用的任务


----------------------------------------------------
hosts: 定义远程的主机组
user: 执行该任务组的用户
remote_user: 与user相同
sudo: 如果设置为yes,执行该任务组的用户在执行任务的时候,获取root权限
sudo_user:如果你设置user为tom, sudo为yes, sudo_user为jerry, 则tom用户会获取jerry用户的权限
connection:通过什么方式连接到远程主机,默认为ssh
gather_facts:除非你明确说明不需要再远程主机上执行setup模块,否则默认会自动执行,如果你确实不需要setup模块所传递过来的变量,你可以启用该选项

-------------------------
变量部分:
vars
vars_files
vars_prompt #通过交互用到的值
setup
---------------
tasks:
  - name: install apache
    action: yum name=httpd state=installed #第一种方法
    
  - name: configure apache
    copy: src=file/httpd.conf dest=/etc/httpd/conf/httpd.conf #第二种方法
    
  - name: restart apache
    service:
      name: httpd
      state: restarted    #第三种方法      
----------------------------------
handlers 

 

posted on 2017-04-17 17:46  augustyang  阅读(229)  评论(0编辑  收藏  举报

导航