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