# --check | -C:只检测可能会发生的改变,但不真正执行操作
# --list-hosts:列出运行任务的主机
# --list-tags:列出playbook文件中定义的所有tags
# --list-tasks:列出playbook文件中定义的所有任务
# --limit:主机列表 只针对主机列表中的某个主机或者某个组执行
Playbook核心元素
• Hosts 执行的远程主机列表
• Tasks 任务集
• Varniables 内置变量或自定义变量在playbook中调用
• Templates 模板,即使用模板语法的文件,比如配置文件等
• Handlers 和notity结合使用,由特定条件触发的操作,满足条件方才执行,否则不执行
• tags 标签,指定某条任务执行,用于选择运行playbook中的部分代码。
准备工作
主机列表安装httpd软件
案例
---
- hosts: all
remote_user: root
vars:
- p1: t1133
- p2: t1132
- p3: t1131
tasks:
- name: copy file
copy:
src: /root/test/3.txt
dest: /root/
- name: copy http html
copy:
src: /var/www/html/index.html
dest: /var/www/html/index.html
when: ansible_hostname=="localhost"
- name: touch file2
file: name=/root/{{ p2 }} state=touch mode=755
when: ansible_hostname=="docker2"
- name: touch file3
file: name=/root/{{ p3 }} state=touch mode=755
when: ansible_hostname=="docker3"
- name: touch file1
file: name=/root/{{ p1 }} state=touch mode=755
when: ansible_hostname=="localhost"
- name: installed lsof tree
yum: name={{ item.name }} state=installed
with_items:
- { name: 'tree' }
- { name: 'lsof' }
- { name: 'httpd '}
- name: Config Httpd
template:
src: /root/anslibetest/templates/httpd.conf.jinja2
dest: /etc/httpd/conf/httpd.conf
tags:
- conf
notify:
- restart httpd
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
特殊颜色都是要学习的知识点 可百度案例
特殊颜色都是要学习的知识点 可百度案例
特殊颜色都是要学习的知识点 可百度案例
--- - hosts: all remote_user: root vars: - p1: t1133 - p2: t1132 - p3: t1131 tasks: - name: copy file copy: src: /root/test/3.txt dest: /root/ - name: copy http html copy: src: /var/www/html/index.html dest: /var/www/html/index.html when: ansible_hostname=="localhost" - name: touch file2 file: name=/root/{{ p2 }} state=touch mode=755 when: ansible_hostname=="docker2" - name: touch file3 file: name=/root/{{ p3 }} state=touch mode=755 when: ansible_hostname=="docker3" - name: touch file1 file: name=/root/{{ p1 }} state=touch mode=755 when: ansible_hostname=="localhost" - name: installed lsof tree yum: name={{ item.name }} state=installed with_items: - { name: 'tree' } - { name: 'lsof' } - { name: 'httpd '} - name: Config Httpd template: src: /root/anslibetest/templates/httpd.conf.jinja2 dest: /etc/httpd/conf/httpd.conf tags: - conf notify: - restart httpd handlers: - name: restart httpd service: name: httpd state: restarted