playbook的核心元素:

    tasks: 任务

    variables: 变量

    templates: 模板

    handlers: 处理器

    roles: 角色

  变量:

    facts

    --extra-vars "name=value name=value"

    role定义

    Inventory中的变量:
      主机变量

        hostname name=value name=value
      组变量

        [groupname:vars]

        name=value

        name=value

  Inventory的高级用法:

 

Playbook:

  - host: 

    vars:

    remote_user:

    tasks:

      -

      -

      -

    variables

  - host: 

  - host:

 

  "ansible_distribution_major_version": "7",

  nginx.conf

    worker_processes cpu*core

    worker_processes {{ ansible_processor_cores * ansible_processor_count - 1 }};

  实战: 用ansible playbook完成配置keepalived的集群:

 

node1:

[root@node1 ~]# vim test.yml 
- hosts: websrvs
  remote_user: root
  tasks:
    - name: install nginx package
      yum: name=nginx state=latest
    - name: start nginx service
      service: name=nginx state=started enabled=yes
[root@node1 ~]# man ansible-playbook
[root@node1 ~]# ansible-playbook test.yml 
[root@node1 ~]# ansible websrvs -m shell -a 'ps aux | grep nginx'
172.16.100.69 | CHANGED | rc=0 >>
root      17250  0.0  0.1 120888  2092 ?        Ss   22:10   0:00 nginx: master process /usr/sbin/nginx
nginx     17251  0.0  0.1 121272  3128 ?        S    22:10   0:00 nginx: worker process
nginx     17252  0.0  0.1 121272  3128 ?        S    22:10   0:00 nginx: worker process
root      17320  0.0  0.0 113172  1212 pts/2    S+   22:12   0:00 /bin/sh -c ps aux | grep nginx
root      17322  0.0  0.0 112720   944 pts/2    S+   22:12   0:00 grep nginx
172.16.100.68 | CHANGED | rc=0 >>
root      17690  0.0  0.1 120888  2092 ?        Ss   22:10   0:00 nginx: master process /usr/sbin/nginx
nginx     17691  0.0  0.1 121272  3128 ?        S    22:10   0:00 nginx: worker process
nginx     17692  0.0  0.1 121272  3128 ?        S    22:10   0:00 nginx: worker process
root      17760  0.0  0.0 113172  1212 pts/2    S+   22:12   0:00 /bin/sh -c ps aux | grep nginx
root      17762  0.0  0.0 112720   944 pts/2    S+   22:12   0:00 grep nginx
172.16.100.6 | CHANGED | rc=0 >>
root      36569  0.0  0.1 108936  2164 ?        Ss   22:10   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     36570  0.0  0.1 109360  2844 ?        S    22:10   0:00 nginx: worker process                   
nginx     36571  0.0  0.1 109360  2892 ?        S    22:10   0:00 nginx: worker process                   
root      36623  0.0  0.0 106072  1272 pts/1    S+   22:12   0:00 /bin/sh -c ps aux | grep nginx
root      36625  0.0  0.0 103332   900 pts/1    S+   22:12   0:00 grep nginx
[root@node1 ~]# ansible-playbook test.yml 

node3:

[root@node3 ~]# ls /etc/nginx/nginx.conf
/etc/nginx/nginx.conf
[root@node3 ~]# cat /etc/nginx/nginx.conf
worker_processes auto;

node4:

[root@node4 ~]# cat /etc/nginx/nginx.conf
worker_processes auto;

node1:

[root@node1 ~]# vim /etc/ansible/hosts
[websrvs]
172.16.100.68
172.16.100.69

[dbsrvs]
172.16.100.68
172.16.100.6
[root@node1 ~]# scp 172.16.100.68:/etc/nginx/nginx.conf /tmp
[root@node1 ~]# vim /tmp/nginx.conf 
worker_processes 2;
[root@node1 ~]# vim test.yml 
- hosts: websrvs
  remote_user: root
  tasks:
    - name: install nginx package
      yum: name=nginx state=latest
    - name: copy conf file nginx.conf
      copy: src=/tmp/nginx.conf dest=/etc/nginx/nginx.conf
    - name: start nginx service
      service: name=nginx state=started enabled=yes
