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