ansible的playbook劇本

若是用模塊形式通常有冪等性,若是用shell或者command沒有冪等性
playbooks至關於shell的腳本,能夠吧執行的任務寫到文件當一次執行,方便調用
tasks:一個task至關因而一個play
varobles:變量,一個定義,屢次調用
template:模板。能夠區分不一樣主機的特色
handlers:觸發器,依賴於前一個任務,前一個任務若是執行改變,那麼就會觸發handlers
1.vim /etc/ansible/hosts
[zxw]
192.168.126.7 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=123
192.168.126.6 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=123
~shell

基本格式vim

- hosts: zxw
remote_user: root
tasks:
- name: copy httpd.conf
copy: src=name dest=name
- name: restarted httpd
service: name=httpd state=restartedssh

Vim name.yaml
- hosts: zxw 定義的組 (IP,all,組名)
remote_user: root 主機執行
tasks: 任務
- name: copy httpd.conf 任務名字
copy: src=/etc/httpd/conf/httpd.conf 模塊 本地文件 dest=/etc/httpd/conf/httpd.conf 目標文件
- name: restarted httpd
service: name=httpd state=restarted
[root@zxw8 ~]# ansible-playbook host.yamlspa

192.168.126.7 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
執行3個| 修給2個| 不可達=0 | 失敗=0| 跳過=0| 獲救=0| 忽略=0

定義變量:
第一種:
- hosts: zxw
remote_user: root
vars:
- file: /etc/httpd/conf/httpd.conf
tasks:
- name: copy httpd.conf
copy: src={{ file }} dest={{ file }}
- name: restarted httpd
service: name=httpd state=restarted
第二種:
vim /etc/ansible/hosts
[zxw:vars]
file= /etc/httpd/conf/httpd.conf
packages=tree2
第三種:
[root@zxw8 ~]# ansible-playbook host.yaml --extra-vars "file=/etc/httpd/conf/httpd.conf "3d

註冊變量:
register註冊變量:把date命令輸出的結果賦予給date_outputrest

- hosts: zxw
remote_user: root
tasks:
- name: get date
command: date
register: date_output
- name: echo date_output
shell: "echo {{date_output.stdout}} > /root/zxw.txt"
[root@zxw8 ~]# ansible-playbook date.yamlblog

when語句:
when條件語句:能夠根據setup顯示出客戶端信息爲依據來判斷
[root@zxw8 ~]# ansible zxw -m setup | more
列出客戶端詳細信息
- hosts: zxw
remote_user: root
tasks:
- name: mkdir when
command: 'mkdir when'
when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"ip

異常處理:
ignore_errors:若是任務出錯,直接跳過,不會影響其餘任務rem

- hosts: zxw
remote_user: root
tasks:
- name: touch zhao
command: 'touch zhao'
ignore_errors: yes
- name: restarted httpd
service: name=httpd state=restarted
ignore_errors: yesrpc

循環語句:
第一種:
{{ item }}:循環建立
建立
- hosts: zxw
remote_user: root
tasks:
- name: add many users
user: name={{ item }} state=present
with_items:
- user1
- user2
- user3
刪除:
user: name={{ item }} state=absent
- hosts: zxw
remote_user: root
tasks:
- name: yum many rujian
yum: name={{ item }} state=latest
with_items:
- samba
- vsftpd
- nfs-utils
- rpcbind


第二種:
- hosts: 192.168.254.12
remote_user: root
tasks:
- name: add several user
user: name={{item.name}} state=present groups={{item.groups}}
with_items:
- { name: 'testuser1', groups: 'wheel'}
- { name: 'testuser2', groups: 'root'}
觸發器:
handlers:若是執行的任務被改變那麼會觸發handlers的任務
- hosts: zxw
remote_user: root
tasks:
- name: copy httpd.conf
copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- service httpd restarted
handlers:
- name: service httpd restarted
service: name=httpd state=restarted

模板拷貝:
template,用來區分不一樣客戶端上的特性
- hosts: zxw
remote_user: root
tasks:
- name: copy httpd.conf
template: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- service httpd restarted
handlers:
- name: service httpd restarted
service: name=httpd state=restarted
[root@zxw8 ~]]# cat /etc/ansible/hosts
[zxw]
192.168.126.7 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=123 prot=80
192.168.126.6 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=123 prot=80


[root@zxw8 ~]# cat /etc/redhat-release 查看版本
1 .對劇本語法檢測:
ansible-playbook --syntax-check  /root/ansible/httpd.yaml
 
2.-C模擬執行劇本:
ansible-playbook  -C /root/ansible/httpd.yaml
 
3.執行劇本:
ansible-playbook   /root/ansible/httpd.yaml
 

示例1:基礎

 

 


示例2:變量

 

 


示例3:迭代

 

 


 示例4:觸發器notify

 

示例5:模板templates

 

相關文章
相關標籤/搜索