http://lixcto.blog.51cto.com/4834175/1431247
http://lixcto.blog.51cto.com/4834175/1431659
http://rfyiamcool.blog.51cto.com/1030776/1413031
http://rfyiamcool.blog.51cto.com/1030776/1413387
http://guoting.blog.51cto.com/8886857/1553451
http://edu.51cto.com/course/course_id-2032.html
這裏有燦哥的視頻。
磊哥寫的有些複雜,還沒看懂,
[root@ansible]# cat /etc/ansible/hosts
[webserver]
centos5 ansible_ssh_host=192.168.1.215 ansible_ssh_user=root
# hosts文件這裏沒有設置密碼,所以運行的時候帶了 -k 參數 輸入密碼,可以設置密碼,還可以使用ssh無密碼登陸方式。
[root@ansible]# ansible centos5 -m ping -k
SSH password:
centos5 | success >> {
"changed": false,
"ping": "pong"
}
[root@ansible]# cat testuser.yaml
- name: create user
hosts: centos5
remote_user: root
gather_facts: false
vars:
- user: "billy"
tasks:
- name: create ` user `
user: name=` user `
#新建一個用戶billy
[root@ansible] # ansible-playbook testuser.yaml -k
SSH password:
PLAY [create user] ************************************************************
TASK: [create billy] **********************************************************
changed: [centos5]
PLAY RECAP ********************************************************************
centos5 : ok=1 changed=1 unreachable=0 failed=0
[root@centos5]# id billy
uid=502(billy) gid=502(billy) groups=502(billy)
# 安裝nginx並開啓服務
[root@ansible]# cat nginx.yaml
- name: install and start nginx
hosts: centos5
user: root
tasks:
- name: install nginx
action: yum name=nginx state=latest
- name: start nginx
service: name=nginx state=running
[root@ansible]# ansible-playbook nginx.yaml -k
SSH password:
PLAY [install and start nginx] ************************************************
GATHERING FACTS ***************************************************************
ok: [centos5]
TASK: [install nginx] *********************************************************
ok: [centos5]
TASK: [start nginx] ***********************************************************
changed: [centos5]
PLAY RECAP ********************************************************************
centos5 : ok=3 changed=1 unreachable=0 failed=0