[root@node1 ~]# ansible-playbook test.yml 
[root@node1 ~]# ansible websrvs -m shell -a 'ps aux | grep nginx'
172.16.100.69 | CHANGED | rc=0 >>
root      17250  0.0  0.1 120888  2092 ?        Ss   22:10   0:00 nginx: master process /usr/sbin/nginx
nginx     17251  0.0  0.1 121272  3128 ?        S    22:10   0:00 nginx: worker process
nginx     17252  0.0  0.1 121272  3128 ?        S    22:10   0:00 nginx: worker process
root      17907  0.0  0.0 113172  1208 pts/2    S+   22:32   0:00 /bin/sh -c ps aux | grep nginx
root      17909  0.0  0.0 112720   940 pts/2    S+   22:32   0:00 grep nginx
172.16.100.68 | CHANGED | rc=0 >>
root      17690  0.0  0.1 120888  2092 ?        Ss   22:10   0:00 nginx: master process /usr/sbin/nginx
nginx     17691  0.0  0.1 121272  3128 ?        S    22:10   0:00 nginx: worker process
nginx     17692  0.0  0.1 121272  3128 ?        S    22:10   0:00 nginx: worker process
root      18355  0.0  0.0 113172  1208 pts/2    S+   22:32   0:00 /bin/sh -c ps aux | grep nginx
root      18357  0.0  0.0 112720   944 pts/2    S+   22:32   0:00 grep nginx
[root@node1 ~]# vim test.yml 
- hosts: websrvs
  remote_user: root
  tasks:
    - name: install nginx package
      yum: name=nginx state=latest
    - name: copy conf file nginx.conf
      copy: src=/tmp/nginx.conf dest=/etc/nginx/nginx.conf
      notify:
        - restart nginx service
    - name: start nginx service
      service: name=nginx state=started enabled=yes
  handlers:
    - name: restart nginx service
      service: name=nginx state=restarted
[root@node1 ~]# ansible-playbook test.yml 
[root@node1 ~]# vim /tmp/nginx.conf 
worker_processes 3;
[root@node1 ~]# ansible-playbook test.yml 
[root@node1 ~]# ansible websrvs -m shell -a 'ps aux | grep nginx'
172.16.100.68 | CHANGED | rc=0 >>
root      19002  0.0  0.1 120888  2240 ?        Ss   22:38   0:00 nginx: master process /usr/sbin/nginx
nginx     19003  0.0  0.1 121272  3132 ?        S    22:38   0:00 nginx: worker process
nginx     19004  0.0  0.1 121272  3132 ?        S    22:38   0:00 nginx: worker process
nginx     19005  0.0  0.1 121272  3132 ?        S    22:38   0:00 nginx: worker process
root      19073  0.0  0.0 113172  1212 pts/2    S+   22:39   0:00 /bin/sh -c ps aux | grep nginx
root      19075  0.0  0.0 112720   944 pts/2    S+   22:39   0:00 grep nginx
172.16.100.69 | CHANGED | rc=0 >>
root      18552  0.0  0.1 120888  2244 ?        Ss   22:38   0:00 nginx: master process /usr/sbin/nginx
nginx     18553  0.0  0.1 121272  3336 ?        S    22:38   0:00 nginx: worker process
nginx     18554  0.0  0.1 121272  3336 ?        S    22:38   0:00 nginx: worker process
nginx     18555  0.0  0.1 121272  3136 ?        S    22:38   0:00 nginx: worker process
root      18622  0.0  0.0 113172  1212 pts/2    S+   22:39   0:00 /bin/sh -c ps aux | grep nginx
root      18624  0.0  0.0 112720   944 pts/2    S+   22:39   0:00 grep nginx
[root@node1 ~]# ansible 172.16.100.67 -m setup
"ansible_distribution_major_version": "7",
[root@node1 ~]# ansible 172.16.100.6 -m setup | grep version
"ansible_distribution_major_version": "6",
[root@node1 ~]# vim test.yml 
- hosts: websrvs
  remote_user: root
  tasks:
    - name: install nginx package
      yum: name=nginx state=latest
    - name: copy conf file nginx.conf CentOS 7
      copy: src=/tmp/nginx.conf dest=/etc/nginx/nginx.conf
      when: ansible_distribution_major_version == "7"
      notify:
        - restart nginx service
    - name: copy conf file nginx.conf CentOS 6
      copy: src=/tmp/nginx.6.conf dest=/etc/nginx/nginx.conf
      when: ansible_distribution_major_version == "6"
      notify:
        - restart nginx service
    - name: start nginx service
      service: name=nginx state=started enabled=yes
  handlers:
    - name: restart nginx service
      service: name=nginx state=restarted 
[root@node1 ~]# scp 172.16.100.6:/etc/nginx/nginx.conf /tmp/nginx.6.conf
[root@node1 ~]# vim /tmp/nginx.6.conf 
worker_processes 2;
[root@node1 ~]# vim /etc/ansible/hosts
172.16.100.67

