第1章 ansible介紹
1.什么是ansible
1.python寫的?套?動(dòng)化運(yùn)維?具
2.ansible基于SSH協(xié)議通訊
2.為什么需要ansible
1.有狀態(tài)管理
2.批量部署,批量執(zhí)?命令
3.統(tǒng)?配置管理,模板管理
4.批量收集主機(jī)信息
5.批量分發(fā)?件
3.如何學(xué)習(xí)ansible
1.你所需要的命令都有專?的模塊
2.模塊使?的語法是官?定義的
3.盡量少?shell模塊.當(dāng)需要?shell模塊的時(shí)候,停下來思考?下,是不是有專?的模塊可以使?
4.多看優(yōu)秀同學(xué)的分享
第2章 ansible安裝部署
yum install ansible -y
ansible --version
第3章 ansible主機(jī)清單
1.什么是主機(jī)清單
https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html
2.主機(jī)分組執(zhí)?
主機(jī)清單配置:
[root@m01 ~]# vim /etc/ansible/hosts
[web]
172.16.1.31
172.16.1.41
[nfs]
172.16.1.31
[backup]
172.16.1.41
分組執(zhí)?測(cè)試命令:
ansible web -m ping
ansible nfs -m ping
ansible backup -m ping
3.所有的主機(jī)都執(zhí)?
兩種?法:
1.執(zhí)?all就代表把所有主機(jī)全部執(zhí)?
2.主機(jī)清單?把所有主機(jī)劃分到?個(gè)組?,注意,?個(gè)主機(jī)可以屬于多個(gè)組
主機(jī)清單配置:
[web]
172.16.1.7
172.16.1.8
[nfs]
172.16.1.31
[backup]
172.16.1.41
[zabbix]
172.16.1.7
172.16.1.8
172.16.1.31
172.16.1.41
測(cè)試命令:
ansible all -m ping
ansible zabbix -m ping
4.SSH使?密碼連接并且端?號(hào)不是22
主機(jī)清單配置:
[web]
172.16.1.31 ansible_port=9527
172.16.1.41
測(cè)試命令:
ansible web -m ping
5.同組主機(jī)SSH端?號(hào)不?樣,賬號(hào)密碼也不?樣
?法1: 修改主機(jī)清單配置:
前提條件,需要提前把主機(jī)信息加?到know_host?件?
[web]
172.16.1.31 ansible_ssh_port=9527 ansible_ssh_pass='12345678'
172.16.1.41 ansible_ssh_port=9528 ansible_ssh_pass='123456'
?法2: 修改ansible配置?件,打開取消認(rèn)證的注釋
host_key_checking = False
測(cè)試命令:
ansible web -m ping
6.同?組連續(xù)的IP
主機(jī)清單配置:
[zabbix]
172.16.1.[31:41]
測(cè)試命令:
ansible zabbix -m ping
7.同?組具有相同的變量
主機(jī)清單配置:
[web]
172.16.1.31 ansible_ssh_pass='12345678'
172.16.1.41 ansible_ssh_pass='123456'
[web:vars]
ansible_ssh_port=9527
測(cè)試命令:
ansible zabbix -m ping
8.所有主機(jī)都生效的變量
[web]
172.16.1.7 ansible_ssh_pass='12345678'
172.16.1.8 ansible_ssh_pass='123456'
[nfs]
172.16.1.31
[all:vars]
ansible_ssh_port=9527
第4章 ansible常?模塊
0.如何學(xué)習(xí)ansible模塊
1.看官? 看官? 看官?
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/index.html
1.ping 測(cè)試連通性
命令解釋:
ansible 主機(jī)組 -m 模塊名稱 [模塊參數(shù)]
執(zhí)?命令:
ansible zabbix -m ping
2.command 簡(jiǎn)單命令模塊
命令解釋:
ansible 主機(jī)組 -m command -a '需要批量執(zhí)?的命令'
執(zhí)?命令:
ansible web -m command -a 'ls /tmp'
3.shell 萬能模塊
命令解釋:
ansible 主機(jī)組 -m shell -a '需要批量執(zhí)?的命令'
執(zhí)?命令:
ansible web -m shell -a 'ls /tmp|grep 123'
4.copy 拷??件
命令解釋:
ansible web -m copy -a '參數(shù)'
簡(jiǎn)單發(fā)送?件:
ansible web -m copy -a 'src=/opt/m-61.txt dest=/opt/'
發(fā)送?件的同時(shí)指定?件權(quán)限和屬性:屬于www?戶,并且權(quán)限為600
ansible all -m copy -a "src=/root/m-61.txt dest=/opt/ owner=www group=www
mode=600"
發(fā)送?件的同時(shí)備份?份:
ansible all -m copy -a "src=/root/m-61.txt dest=/opt/ owner=www group=www
mode=600 backup=yes"
寫????本到指定?件:
ansible backup -m copy -a "content='rsync_backup:123456' dest=/etc/rsync.passwd mode=600"
復(fù)制?錄:
ansible backup -m copy -a "src=/opt dest=/opt/"
復(fù)制?錄下的?件:
ansible backup -m copy -a "src=/opt/ dest=/opt/"
5.file ?件相關(guān)
命令解釋:
請(qǐng)-去看官?
https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module
創(chuàng)建?個(gè)?件:
ansible all -m file -a "path=/opt/xiaozhang.txt state=touch"
創(chuàng)建?個(gè)?錄:
ansible all -m file -a "path=/opt/xiaozhang state=directory"
刪除一個(gè)文件或目錄
ansible web -m file -a 'path=/opt/linux6 state=absent'
ansible web -m file -a 'path=/opt/linux6.txt state=absent'
創(chuàng)建?件同時(shí)制定?戶屬主權(quán)限
ansible web -m file -a 'path=/opt/linux6.txt state=touch owner=www group=www mode=600'
ansible web -m file -a 'path=/opt/linux6 state=directory owner=www group=www mode=777'
對(duì)已經(jīng)存在的文件修改屬性
ansible web -m file -a 'path=/opt/linux6.txt mode=644'
ansible web -m file -a 'path=/opt/yazhang mode=777 owner=www group=www'
ansible web -m file -a 'path=/opt/linux6.txt mode=644 owner=www group=www'
6.script 執(zhí)?腳本
命令解釋:
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/shell_module.html#shell-module
編寫腳本?件:
[root@m01 ~]# cat > echo_ip.sh <<EOF
#!/bin/bash
echo "$(hostname -I)" > /tmp/ip.txt
EOF
執(zhí)?命令:
ansible all -m script -a "echo_ip.sh"
查看主機(jī)?成的?件:
ansible all -m shell -a "cat /tmp/ip.txt"
查看詳細(xì)輸出過程
ansible all -vvv -m script -a "echo_ip.sh"
7.cron 定時(shí)任務(wù)
命令解釋:
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/cron_module.html#cron-module
創(chuàng)建測(cè)試腳本:
[root@m01 ~]# cat echo_hostname.sh
#!/bin/bash
echo "$(date +%M:%S) $(hostname)" >> /tmp/hostname.txt
傳統(tǒng)定時(shí)任務(wù)命令:
* * * * * /bin/bash /opt/echo_hostname.sh
默認(rèn)5顆星創(chuàng)建定時(shí)任務(wù):
ansible web -m cron -a "job='/bin/bash /opt/echo_hostname.sh'"
默認(rèn)5顆星創(chuàng)建定時(shí)任務(wù)并指定任務(wù)名稱:
ansible web -m cron -a "name=hostname job='/bin/bash /opt/echo_hostname.sh'"
修改指定名稱的定時(shí)任務(wù):
ansible web -m cron -a "name=hostname minute='*/5' job='/bin/bash
/opt/echo_hostname.sh'"
注釋?條任務(wù):
ansible all -m cron -a "name=hostname minute='*/5' job='/bin/bash
/opt/echo_hostname.sh' disabled=yes"
打開注釋的任務(wù):
ansible all -m cron -a "name=hostname minute='*/5' job='/bin/bash
/opt/echo_hostname.sh'"
刪除定時(shí)任務(wù):
ansible web -m cron -a "name=hostname state=absent"
8.group 組相關(guān)
命令解釋:
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/group_module.html#group-module
創(chuàng)建組的同時(shí)指定gid:
ansible all -m group -a "name=oldzhang gid=1010"
刪除?戶組
ansible all -m group -a "name=oldzhang gid=1010 state=absent"
9.user ?戶相關(guān)
命令解釋:
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/user_module.html#user-module
創(chuàng)建?戶的同時(shí)指定uid和組id并且不允許登陸不創(chuàng)建家?錄:
ansible all -m user -a "name=oldzhang uid=1010 group=oldzhang create_home=no
shell=/sbin/nologin"
10.yum 安裝軟件
命令解釋:
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/yum_module.html#yum-module
安裝?個(gè)軟件的最新版本:
ansible all -m yum -a "name=iftop state=latest"
卸載?個(gè)軟件:
ansible all -m yum -a "name=iftop state=absent"
11.service 服務(wù)啟動(dòng)
命令解釋:
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/systemd_module.html#systemd-module
啟動(dòng)?個(gè)服務(wù):
ansible web -m systemd -a "name=nginx state=started"
停??個(gè)服務(wù):
ansible web -m systemd -a "name=nginx state=stopped"
設(shè)置?個(gè)服務(wù)開啟?啟動(dòng):
ansible web -m service -a "name=nginx enabled=yes"
設(shè)置?個(gè)服務(wù)不要開機(jī)?啟動(dòng):
ansible web -m service -a "name=nginx enabled=no"
12.mount 掛載命令
命令解釋:
https://docs.ansible.com/ansible/latest/collections/ansible/posix/mount_module.html#mount-module
掛載?個(gè)?錄并且寫?開機(jī)?啟動(dòng)?件fstab:
ansible web -m mount -a "src='10.0.0.31:/data' path=/data fstype=nfs state=mounted"
只寫?fstab但是不掛載:
ansible web -m mount -a "src='10.0.0.31:/data' path=/data fstype=nfs state=present"
卸載已經(jīng)掛載的?錄并且刪除fstab?的條?:
ansible web -m mount -a "src='10.0.0.31:/data' path=/data fstype=nfs state=absent"
卸載已經(jīng)掛載的?錄,但是不刪除fstab?的條?:
ansible web -m mount -a "src='10.0.0.31:/data' path=/data fstype=nfs state=unmounted"
掛載狀態(tài)解釋:
mounted 掛載上并且寫?fstab
present 僅寫?fstab,不掛載
absent 卸載并且移除fstab條?
unmounted 僅卸載,不移除fstab條?
13.unarchive 解壓縮
命令解釋:
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/unarchive_module.html
把??的壓縮包解壓到遠(yuǎn)程服務(wù)器的指定?錄:
ansible web -vvv -m unarchive -a "src=php71.tar.gz dest=/opt/"
將遠(yuǎn)程服務(wù)器本身的壓縮包解壓到遠(yuǎn)程服務(wù)器的指定?錄;
ansible web -m unarchive -a "src=/opt/php71.tar.gz dest=/opt/ remote_src=yes"
14.archive 壓縮
命令解釋:
https://docs.ansible.com/ansible/latest/collections/community/general/archive_module.html
壓縮?件到指定?錄:
ansible web -m archive -a "path=/opt/php71 dest=/opt/php71.tar.gz"
15.setup 獲取主機(jī)信息
命令解釋:
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/setup_module.html
使?內(nèi)置變量獲取遠(yuǎn)程主機(jī)的IP地址:
ansible web -m setup
16.查看幫助
命令解釋:
ansible-doc
執(zhí)?命令:
ansible-doc copy
第5章 ansible顏?輸出解釋
綠?: 代表執(zhí)?成功,但是狀態(tài)沒有發(fā)?任何改變
??: 代表執(zhí)?成功,狀態(tài)并發(fā)?了改變
紅?: 有報(bào)錯(cuò),執(zhí)?失敗
紫?: 警告,建議使?專?的模塊
藍(lán)?: 詳細(xì)的執(zhí)?過程
=======================================================================
第1章 ansible ??介紹
1.為什么需要使???
1.不太靈活,臃腫
2.全部寫在?起,修改不?便
3.配置?件隨便放,不標(biāo)準(zhǔn)
2.??解決了什么問題
1.把劇本 拆分 拆分 拆分
2.解耦,結(jié)構(gòu)更清晰,調(diào)試更?便
3.編寫??的最佳實(shí)踐
1.初級(jí)階段,不要直接寫??,先寫好劇本,然后再拆分
2.?開始不要想?步到位,不?拆的很細(xì),尤其是變量
第2章 ???錄規(guī)劃
0.官?說明
https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.htm
1.?錄說明
注意!這?的?錄結(jié)構(gòu)必須按照官?定義的要求來做!不是??隨便亂起!
tasks #存放主任務(wù)執(zhí)??件
handlers #存放handlers?件
files #存放需要發(fā)送的?件或壓縮包
templates #存放jinja模版配置?件
vars #存放變量?件
第3章 編寫rsync??
0.編寫思路
1.先寫好劇本
2.創(chuàng)建???錄
3.拷?需要發(fā)送的?件到指定?錄
4.拆分劇本
1.編寫劇本
- hosts: backup
vars:
user_id: '666'
rsync_user: 'www'
tasks:
#1.創(chuàng)建www組和www?戶
- name: 01-create_group
group:
name: "{{ rsync_user }}"
gid: "{{ user_id }}"
#2.創(chuàng)建www?戶
- name: 02-create_user
user:
name: "{{ rsync_user }}"
uid: "{{ user_id }}"
group: "{{ rsync_user }}"
create_home: no
shell: /sbin/nologin
#3.創(chuàng)建數(shù)據(jù)?錄并更改授權(quán)
- name: 03-create_data
file:
path: "{{ item }}"
state: directory
owner: "{{ rsync_user }}"
group: "{{ rsync_user }}"
mode: '755'
loop:
- /data/
- /backup/
#4.安裝rsync軟件
- name: 04-install_rsync
yum:
name: rsync
state: latest
#5.復(fù)制配置?件和密碼?件
- name: 05-copy pwd&conf
copy:
src: "{{ item.src }}"
dest: /etc/
mode: "{{ item.mode }}"
notify:
- restart rsyncd
loop:
- { src: /root/script/rsync/rsyncd.conf, mode: '644'}
- { src: /root/script/rsync/rsync.passwd, mode: '600'}
#6.啟動(dòng)服務(wù)
- name: 06-start
systemd:
name: rsyncd
state: started
enabled: yes
#7.重啟服務(wù)
handlers:
- name: restart rsyncd
systemd:
name: rsyncd
state: restarted
2.創(chuàng)建???錄
[root@m-61 ~]# cd /etc/ansible/roles/
[root@m-61 /etc/ansible/roles]# mkdir
rsync_server/{tasks,handlers,files,templates,vars} -p
[root@m-61 /etc/ansible/roles]# tree rsync_server/
rsync_server/
├── files
├── handlers
├── tasks
├── templates
└── vars
3.把劇本復(fù)制到tasks?錄
├── tasks
│ └── main.yaml
4.把配置?件復(fù)制到file?錄
cp script/rsync/* /etc/ansible/roles/rsync_server/files/
5.拆分handlers
[root@m-61 ~]# cat /etc/ansible/roles/rsync_server/handlers/main.yaml
- name: restart rsyncd
systemd:
name: rsyncd
state: restarted
6.拆分vars
[root@m-61 ~]# cat /etc/ansible/roles/rsync_server/vars/main.yaml
user_id: '666'
rsync_user: 'www'
7.精簡(jiǎn)tasks任務(wù)?件
[root@m-61 ~]# cat /etc/ansible/roles/rsync_server/tasks/main.yaml
#1.創(chuàng)建www組和www?戶
- name: 01-create_group
group:
name: "{{ rsync_user }}"
gid: "{{ user_id }}"
#2.創(chuàng)建www?戶
- name: 02-create_user
user:
name: "{{ rsync_user }}"
uid: "{{ user_id }}"
group: "{{ rsync_user }}"
create_home: no
shell: /sbin/nologin
#3.創(chuàng)建數(shù)據(jù)?錄并更改授權(quán)
- name: 03-create_data
file:
path: "{{ item }}"
state: directory
owner: "{{ rsync_user }}"
group: "{{ rsync_user }}"
mode: '755'
loop:
- /data/
- /backup/
#4.安裝rsync軟件
- name: install_rsync
yum:
name: 04-rsync
state: latest
#5.復(fù)制配置?件和密碼?件
- name: 05-copy pwd&conf
copy:
src: "{{ item.src }}"
dest: /etc/
mode: "{{ item.mode }}"
notify:
- restart rsyncd
loop:
- { src: rsyncd.conf, mode: '644'}
- { src: rsync.passwd, mode: '600'}
#6.啟動(dòng)服務(wù)
- name: start
systemd:
name: rsyncd
state: started
enabled: yes
8.編寫調(diào)??件
[root@m-61 ~]# cat /etc/ansible/rsync_server.yaml
- hosts: rsync_server
roles:
- rsync_server
9.編寫主機(jī)清單
[root@m-61 ~]# cat /etc/ansible/hosts
[rsync_server]
172.16.1.41
10.調(diào)試運(yùn)?
cd /etc/ansible/
ansible-playbook -C rsync_server.yaml
ansible-playbook rsync_server.yaml
第4章 編寫sshd??
1.編寫思路
1.先拷?配置?件到template?錄下并重命名為j2
2.編寫tasks?件
3.調(diào)試運(yùn)?
2.創(chuàng)建???錄
cd /etc/ansible/roles/
mkdir sshd/{tasks,handlers,files,templates,vars} -p
3.編寫jinja模版?件
jinja模板注意:
1.模塊必須是template
2.模版?件必須以.j2結(jié)尾
3.模版?件必須放在template?錄下
關(guān)鍵配置:
#復(fù)制sshd配置?件到template?件夾下
Port {{ ssh_port }}
ListenAddress {{ ansible_facts.eth1.ipv4.address }}
4.編寫變量?件
[root@m-61 /etc/ansible/roles/sshd]# cat vars/main.yaml
ssh_port: '22'
5.編寫handlers?件
[root@m-61 /etc/ansible/roles/sshd]# cat handlers/main.yaml
- name: restart sshd
systemd:
name: sshd
state: restarted
6.編寫主任務(wù)?件
[root@m-61 /etc/ansible/roles/sshd]# cat tasks/main.yaml
#1.復(fù)制配置?件和密碼?件
- name: 01_copy_sshd
template:
src: sshd_config.j2
dest: /etc/ssh/sshd_config
mode: '600'
backup: yes
notify:
- restart sshd
#2.啟動(dòng)服務(wù)
- name: start
systemd:
name: sshd
state: started
enabled: yes
7.查看最終的?錄
[root@m-61 /etc/ansible/roles]# tree sshd/
sshd/
├── files
├── handlers
│ └── main.yaml
├── tasks
│ └── main.yaml
├── templates
│ └── sshd_config.j2
└── vars
└── main.yaml
8.編寫主調(diào)??件
[root@m-61 /etc/ansible/roles]# cat ../sshd.yaml
- hosts: ssh
roles:
- sshd
第5章 編寫nfs??
1.編寫思路
1.先拷?配置?件到template?錄下并重命名為j2
2.編寫handlers
3.編寫tasks
2.創(chuàng)建???錄
cd /etc/ansible/roles/
mkdir nfs_server/{tasks,handlers,files,templates,vars} -p
3.編寫jinja模版?件
[root@m-61 ~]# cat /etc/ansible/roles/nfs_server/templates/exports.j2
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=1001,anongid=1001)
4.編寫handlers?件
[root@m-61 ~]# cat /etc/ansible/roles/nfs_server/handlers/main.yaml
- name: restart nfs
systemd:
name: nfs
state: restarted
5.編寫主任務(wù)?件
[root@m-61 ~]# cat /etc/ansible/roles/nfs_server/tasks/main.yaml
#1.創(chuàng)建www組和www?戶
- name: create_group
group:
name: www
gid: 666
#2.創(chuàng)建www?戶
- name: create_user
user:
name: www
uid: 666
group: www
create_home: no
shell: /sbin/nologin
#3.創(chuàng)建數(shù)據(jù)?錄并更改授權(quán)
- name: create_data
file:
path: "{{ item }}"
state: directory
owner: www
group: www
mode: '755'
loop:
- /data/
- /backup/
#4.安裝nfs軟件
- name: install_nfs
yum:
name: nfs-utils
state: latest
#5.復(fù)制配置?件和密碼?件
- name: copy_exports
template:
src: exports.j2
dest: /etc/exports
notify:
- restart nfs
#6.啟動(dòng)服務(wù)
- name: start
systemd:
name: nfs
state: started
enabled: yes
6.編寫調(diào)??件
[root@m-61 ~]# cat /etc/ansible/nfs_server.yaml
- hosts: nfs
roles:
- nfs_server
第六章 編寫lsyncd服務(wù)
第6章 拆分init??
0.編寫思路
1.先分析以前寫過所有的???重復(fù)的操作
2.把重復(fù)的操作內(nèi)容單獨(dú)寫?個(gè)??,例如:init
3.先備份?份以前寫好的???件
4.精簡(jiǎn)以前的???件,刪除重復(fù)的內(nèi)容
5.調(diào)試,運(yùn)?,檢查
1.找出重復(fù)的操作
1.創(chuàng)建www組和www?戶
2.創(chuàng)建www?戶
3.創(chuàng)建數(shù)據(jù)?錄并更改授權(quán)
4.安裝rsync軟件
5.安裝nfs軟件
2.創(chuàng)建???錄
cd /etc/ansible/roles/
mkdir init/{tasks,handlers,files,templates,vars} -p
3.編寫jinja模版?件
4.編寫handlers?件
5.編寫主任務(wù)?件
[root@m-61 /etc/ansible]# cat /etc/ansible/roles/init/tasks/main.yaml
#1.創(chuàng)建www組和www?戶
- name: create_group
group:
name: www
gid: 666
#2.創(chuàng)建www?戶
- name: create_user
user:
name: www
uid: 666
group: www
create_home: no
shell: /sbin/nologin
#3.創(chuàng)建數(shù)據(jù)?錄并更改授權(quán)
- name: create_data
file:
path: "{{ item }}"
state: directory
owner: www
group: www
mode: '755'
loop:
- /data/
- /backup/
#4.安裝nfs軟件
- name: install_soft
yum:
name: "{{ item }}"
state: latest
loop:
- rsync
- nfs-utils
第?章 拆分后的各個(gè)服務(wù)???件
1.拆分后的rsync??
[root@m-61 ~]# cat /etc/ansible/roles/rsync_server/tasks/main.yaml
#1.復(fù)制配置?件和密碼?件
- name: copy pwd&conf
copy:
src: "{{ item.src }}"
dest: /etc/
mode: "{{ item.mode }}"
notify:
- restart rsyncd
loop:
- { src: rsyncd.conf, mode: '644'}
- { src: rsync.passwd, mode: '600'}
#2.啟動(dòng)服務(wù)
- name: start
systemd:
name: rsyncd
state: started
enabled: yes
2.拆分后的nfs??
[root@m-61 ~]# cat /etc/ansible/roles/nfs_server/tasks/main.yaml
#1.復(fù)制配置?件和密碼?件
- name: copy_exports
template:
src: exports.j2
dest: /etc/exports
notify:
- restart nfs
#2.啟動(dòng)服務(wù)
- name: start
systemd:
name: nfs
state: started
enabled: yes
3.拆分后的lsyncd??
4.調(diào)??件
rsync
[root@m-61 ~]# cat /etc/ansible/rsync_server.yaml
- hosts: rsync_server
roles:
- init
- rsync_server
nfs
[root@m-61 ~]# cat /etc/ansible/nfs_server.yaml
- hosts: nfs
roles:
- init
- nfs_server
第1章 Ansible劇本介紹
1.什么是playbook劇本
電影劇本:
電影名
演員
場(chǎng)景
時(shí)間
事件
臺(tái)詞
Ansible劇本:
?系列的任務(wù)按照我們期望的結(jié)果編排在?起
playbook組成:
hosts: 定義主機(jī)??
tasks: 具體執(zhí)?的任務(wù)
簡(jiǎn)單理解:不同的模塊去完成?件事
舉例: xx的陽光快樂時(shí)光
- 演員列表: xx
場(chǎng)景:
- 場(chǎng)景1: xx??拿著肥皂?進(jìn)浴室
動(dòng)作場(chǎng)?: ??滑,肥皂在空中托?斯360度回旋落地
- 場(chǎng)景2: xx撿肥皂
動(dòng)作場(chǎng)?: xx彎腰的時(shí)候(?賽克),美?房東從后?出現(xiàn)了
- 需要執(zhí)?的主機(jī): nfs
任務(wù):
- 任務(wù)1: 創(chuàng)建?戶
動(dòng)作: 創(chuàng)建?戶的命令
- 任務(wù)2: 創(chuàng)建?錄
動(dòng)作: 創(chuàng)建?錄的命令
2.playbook劇本的優(yōu)勢(shì)
1.減少重復(fù)的書寫的指令: ansible backup -m file -a
2.看起來簡(jiǎn)潔清晰
3.功能強(qiáng)?,可以控制流程,?如:判斷,循環(huán),變量,標(biāo)簽
4.其他劇本可以復(fù)?
5.提供語法檢查以及模擬執(zhí)?
第2章 劇本的格式書寫要求
1.YAML格式特點(diǎn)
1.嚴(yán)格的縮進(jìn)表示層級(jí)關(guān)系
2.不要使?tab縮進(jìn)
3.: 后??定要有空格
4.- 后??定要有空格
5.?件后綴名需要改為yaml或yml,vim可以智能?亮提示
2.劇本的組成
hosts: 需要執(zhí)?的主機(jī)
tasks: 需要執(zhí)?的任務(wù)
name: 任務(wù)名稱
第3章 編寫Rsync劇本
0.官?舉例
https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html#play
1.命令?模式的編寫
#1.創(chuàng)建www組和www?戶
ansible backup -m group -a "name=www gid=666"
ansible backup -m user -a "name=www uid='666' group=www create_home=no shell=/sbin/nologin"
#2.創(chuàng)建數(shù)據(jù)?錄并更改授權(quán)
ansible backup -m file -a "path=/data state=directory owner=www group=www mode='755'"
ansible backup -m file -a "path=/backup state=directory owner=www group=www mode='755'"
#3.安裝rsync軟件
ansible backup -m yum -a "name=rsync state=latest"
#4.復(fù)制配置?件和密碼?件
ansible backup -m copy -a "src=/root/script/rsync/rsyncd.conf dest=/etc/"
ansible backup -m copy -a "src=/root/script/rsync/rsync.passwd dest=/etc/ mode='600'"
#6.啟動(dòng)服務(wù)
ansible backup -m systemd -a "name=rsyncd state=started enabled=yes"
2.改寫成劇本
- hosts: backup
tasks:
- name: 01創(chuàng)建www?戶組
group:
name: www
gid: 666
- name: 02創(chuàng)建www?戶
user:
name: www
uid: '666'
group: www
create_home: no
shell: /sbin/nologin
- name: 03創(chuàng)建數(shù)據(jù)?錄并更改授權(quán)
file:
path: /data
state: directory
owner: www
group: www
mode: '755'
- name: 04安裝rsync軟件
yum:
name: rsync
state: latest
- name: 05復(fù)制配置?件和密碼?件
copy:
src: /root/script/rsync/rsyncd.conf
dest: /etc/
- name: 06創(chuàng)建密碼?件權(quán)限為600
copy:
src: /root/script/rsync/rsync.passwd
dest: /etc/
mode: 600
- name: 07啟動(dòng)服務(wù)
systemd:
name: rsyncd
state: started
enabled: yes
3.模擬執(zhí)?
ansible-playbook -C rsync_install.yaml
4.執(zhí)?
ansible-playbook rsync_install.yaml
第4章 編寫NFS劇本
1.命令?模式的編寫
NFS服務(wù)端:
[root@m-61 /scripts]# cat nfs_server_install.yaml
- hosts: nfs_server
tasks:
- name: 01-add group
group: name=www gid='666'
- name: 02-add user
user: name=www create_home=no shell=/sbin/nologin group=www uid=666
- name: 03-install nfs service
yum: name=nfs-utils state=latest
- name: 04-copy nfs exports
copy: src=/server/scripts/exports dest=/etc/
- name: 05-create data dir
file: path=/data state=directory owner=www group=www
- name: 06-start rpcbind
service: name=rpcbind state=started
- name: 07-start nfs
service: name=nfs state=started
- name: 08-enable rpcbind
systemd: name=rpcbind enabled=yes
- name: 09-enable nfs
systemd: name=nfs enabled=yes
NFS客戶端:
[root@m-61 /scripts]# cat nfs_client_install.yaml
- hosts: nfs_client
tasks:
- name: 01-add group
group: name=www gid=666
- name: 02-add user
user: name=www create_home=no shell=/sbin/nologin group=www uid=666
- name: 03-install nfs service
yum: name=nfs-utils state=latest
- name: 04-create data dir
file: path=/data state=directory owner=www group=www
- name: 05-start rpcbind
service: name=rpcbind state=started
- name: 06-enable rpcbind
systemd: name=rpcbind enabled=yes
- name: 07-mount data
mount: path=/data src=172.16.1.31:/data fstype=nfs opts=defaults state=mounted
2.改寫成劇本
第5章 劇本?級(jí)特性-循環(huán)
0.官??檔
1.應(yīng)?場(chǎng)景
安裝多個(gè)軟件
創(chuàng)建多個(gè)?錄
復(fù)制多個(gè)?錄
復(fù)制多個(gè)?件到不同的?錄
不同的?件權(quán)限不?樣
2.循環(huán)書寫?格1:?jiǎn)?模式
- name: create_data
file: path=/data state=directory owner=www group=www
- name: create_backup
file: path=/backup state=directory owner=www group=www
3.循環(huán)書寫?格2:縮進(jìn)模式
需求: 創(chuàng)建2個(gè)?錄/data和/backup
以前的寫法:
- name: create_data
file:
path: /data
state: directory
owner: www
group: www
- name: create_data
file:
path: /backup
state: directory
owner: www
group: www
循環(huán)實(shí)現(xiàn):
- name: create_data
file:
path: "{{ item }}"
state: directory
owner: www
group: www
loop:
- /data
- /backup
4.循環(huán)書寫?格3: 混合?格
- name: create_data
file: path="{{ item }}" state=directory owner=www group=www
loop:
- /data
- /backup
5.循環(huán)書寫?格3: 多參數(shù)循環(huán)模式
- hosts: backup
tasks:
- name: create_data
file:
path: "{{ item.path }}"
state: directory
owner: www
group: www
mode: "{{ item.mode }}"
loop:
- { path: '/data' , mode: '755' }
- { path: '/backup', mode: '777' }
第6章 劇本?級(jí)特性-變量
1.應(yīng)?場(chǎng)景
1.?定義某個(gè)變量,在任務(wù)中被多次引?
2.從主機(jī)收集到系統(tǒng)信息?提取某個(gè)變量,?如IP地址,主機(jī)名
2.?定義變量并引?
- hosts: backup
vars:
data_path: /data/
dest_path: /etc/
file_path: /etc/rsync.passwd
tasks:
- name: 01mkdir
file:
path: "{{ data_path }}"
state: directory
- name: 02copy
copy:
src: "{{ file_path }}"
dest: "{{ dest_path }}"
3.使?變量獲取主機(jī)的eth1地址和主機(jī)名
- hosts: all
tasks:
- name: 03get IP
shell: "echo {{ ansible_default_ipv4.address }} >> /tmp/ip.txt"
- name: 04get hostname
shell: "echo {{ ansible_hostname }} >> /tmp/hostname.txt"
4.在主機(jī)清單?件?定義變量
主機(jī)清單
[root@m01 ~/ansible_script]# cat /etc/ansible/hosts
[web]
172.16.1.7
[web:vars]
service_name=web
[nfs]
172.16.1.31 service_name=nfs
[backup]
172.16.1.41 service_name=rsync
[all:vars]
job=it
[root@m01 ~/ansible_script]# cat /etc/ansible/hosts
[web]
10.0.0.7 port=8888
10.0.0.8 port=9999
[web:vars]
nginx_version='1.19'
引?變量
- hosts: all
tasks:
- name: echo IP
shell: "echo {{ service_name }} >> /tmp/service.txt"
- name: echo hostname
shell: "echo {{ job }} >> /tmp/service.txt"
[root@m-61 /script/playbook]# cat web_vars.yaml
- hosts: web
tasks:
- name: 01get port
shell: "echo {{ port }} >> /tmp/port.txt"
- name: 02get version
shell: "echo {{ nginx_version }} >> /tmp/version.txt"
5.循環(huán)里引用變量
- name: test for
hosts: backup
vars:
rsyncd_conf: /script/rsyncd.conf
rsyncd_pass: /script/rsync.passwd
tasks:
- name: 01-copy
copy:
src: "{{ item.src }}"
dest: /etc/
mode: "{{ item.mode }}"
loop:
- { src: "{{ rsyncd_conf }}", mode: '0644'}
- { src: "{{ rsyncd_pass }}", mode: '0600'}
6.整理ansible內(nèi)置變量
其他ansible內(nèi)置變量
ansible_facts.eth0.ipv4.address
ansible_facts.eth1.ipv4.address
ansible_nodename 節(jié)點(diǎn)名字
ansible_form_factor 服務(wù)器類型
ansible_virtualization_role 虛擬機(jī)??(宿主機(jī)或者虛擬機(jī))
ansible_virtualization_type 虛擬機(jī)類型(kvm)
ansible_system_vendor 供應(yīng)商(Dell)
ansible_product_name 產(chǎn)品型號(hào)(PowerEdge R530)
ansible_product_serial 序列號(hào)(sn)
ansible_machine 計(jì)算機(jī)架構(gòu)(x86_64)
ansible_bios_version BIOS版本
ansible_system 操作系統(tǒng)類型(linux)
ansible_os_family 操作系統(tǒng)家族(RedHat)
ansible_distribution 操作系統(tǒng)發(fā)?版(CentOS)
ansible_distribution_major_version 操作系統(tǒng)發(fā)?版主版本號(hào)(7)
ansible_distribution_release 操作系統(tǒng)發(fā)?版代號(hào)(core)
ansible_distribution_version 操作系統(tǒng)發(fā)?版本號(hào)(7.3.1611)
ansible_architecture 體系(x86_64)
ansible_kernel 操作系統(tǒng)內(nèi)核版本號(hào)
ansible_userspace_architecture ?戶模式體系(x86_64)
ansible_userspace_bits ?戶模式位數(shù)
ansible_pkg_mgr 軟件包管理器
ansible_selinux.status selinux狀態(tài)
#--------------------------------------------
ansible_processor CPU產(chǎn)品名稱
ansible_processor_count CPU數(shù)量
ansible_processor_cores 單顆CPU核?數(shù)量
ansible_processor_threads_per_core 每個(gè)核?線程數(shù)量
ansible_processor_vcpus CPU核?總數(shù)
ansible_memtotal_mb 內(nèi)存空間
ansible_swaptotal_mb 交換空間
ansible_fqdn 主機(jī)的域名
ansible_default_ipv4.interface 默認(rèn)?卡
ansible_default_ipv4.address 默認(rèn)IP地址
ansible_default_ipv4.gateway 默認(rèn)?關(guān)
********* json 格式 ********
ansible_devices 硬盤設(shè)備名
ansible_devices.vendor 硬盤供應(yīng)商
ansible_devices.model 硬盤整列卡型號(hào)
ansible_devices.host 硬盤整列卡控制器
ansible_devices.size 設(shè)備存儲(chǔ)空間
********* json 格式 ********
ansible_interfaces ?卡
ansible_{interfaces}.ipv4.address ?卡IP地址
ansible_{interfaces}.ipv6.0.address ?卡IPv6地址
ansible_{interfaces}.macaddress ?卡mac地址
第7章 劇本?級(jí)特性-注冊(cè)變量
1.應(yīng)?場(chǎng)景
調(diào)試,回顯命令執(zhí)?的內(nèi)容
把狀態(tài)保存成變量,其他任務(wù)可以進(jìn)?判斷或引?
2.使?內(nèi)置變量將IP地址保存到?本?,并將?本內(nèi)容顯示出來
案例1:引用單個(gè)注冊(cè)變量
- hosts: all
tasks:
- name: echo IP
shell: "echo {{ ansible_default_ipv4.address }} >> /tmp/ip.txt"
- name: cat IP
shell: "cat /tmp/ip.txt"
register: ip_txt
- debug:
msg: "{{ ip_txt.stdout_lines }}"
案例2:引用多個(gè)注冊(cè)變量
[root@m-61 /script/playbook]# cat register.yml
- hosts: nfs
tasks:
- name: 01get IP
shell: "echo {{ ansible_default_ipv4.address }} > /tmp/ip.txt"
- name: 02get hostname
shell: "echo {{ ansible_hostname }} > /tmp/hostname.txt"
- name: 03get hostname
shell: "cat /tmp/hostname.txt"
register: hostname
- name: 04cat
shell: "showmount -e 172.16.1.31"
register: showmount
- debug:
msg: "{{ item }}"
loop:
- "{{ showmount.stdout_lines }}"
- "{{ hostname.stdout_lines }}"
3.如果配置?件發(fā)?了變化,就重啟服務(wù),否則不重啟
- hosts: backup
tasks:
- name: 01-copy_conf
copy:
src: /opt/rsyncd.conf
dest: /etc/
register: conf_status
- name: 02-start
systemd:
name: rsyncd
state: started
enabled: yes
- name: 03-restart
systemd:
name: rsyncd
state: restarted
when: conf_status.changed
4.注冊(cè)變量和判斷場(chǎng)景
官?地址:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.ht
使?場(chǎng)景:
場(chǎng)景:
判斷所有機(jī)器/tmp/下有沒有ip.txt的?件
如果有,打印出來內(nèi)容并且格式為:
例如:
web01 has ip.txt
內(nèi)容為:
如果不存在:
輸出內(nèi)容:nfs is nofile
5.解決?案
- hosts: all
vars:
path1: /tmp/ip
tasks:
- name: test1
shell: 'cat {{path1}}'
register: retval
ignore_errors: true
- name: test2
debug:
msg: '{{ansible_hostname}} has {{path1}} , content is:
{{retval.stdout}}'
when: retval is success
- name: test3
debug:
msg: '{{path1}} is nofile'
when: retval is failed
第8章 劇本?級(jí)特性-服務(wù)狀態(tài)管理
0.官??檔
https://docs.ansible.com/ansible/latest/user_guide/playbooks_handlers.html
1.應(yīng)?場(chǎng)景
?前的情況:
配置?件發(fā)?變化也不會(huì)重啟
理想中的情況:
1.如果配置?件不發(fā)?變化,就不執(zhí)?重啟
2.如果配置?件發(fā)?變化,就執(zhí)?重啟
2.命令實(shí)現(xiàn)
- hosts: backup
tasks:
- name: 01-copy_conf
copy:
src: /root/script/rsync/rsyncd.conf
dest: /etc/
notify:
- restart rsyncd
- name: 02-start
systemd:
name: rsyncd
state: started
enabled: yes
handlers:
- name: restart rsyncd
systemd:
name: rsyncd
state: restarted
3.錯(cuò)誤總結(jié)
1.handlers位置要放在最后
2.handlers?任務(wù)定義的名字是什么,notify?就寫什么,不能不?樣
第9章 劇本?級(jí)特性-選擇標(biāo)簽
1.應(yīng)?場(chǎng)景
調(diào)試,選擇性的執(zhí)?任務(wù)
2.添加標(biāo)簽-編寫
- hosts: nfs
tasks:
- name: 01-add group
group: name=www gid=666
tags: 01-add-group
- name: 02-add user
user: name=www create_home=no shell=/sbin/nologin group=www uid=666
tags: 02-add-user
- name: 03-install nfs service
yum: name=nfs-utils state=latest
tags: 03-install nfs service
- name: 04-copy nfs exports
copy: src=/service/scripts/exports dest=/etc/
tags: 04-copy-nfs-exports
- name: 05-create data dir
file: path=/data state=directory owner=www group=www
tags: 05-create-data-dir
- name: 06-create passwd conf
copy: content='123' dest=/etc/rsync.passwd mode=600
tags: 06-create-passwd
- name: 07-start rpcbind
service: name=rpcbind state=started
tags: 07-start-rpcbind
- name: 08-start nfs
service: name=nfs state=started
tags: 08-start-nfs
- name: 09-enable rpcbind
systemd: name=rpcbind enabled=yes
tags: 09-enable-rpcbind
- name: 10-enable nfs
systemd: name=nfs enabled=yes
tags: 10-enable-nfs
3.打印出playbook?要執(zhí)?的所有標(biāo)簽
ansible-playbook --list-tags rsync_install.yaml
4.指定運(yùn)?某個(gè)標(biāo)簽
ansible-playbook -t '03-install nfs service' rsync_install_tag.yaml
5.指定運(yùn)?多個(gè)標(biāo)簽
ansible-playbook -t 01-add-group,02-add-user,05-create-data-dir rsync_install_tag.yaml
6.指定不運(yùn)?某個(gè)標(biāo)簽
ansible-playbook --skip-tags 01-add-group rsync_install_tag.yaml
7.指定不運(yùn)?多個(gè)標(biāo)簽
ansible-playbook --skip-tags 01-add-group,02-add-user,04-copy-nfs-exports
rsync_install_tag.yaml
第10章 劇本?級(jí)特性-選擇tasks
1.應(yīng)?場(chǎng)景
調(diào)試的時(shí)候
從某個(gè)任務(wù)開始往下依次執(zhí)?
2.查看task列表
ansible-playbook --list-tasks rsync_install_tag.yaml
3.選擇從哪?個(gè)task開始執(zhí)?
ansible-playbook --start-at-task '05-create data dir' rsync_install_tag.yaml
第11章 運(yùn)?檢查規(guī)范
1.檢查劇本拼寫規(guī)范
ansible-playbook --syntax-check rsync_install_tag.yaml
2.檢查這個(gè)任務(wù)執(zhí)?的主機(jī)對(duì)象
ansible-playbook --list-hosts rsync_install_tag.yaml
3.檢查這個(gè)劇本需要執(zhí)?哪些任務(wù)
ansible-playbook --list-tasks rsync_install_tag.yaml
4.模擬執(zhí)?劇本
ansible-playbook -C rsync_install_tag.yaml
5.真正執(zhí)?
ansible-playbook rsync_install_tag.yaml