自定義主機列表 創(chuàng)建一個文件寫入要執(zhí)行的配置語句ansible -i 文件名 dockers -m ping -o方法二:修改 /etc/ansible/ansible.cfg/ 配置文件里面的 inventory = /etc/ansible/hosts 換成自己的定義的文件路徑
ansible是新出現(xiàn)的自動化運維工具烟号,基于Python開發(fā)售睹,集合了眾多運維工具(puppet帕翻、cfengine、chef称勋、func、fabric)的優(yōu)點涯竟,實現(xiàn)了批量系統(tǒng)配置赡鲜、批量程序部署、批量運行命令等功能庐船。
無客戶端银酬。
安裝:
yum -y install rpel-release
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install ansible
rpm -ql ansible? #列出所有文件
rpm-qc ansible? #查看配置文件
ansible-help? ? #查看ansible幫助
ansible-doc-l? #看所有模塊(A10,華為筐钟,docker揩瞪,EC2,aws等等廣大廠商設(shè)備)
ansible-doc-s yum #看yum模塊篓冲,了解其功能? exam
定義主機清單? ? vim /etc/ansible/hosts?
測試連通性? ansible localhost -m ping? ? #-m 指定模塊李破,-o 一行輸出
簡潔輸出? ansible host1 -m ping -o
ansible host2 -m ping -u root? -k 去掉(yes/no)的詢問
方法二:vim /etc/ansible/ansible.cfg
? ? ? ? ? ? ? ? host_key_checking = False? ? 去掉注釋
修改端口:vim /etc/ssh/sshd_config
Port 2222
ansible_ssh_user='root'
ansible_ssh_pass='chen'
ansible_ssh_prot='2222'
變量
[webserver]
host[
1:4]
[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='666666'
子分組
[webserver]
host[1:4]
[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='666666'
自定義主機列表? 創(chuàng)建一個文件寫入要執(zhí)行的配置語句
ansible -i 文件名 dockers -m ping -o
方法二:
修改 /etc/ansible/ansible.cfg/ 配置文件里面的 inventory = /etc/ansible/hosts?
換成自己的定義的文件路徑
點對點模式(模塊)
shell模塊(幫助:ansible-doc shell)
ansible webserver -m shell -a 'hostname' -o 獲取主機名
ansible wevserver -m shell -a 'hostname' -o -f 2
? -f FORKS, --forks=FORKS? Ansible一次命令執(zhí)行并發(fā)的線程數(shù)。NUM被指定為一個整數(shù),默認是5
? ? ? ? ? ? ? ? ? ? ? ? specify number of parallel processes to use
? ? ? ? ? ? ? ? ? ? ? ? (default=5)
復(fù)制模塊(幫助:ansible-doc copy)
ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp2.txt owner=root group=bin mode=777 backup='yes'
用戶模塊(幫助:ansible-doc user)
創(chuàng)建用戶 ansible webserver -m user -a 'name=chen state=present'? 創(chuàng)建
刪除用戶 ansible webserver -m user -a 'name=chen state=absent'? 刪除
修改密碼 ansible webserver -m user -a 'name=chen password="123"'
加密密碼 echo "123" | openssl passwd -1 -stbin
修改shell? ansible webserver -m user -a 'name=chen shell=/sbin/nologin append=yes'
軟件包管理(幫助:ansible-doc yum)
升級所有的包 ansible host1 -m yum -a 'name="*" state=latest
例:
? ? 安裝Apache:
? ? ansible host2 -m yum -a 'name="apache" state=latest
服務(wù)模塊(幫助:ansible-doc service)
啟動httpd? ansible host2 -m service -a 'name=httpd state=started'
開機啟動? ? ansible host2 -m service -a 'name=httpd state=started enable=yes'
停止? ? ? ? ansible host2 -m service -a 'name=httpd state=stoped'
重啟? ? ? ? ansible host2 -m service -a 'name=httpd state=restarted'
開機機制啟動 ansible host2 -m service -a 'name=httpd state=start enable=no'
文件模塊(幫助:ansible-doc file)
創(chuàng)建文件 ansible host2 -m file -a 'path=/tmp/88.txt mode=777 state=touch'
創(chuàng)建目錄 ansible host2 -m file -a 'path=/tmp/88.txt mode=777 state='directory'
收集模塊(幫助:ansible-doc setup)
查詢(收集)所有的信息 ansible host3 -m setup
ansible host3 -m serup -a 'file=ansible_all_ipv4_addresses'
Role-角色扮演
roles簡介:roles則是在ansible中纹因,playbooks的目錄組織結(jié)構(gòu)喷屋。
而模塊化之后,成為roles的組織結(jié)構(gòu)瞭恰,易讀屯曹,代碼可重用,層次清晰。
準備目錄結(jié)構(gòu) :
touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
echo 1234 > roles/nginx/files/index.html
yum install -y nginx && cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2
編寫任務(wù)
---
- name: install epel-release packge
? yum: name=epel-release state=latest
- name: install nginx packge
? yum: name=nginx state=latest
- name: copy index.html
? copy: src=index.html dest=/usr/share/nginx/html/index.html
- name: copy nginx.conf template
? template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
? notify: restart nginx
- name: make sure nginx service running
? service: name=nginx state=started enabled=yes
對迭代項的引用恶耽,固定變量名為"item”密任,使用with_item屬性給定要迭代的元素;
準備配置文件
vim roles/nginx/templates/nginx.conf.j2
修改配置文件中的:
? ? worker_processes? {{ ansible_processor_cores }};
調(diào)用內(nèi)部已知變量
? ? worker_connections {{ worker_connections }};
自定義變量
編寫變量
vim roles/nginx/vars/main.yaml
worker_connections: 10240
編寫處理程序
vim roles/nginx/handlers/main.yaml
? ? ---
? ? - name: restart nginx
? ? ? service: name=nginx state=restarted
編寫劇本
vim roles/site.yaml
? ? - hosts: host4 #這里需要安裝那臺機器就寫那臺
? ? - roles:
? ? - nginx? ?
實施
ansible-playbook site.yaml --syntax-check #在本地測試不真正曲線上環(huán)境安裝
ansible-playbook site.yaml --syntax-check #s實施腳本
然后去驗證機器
課堂隨記
ansble localhost -m (模塊)? ping
ping? ICMP:網(wǎng)際消息管理協(xié)議
saltack
主機清單
客戶機更改ssh的端口號以保護ansible
vm /etc/ansible/hosts? 加各種屬性
wars:vars 變量?
children 和組
-doc exam
192.168.113.129? ? 6
192.168.113.130? ? 7? ? 主機
192.168.113.133? ? ? 1
192.168.113.132? ? ? 2
packages
var