[websrvs]
172.16.100.68
172.16.100.69
172.16.100.6

[dbsrvs]
172.16.100.68
172.16.100.6
[root@node1 ~]# ansible-playbook test.yml 
[root@node1 ~]# vim useradd.yml
- hosts: websrvs
  remote_user: root
  tasks:
  - name: add some users
    user: name={{ item }} state=present
    with_items:
    - user1
    - user2
    - user3
[root@node1 ~]# ansible-playbook useradd.yml 
[root@node1 ~]# ansible 172.16.100.67 -m setup
[root@node1 ~]# ansible 172.16.100.67 -m setup
        "ansible_processor_cores": 1, 
        "ansible_processor_count": 2, 
[root@node1 ~]# ansible-doc -s template
[root@node1 ~]# cp /tmp/nginx.conf /tmp/nginx.conf.template
[root@node1 ~]# vim /tmp/nginx.conf.template 
worker_processes {{ ansible_processor_cores * ansible_processor_count }};
[root@node1 ~]# ansible websrvs -m template -a 'src=/tmp/nginx.conf.template dest=/tmp/nginx.conf'
172.16.100.68 | FAILED! => {
    "changed": false, 
    "msg": "AnsibleUndefinedVariable: 'ansible_processor_cores' is undefined"
}
172.16.100.69 | FAILED! => {
    "changed": false, 
    "msg": "AnsibleUndefinedVariable: 'ansible_processor_cores' is undefined"
}
172.16.100.6 | FAILED! => {
    "changed": false, 
    "msg": "AnsibleUndefinedVariable: 'ansible_processor_cores' is undefined"
}
[root@node1 ~]# vim tmp.yml
- hosts: websrvs
  remote_user: root
  tasks:
  - name: generate the conf file
    template: src=/tmp/nginx.conf.template dest=/tmp/nginx.conf
[root@node1 ~]# ansible-playbook tmp.yml 

node2:

[root@node2 ~]# grep worker_processes /tmp/nginx.conf 
worker_processes 2;

node1:

[root@node1 ~]# vim /tmp/nginx.conf.template 
listen       80 default_server;
[root@node1 ~]# vim /etc/ansible/hosts
172.16.100.67

[websrvs]
172.16.100.68 nginx_port=80
172.16.100.69 nginx_port=8080
172.16.100.6 nginx_port=808

[dbsrvs]
172.16.100.68
172.16.100.6
[root@node1 ~]# vim /tmp/nginx.conf.template 
listen       {{ nginx_port }} default_server;
[root@node1 ~]# ansible-playbook tmp.yml

node4:

[root@node4 ~]# grep listen /tmp/nginx.conf 
        listen       808 default_server;

node3:

[root@node3 ~]# grep listen /tmp/nginx.conf
        listen       8080 default_server;

node1:

[root@node1 ~]# vim test.yml 
[root@node1 ~]# vim vars.yml
- hosts: websrvs
  remote_user: root
  vars:
    package: httpd
  tasks:
  - name: install {{ package }} 
    yum: name={{ package }} state=present
[root@node1 ~]# ansible-playbook vars.yml 

node2:

[root@node2 ~]# rpm -q httpd
httpd-2.4.6-93.el7.centos.x86_64

node1:

[root@node1 ~]# vim vars.yml 
- hosts: websrvs
  remote_user: root
  vars:
    package: telnet-server
  tasks:
  - name: install package
    yum: name={{ package }} state=present
[root@node1 ~]# ansible-playbook vars.yml 
[root@node1 ~]# vim vars.yml 
- hosts: websrvs
  remote_user: root
  tasks:
  - name: install package
    yum: name={{ package }} state=present
[root@node1 ~]# ansible-playbook -e "package=tftp-server" vars.yml 
[root@node1 ~]# mkdir -pv ansible_playbooks/roles/{synctime,nginx,mariadb}/{files,templates,vars,tasks,handlers,meta}
[root@node1 ~]# tree ansible_playbooks/
ansible_playbooks/
└── roles
    ├── mariadb
    │   ├── files
    │   ├── handlers
    │   ├── meta
    │   ├── tasks
    │   ├── templates
    │   └── vars
    ├── nginx
    │   ├── files
    │   ├── handlers
    │   ├── meta
    │   ├── tasks
    │   ├── templates
    │   └── vars
    └── synctime
        ├── files
        ├── handlers
        ├── meta
        ├── tasks
        ├── templates
        └── vars

