playbook
    一系列模块任务的集合

    1. ---
    2. - hosts: webser
    3. remove_user: root
    4. tasks:
    5. - name: hello
    6. command: echo "hello"
    7. - name: create file
    8. file: name=/data/newfile state=touch
    9. - name: create user
    10. user: name=nobody shell=/sbin/nologin
    11. - name: install pakcage httpd
    12. yum: name=httpd state=laste
    13. # 同样的模块 不能写在一起
    14. - name: copy index.html
    15. copy: src=/data/index.html dest=/var/html
    16. - name: copy main.html
    17. copy: src=/data/main.html dest=/var/html

    playbook 执行的相对路径是 文件所在的路径
    ansible-playbook hello.yaml

    image.png
    handlers和notify
    当notify配置的任务发生改变时 会执行notify配置的任务(可以触发多个任务 列表形式)
    如: copy模块改动了配置文件 service就会被重启

    1. ---
    2. - hosts: webser
    3. remove_user: root
    4. tasks:
    5. - name: install httpd
    6. yum: name=httpd
    7. - name: copy conf file
    8. copy: src=files/httpd.conf dest=/etc/httpd/conf backup=yes
    9. notify: restart service
    10. - name: start service
    11. service: name=httpd state=started enabled=yes
    12. handlers:
    13. - name: restart service
    14. service: name=httpd state=restarted

    tags
    标签 可以通过tags关键字调用任务

    1. ---
    2. - hosts: webser
    3. remove_user: root
    4. tasks:
    5. - name: install httpd
    6. yum: name=httpd
    7. tags: installhttpd
    8. - name: copy conf file
    9. copy: src=files/httpd.conf dest=/etc/httpd/conf backup=yes
    10. notify: restart service
    11. - name: start service
    12. service: name=httpd state=started enabled=yes
    13. tags: starthttpd
    14. handlers:
    15. - name: restart service
    16. service: name=httpd state=restarted

    ansible-playbook -t starthttpd httpd.yaml //只执行 starthttpd 这一个标签的类容
    ansible-playboot —list-tags httpd.yaml
    多个任务可以共用一个标签

    playbook中的变量
    变量仅能由字母 数字 下划线组成 只能以字母开头
    变量来源:
    ansible setup facts // 系统状态信息
    ansible webser -m setup -a “filter=address“ //查询带address的变量名称

    palay脚本中定义变量

    1. - hosts: appser
    2. remote_user: root
    3. vars:
    4. - pk_name: httpd
    5. tasks:
    6. - name: install package
    7. yum: name={{ pk_name }}
    8. - name: start service
    9. service: name={{ pk_name }} state=started enabled=yes

    ansible-playbook -e “pk_name= httpd” app.yml // 对playbook变量 赋值运行
    playbook中 也可以对变量赋值

    在/etc/ansible/hosts中 也可以定义变量

    1. [webser]
    2. 192.168.200.147 pk_name=httpd
    3. 192.168.200.148 pk_name=vsftpd
    4. [webser:vars]
    5. hostname=webserver

    可以针对单台主机 或者主机组 进行变量设置

    主机变量优先级 高于 主机组组变量
    命令行变量优先级 高于 playbook中的变量
    playbook中的变量 高于 配置文件中变量

    剧本调用vars文件
    vars文件内容一下:

    1. ---
    2. pk_name: httpd
    1. ---
    2. - hosts: webser
    3. remote_user: root
    4. var_files:
    5. - vars.yaml
    6. tasks:
    7. - name: install package
    8. yum: name={{pk_name}}

    templates
    模板用于配置文件

    1. - hosts: nginxser
    2. remote_user: root
    3. tasks:
    4. - name: install package
    5. yum: name=nginx
    6. - name: copy template
    7. template: src=nginx.conf dest=/etc/nginx/nginx.conf
    8. notiyf: restart nginx
    9. - name: start nginx
    10. service: name=nginx state=started enable=yes
    11. handlers:
    12. - name: restart nginx
    13. service: name=nginx state=restart
    1. #nginx config
    2. #path templates/nginx.conf
    3. user nginx;
    4. worker_processes {{ ansible_processor_vcpus * 2 }}
    5. error_log /var/log/nginx/error.log;
    6. http{
    7. server{
    8. listen {{ http_port }}
    9. }
    10. }

    when
    条件判断

    1. ---
    2. - hosts: webser
    3. remote_user: root
    4. tasks:
    5. - name: install package
    6. yum: name=nginx
    7. - name: copy template for centos7
    8. template: src=ngin7.conf dest=/etc/nginx/conf/nginx.conf
    9. when: ansible_distribution_major_version == "7"
    10. - name: copy template for centos6
    11. template: src=nginx6.conf dest=/etc/nginx/conf/nginx.conf
    12. when: ansible_distribution_major_version == "6"
    13. - name: start nginx
    14. service: name=nginx state=started enable=yes

    with_items循环

    1. ---
    2. - hosts: webser
    3. remote_user: root
    4. tasks:
    5. - name: create some files
    6. file: name=/data/{{ item }} state= touch
    7. with_items:
    8. - file1
    9. - file2
    10. - file3
    11. - name: install some packages
    12. yum: name={{ item }}
    13. with_items:
    14. - httpd
    15. - vsftpd
    16. - hping3