22 directories, 0 files
[root@node1 ~]# cd ansible_playbooks/
[root@node1 ansible_playbooks]# vim roles/synctime/tasks/main.yml
- name: sync time from ntp1.aliyun.com
  cron: name="sync time from ntp1.aliyun.com" minute="*/10" job="/usr/sbin/ntpdate ntp1.aliyun.com &> /dev/null"
[root@node1 ansible_playbooks]# scp 172.16.100.68:/etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.7.j2
[root@node1 ansible_playbooks]# scp 172.16.100.6:/etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.6.j2
[root@node1 ansible_playbooks]# vim roles/nginx/templates/nginx.conf.7.j2 
worker_processes {{ ansible_processor_cores * ansible_processor_count }};
[root@node1 ansible_playbooks]# vim roles/nginx/templates/nginx.conf.6.j2 
worker_processes {{ ansible_processor_cores * ansible_processor_count - 1 }};
[root@node1 ansible_playbooks]# vim roles/nginx/tasks/main.yml
- name: install nginx
  yum: name=nginx state=present
- name: generate main conf file for CentOS 7
  template: src=nginx.conf.7.j2 dest=/etc/nginx/nginx.conf
  when: ansible_distribution_major_version == "7"
  notify:
  - restart nginx
- name: generate main conf file for CentOS 6
  template: src=nginx.conf.6.j2 dest=/etc/nginx/nginx.conf
  when: ansible_distribution_major_version == "6"
  notify:
  - restart nginx
- name: start nginx service
  service: name=nginx state=started
[root@node1 ansible_playbooks]# vim roles/nginx/handlers/main.yml
- name: restart nginx
  service: name=nginx state=restarted
[root@node1 ansible_playbooks]# ansible websrvs -m service -a 'name=nginx state=stopped'
[root@node1 ansible_playbooks]# ansible websrvs -m yum -a 'name=nginx state=absent'
[root@node1 ansible_playbooks]# ansible websrvs -m shell -a 'rm -rf /etc/nginx'
[root@node1 ansible_playbooks]# tree roles/
roles/
├── mariadb
│   ├── files
│   ├── handlers
│   ├── meta
│   ├── tasks
│   ├── templates
│   └── vars
├── nginx
│   ├── files
│   ├── handlers
│   │   └── main.yml
│   ├── meta
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   │   ├── nginx.conf.6.j2
│   │   └── nginx.conf.7.j2
│   └── vars
└── synctime
    ├── files
    ├── handlers
    ├── meta
    ├── tasks
    │   └── main.yml
    ├── templates
    └── vars

21 directories, 5 files
[root@node1 ansible_playbooks]# vim nginx.yml
- hosts: websrvs
  remote_user: root
  roles:
  - synctime
  - nginx
[root@node1 ansible_playbooks]# ansible-playbook nginx.yml 
[root@node1 ansible_playbooks]# man ansible-playbook
[root@node1 ansible_playbooks]# cd
[root@node1 ~]# vim test.yml 
- hosts: websrvs
  remote_user: root
  tasks:
    - name: install nginx package
      yum: name=nginx state=latest
    - name: copy conf file nginx.conf CentOS 7
      copy: src=/tmp/nginx.conf dest=/etc/nginx/nginx.conf
      when: ansible_distribution_major_version == "7"
      tags: conf
      notify:
        - restart nginx service
    - name: copy conf file nginx.conf CentOS 6
      copy: src=/tmp/nginx.6.conf dest=/etc/nginx/nginx.conf
      when: ansible_distribution_major_version == "6"
      tags: conf
      notify:
        - restart nginx service
    - name: start nginx service
      service: name=nginx state=started enabled=yes
  handlers:
    - name: restart nginx service
      service: name=nginx state=restarted
[root@node1 ~]# ansible-playbook -t conf test.yml 
[root@node1 ~]# cd ansible_playbooks/
[root@node1 ansible_playbooks]# vim roles/nginx/tasks/main.yml 
- name: install nginx
  yum: name=nginx state=present
- name: generate main conf file for CentOS 7
  template: src=nginx.conf.7.j2 dest=/etc/nginx/nginx.conf
  when: ansible_distribution_major_version == "7"
  tags: conffile
  notify:
  - restart nginx
- name: generate main conf file for CentOS 6
  template: src=nginx.conf.6.j2 dest=/etc/nginx/nginx.conf
  when: ansible_distribution_major_version == "6"
  tags: conffile
  notify:
  - restart nginx
- name: start nginx service
  service: name=nginx state=started
[root@node1 ansible_playbooks]# ansible-playbook -t conffile nginx.yml 
[root@node1 ansible_playbooks]# ansible-playbook nginx.yml