ansible
特性
模塊化:調用特定的模塊,完成特定任務
有paramiko,pyYAML, ??jinja2(模板語言)三個關鍵模塊
支持自定義模塊
基于Python語言實現
部署簡單,基于Python和SSH(默認已安裝),agentless
安全,基于openssh
支持playbook編排任務
冪等性:一個任務執行1遍和執行n遍效果一樣,不因重復執行帶來意外情況
無需代理不依賴PKI(無需ssl)
可使用任何編程語言寫模塊
YAML格式,編排任務,支持豐富的數據結構
較強大的多層解決方案
Ansible主要組成部分
Ansible playbooks:任務劇本,編排定義ansible任務集的配置文件,由ansible順序依次執行,通常是JSON格式的YML文件
Inventory:ansible管理主機的清單/etc/anaible/hosts
MODULES:ansible執行命令的功能模塊,多數為內置的核心模塊,也可自定義
Plugins 模塊功能的補充,如連接類型插件、循環插件、變量插件、過濾插件等,該功能不常用
API:供第三方程序調用的應用程序編程接口
ANSIBLE:組合inventory、api。Modules
Plugins的綠框,可以理解為是ansible命令工具,其核心執行工具
Ansible命令執行來源
USER,普通用戶,即SYSTEM ADMINISTRATOR
CMDB(資產管理系統),API調用
PUBLIC/PRIVATE CLOUD API 調用
USER–>ANSIBLE PLAYBOOK –>ANSIBILE
利用ansible細線管理的方式
Ad-hoc 即ansible命令,主要用于臨時命令使用場景
Ansible-playbook 主要用于長期規劃好的,大型項目的場景,需要有前提的規劃
[root@centos7 ~]# Hostnamectl ??set-hostname ?ansible ??該主機名字,
[root@centos7 ~]#exec bash
[root@centos7 ~]# vim /etc/ansible/hosts ??將被管理主機加入主機清單
[root@centos7 ~]# vim /etc/ansible/ansible.cfg
host_key_checking = False ?修改注釋 ?不驗證
[root@centos7 ~]# ansible 192.168.27.130 -m ping -k 執行 測試 是否處于連接狀態
[root@centos7 ~]# vim /etc/ansible/hosts
[web]
192.168.27.130
[db]
192.168.27.120
[root@centos7 ~]# ansible web -m ping -k
基于k驗證
[root@centos7 ~]# ssh-keygen
[root@centos7 .ssh]# ssh-cop-id 192.168.27.120
[root@centos7 .ssh]# ssh-copy-id 192.168.27.130
[root@centos7 .ssh]# vim /etc/ansible/ansible.cfg
啟動日志功能
log_path = /var/log/ansible.log
Ansible不受ssh服務的影響,但是受network的影響
[root@centos7 ~]# ansible web -m command -a “ls /root” ??一root身份連接
Command命令模塊
[root@centos7 ~]# ansible db -m command -a “ls /app”
[root@centos7 ~]# ansible db -m command -a “removes=/etc/fstab ls /app” 不存在就不執行
[root@centos7 ~]# ansible db -m command -a “creates=/etc/fstab ls /app”存在就不執行
[root@centos7 ~]# ansible web -m command -a ‘chdir=/app ./f1.sh’ ??執行腳本
Ansible db -m shell -a ‘echo magedu | passwd –stdin test1’?修改用戶test1的密碼
Ansible db -m copy -a ‘src=/etc/selinux/config dest=/etc/selinux/ backup=yes’??遠程拷貝文件
Ansible db -m copy -a ‘src=/etc/fstab dest=/app/fstab2 mode=600 owner=test1’?改權限
Ansible db -m copy -a ‘src=/etc/sysconfig dest=/app/’?復制目錄
Ansible db -m copy -a ‘content=”de -h\nhostname\nls\n”?dest=/app/f1.sh’??將內容復制到文件
[root@centos7 ~]# ansible db -m fetch -a ‘src=/etc/passwd dest=/app/’ ?將遠程的文件抓到我的主機上
[root@centos7 ~]# ansible db -m file -a ‘path=/app/testfile state=touch mode=600 owner=test1’ ?創建空文件
[root@centos7 ~]# ansible db -m file -a ‘path=/app/dir2 state=directory’ 創建空文件夾
[root@centos7 ~]# ansible db -a ‘ls -l /app’ ??查看
Ansible db -m shell -a ‘ls -la /app’
[root@centos7 ~]# ansible 192.168.27.120 -m hostname -a ‘name=centos7-120.magedu.com’ ?該主機名
Ansible-vault encrypt file.yml ?加密文件
Ansible-vault view file.yml 看文件
Ansible-vault edit file.yml 編輯文件
Ansible-vault rekey file.yml ?更改口令
Ansible-vault decrypt file.yml ??解密文件
[root@centos7 ~]# ansible-galaxy install geerlingguy.nginx
[root@centos7 ~]# cd /etc/ansible/roles/geerlingguy.nginx/
[root@centos7 geerlingguy.nginx]# cd tasks
[root@centos7 tasks]# cat setup-RedHat.yml ????playbook
Playbook是由一個或多個play組成的列表
Play的主要功能在于將事先歸并為一組的主機裝扮成事先通過ansible中的task定義好的角色。從根本上來講,所謂task無非是調用ansible的一個module。將多個play組織在一個playbook中,既可以讓他們連同起來按事先編排的機制同唱一臺大戲
Playbook采用yaml語言編寫
YAML語法簡介
在單一檔案中,可用連續三個連字號(—)區分多個檔案。另外,還有選擇性的連續三個點號(…)用來表示檔案結尾
次行開始正常寫playbook的內容,一般建議寫明該playbook的功能
使用#號注釋代碼
縮進必須是統一的,不能空格和tab混用
縮進的級別也必須是一致的,同樣的縮進代表同樣的級別,程序判別配置的級別是通過縮進結合換行來實現的
YAML文件的內容和LINUX系統大小寫判斷方式保持一致,是區別大小寫的,k/v的值均需大小寫敏感
K/v的值可同行寫也可換行寫。同行使用:分隔
V可是個字符串,也可是另一個列表
一個完整的代碼塊功能需最少元素包括name:task
一個name只能包括一個task
YAML文件擴展通常為yml或yaml
Ansible all -m yum -a ‘name=vsftpd’?state=absent ???卸載服務
[root@centos7 ~]# vim test1.yml ??在遠程服務器上安裝一個軟件包
[root@centos7 ~]# ansible-playbook -C test1.yml ?測試運行
– hosts: db
remote_user: root
tasks:
– name: install package
yum: name=httpd state=present
– name: start service
service: name=httpd state=started enabled=yes
[root@centos7 ~]# ansible-playbook test1.yml ??運行腳本
[root@centos7 ~]# cp test1.yml test2.yml
[root@centos7 ~]# vim test2.yml
– hosts: db
remote_user: root
tasks:
– name: install package
yum: name=httpd state=present
– name: start service
service: name=httpd state=started enabled=yes
– hosts: web
remote_user: root
tasks:
– name: copy file
copy: src=/etc/fstab dest=/app/ owner=wang mode=600
– name: create user
user: name=test3 shell=/sbin/nologin system=yes
[root@centos7 ~]# ansible-playbook -C test2.yml ?測試運行
[root@centos7 ~]# vim httpd.yml ??裝包yum ?配置文件覆蓋 ???啟動服務
—
#
– hosts: web
remote_user: root
tasks:
– name: install httpd
yum: name=httpd
– name: copy config file
copy: src=/app/httpd.conf dest=/etc/httpd/conf/ backup=yes
– name: start httpd
service: name=httpd state=started enabled=yes
Ansible-playbook httpd.yml ?運行
—
#
– hosts: web
remote_user: root
tasks:
– name: install httpd
yum: name=httpd
– name: copy config file
copy: src=/app/httpd.conf dest=/etc/httpd/conf/ backup=yes
notify: restart httpd
– name: start httpd
service: name=httpd state=started enabled=yes
handlers:
– name: restart httpd
service: name=httpd state=restarted
第一次,執行照常執行;第二次執行時,notify 觸發handlers執行
Handlers
是task列表,這些task與前述的task并沒有本質上的不同,用于當關注的資源發生變化時,才會采取一定的操作
Notify這個action可用于在每個play的最后被觸發,這樣可以避免多次有改變發生時每次都執行指定的操作,僅僅在所有的變化發生完成后一次性地執行指定操作。在notify中列出的操作稱為handler,也即notify中調用handler中定義的操作
Ansible -playbook –tages copyconf -C httpd.yml ?從我的劇本中挑出tages代表的我的任務執行,其他不執行
[root@centos7 ~]# ansible all –list-hosts ????主機管理的所有列表
[root@centos7 ~]# ansible all -m setup ????列出主機對應的所用信息
[root@centos7 ~]# ansible all -m setup |grep version ??列出版本信息
Playbook中變量使用
變量名:僅能由字母,數字和下劃線組成,且只能以字母開頭
變量來源
- ansible setup facts 遠程主機的所有變量都可直接調用
- 在/etc/ansible/host中定義
普通變量:主機組中主機單獨定義,優先級高于公共變量
公共變量:針對主機組中所有主機定義統一變量
- 通過命令行指定變量,優先級最高 ?ansible-playbook -e varname=value
- 在playbook中定義
- Vars:
[root@centos7 ~]# vim var1.yml
—
– hosts: web
remote_user: root
tasks:
– name: install package
yum: name={{ pkname }}
[root@centos7 ~]# ansible-playbook -e pkname=vsftpd var1.yml ???????????可以傳一個值 ?
[root@centos7 ~]# ansible web -m shell -a ‘rpm -qa|grep vsftpd’ ?查找看是否裝了 vsftpd
[root@centos7 ~]# vim var1.yml
—
– hosts: web
remote_user: root
tasks:
– name: install package
yum: name={{ pkname }}
– name: copy file
copy: src=/app/{{ filename }} dest=/app/
[root@centos7 ~]# ansible-playbook -e “pkname=htop filename=httpd.conf” var1.yml ??使用多個變量
[root@centos7 ~]# ansible web -m yum -a ‘name=vsftpd,htop state=absent’ ??刪除兩個服務
[root@centos7 ~]# vim var2.yml ???在playbook中定義變量和使用變量
—
– hosts: web
remote_user: root
vars:
– username: user1
– groupname: group1
tasks:
– name: create group
group: name={{ groupname }}
– name: create user
user: name={{ username }} group={{ groupname }} home=/app/{{ username }}dir
[root@centos7 ~]# ansible-playbook var2.yml ??運行
[root@centos7 ~]# vim var3.yml ??創建文件
– hosts: web
remote_user: root
tasks:
– name: create file
file: name=/app/{{ ansible_hostname }}.txt state=touch
[root@centos7 ~]# ansible web -a ‘la /app/’ ?運行
[root@centos7 ~]# ansible web -a ‘ls /app/’ ?檢查文件是否生成
[root@centos7 ~]# vim /etc/ansible/hosts ?在配置文件中定義
[web]
192.168.27.130 http_port=85
[root@centos7 ~]# ansible web -m hostname -a ‘name=web{{http_port}}’
[root@centos7 ~]# hostname
web85
??普通變量
[root@centos7 ~]# vim /etc/ansible/hosts
[web]
192.168.27.130 http_port=85 hname=httpd
[root@centos7 ~]# vim var4.yml
– hosts: web
remote_user: root
tasks:
– name: set hostname
hostname: name={{hname}}-{{http_port}}
[root@centos7 ~]# ansible-playbook -C var4.yml
公共變量
[root@centos7 ~]# vim /etc/ansible/hosts
[web]
Hname=web
?
模板templates
文本文件,嵌套有腳本(使用模板編程語言編寫)
Jinjia2語言,使用字面量,有下面形式
字符串:使用單引號或雙引號
數字:整數,浮點數
復制功能
[root@centos7 templates]# vim tmphttpd.yml
– hosts: web
remote_user: root
tasks:
– name: template
template: src=httpd.conf.j2 dest=/app/httpd.conf
~
[root@centos7 templates]# ansible-playbook tmphttpd.yml
[root@centos7 templates]# cd /etc/ansible
[root@centos7 ansible]# cp /etc/nginx/nginx.conf templates/nginx.conf.j2
[root@centos7 ansible]# cp tmphttpd.yml tmpnginx.yml
[root@centos7 ansible]# vim tmpnginx.yml
– hosts: web
remote_user: root
tasks:
– name: install nginx
yum: name=nginx
– name: template
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
tags: instconf
– name: start service
service: name=nginx state=started
~
[root@centos7 ansible]# ansible-playbook -C tmpnginx.yml
[root@centos7 templates]# ansible web -m shell -a ‘ss -ntl|grep :80’ ?查看80 端口 在客戶端
實現centos6和centos7上的httpd安裝和啟動
[root@centos7 ansible]# vim tmphttpd.yml
– hosts: all
remote_user: root
tasks:
– name: install httpd
yum: name=httpd
– name: template 6
template: src=httpd-6.conf.j2 dest=/etc/httpd/conf/httpd.conf
when: ansible_distribution_major_version==”6″
– name: template 7
template: src=httpd-7.conf.j2 dest=/etc/httpd/conf/httpd.conf
when: ansible_distribution_major_version==”7″
– name: start service
service: name=httpd state=started
[root@centos7 ansible]# ansible-playbook -C tmphttpd.yml ?試運行
[root@centos7 ansible]# vim ?tmphttpd.yml ?使用when
– hosts: all
remote_user: root
tasks:
– name: install httpd
yum: name=httpd
– name: template 6
template: src=httpd-6.conf.j2 dest=/etc/httpd/conf/httpd.conf
when: ansible_distribution_major_version==”6″
– name: template 7
template: src=httpd-7.conf.j2 dest=/etc/httpd/conf/httpd.conf
when: ansible_distribution_major_version==”7″
– name: start service
service: name=httpd state=started
迭代:with_items
迭代:當有需要重復性執行的任務時,可以用迭代機制
對迭代項的引用,固定變量名為“item”
要在task中使用with_item給定要迭代的元素列表
列表格式 ?字符竄,字典
[root@centos7 ansible]# vim item1.yml
– hosts: web
remote_user: root
tasks:
– name: create servel user
user: name={{item}} group=root groups=wang,bin
with_items:
– itemuser1
– itemuser2
[root@centos7 ansible]# ansible-playbook -C item1.yml ??檢測執行
[root@centos7 templates]# ansible web -m shell -a ‘getent passwd |tail -n2’ ?查看生成的用戶
復制文件,迭代
[root@centos7 ansible]# vim item2.yml
– hosts: web
remote_user: root
tasks:
– name: copy conf files
copy: src={{item}} dest=/app/
with_items:
– /app/file1
– /app/file2
– /app/file3
– name: install packages
yum: name={{item}}
with_items:
– vsftpd
– memcached
– hping3
For循環
[root@centos7 ansible]# vim templates/for1.conf.j2
– hosts: web
remote_user: root
vars:
ports:
– 81
– 82
– 83
tasks:
– name: test for1
template: src=for1.conf.j2 dest=/app/for1.conf
[root@centos7 ansible]# vim templates/for1.conf.j2 ?寫模板
{%for port in ports %}
server {
listen port;
}
{%for port in ports %}
server {
listen {{port}};
}
{%endfor%}
[root@centos7 ansible]# ansible-playbook -C for1.yml ?運行
另一種方法
[root@centos7 ansible]# vim ?for2.yml
– hosts: web
remote_user: root
vars:
ports:
– listen_port: 81
– listen_port: 82
– listen_port: 83
tasks:
– name: test for2
template: src=for2.conf.j2 dest=/app/for2.conf
[root@centos7 ansible]# vim ?templates/for2.conf.j2
{%for port in ports %}
server {
listen {{port.listen_port}};
}
{%endfor%}
使用if 定義的時候使用該行,不定義的時候就嵌入代碼,
[root@centos7 ansible]# vim for4.yml
– hosts: web
remote_user: root
vars:
vhosts:
– web1:
port: 81
#name: web1.magedu.com
root: /app/webroot1
– web2:
port: 82
name: web2.magedu.com
root: /app/webroot2
– web3:
port: 83
#name: web3.magedu.com
root: /app/webroot3
tasks:
– name: test for1
template: src=for4.conf.j2 dest=/app/for4.conf
定義模板
[root@centos7 ansible]# vim templates/for4.conf.j2
{%for vhost in vhosts %}
server {
listen {{vhost.port}};
{%if vhost.name is defined%}
servername {{vhost.name}};
{%endif%}
rootdir {{vhost.root}};
}
{%endfor%}
roles
Ansible自1.2版本引入的新特性,用于層次性、結構化地組織playbook。Roles能夠根據層次型結構自動裝載變量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令即可。簡單來講,roles就是通過分別將變量、文件、任務、模板及處理器放置于單獨的目錄中,并可以便捷地include他們的一種機制。角色一般用于基于主機構建服務的場景中,但也可以是用于構建守候進程等場景中
復雜場景:建議使用roles,
變更指定主機或主機組
如命令不規范維護和傳承成本大
某些功能需多個playbook,通過include即可實現
實現文件復制的角色
[root@dnsclient roles]# mkdir filecopy/tasks -pv
[root@dnsclient roles]# mkdir filecopy/files
[root@dnsclient ansible]# vim roles/filecopy/tasks/main.yml
– name: file copy
copy: src=fstab dest=/app/
– name: file create
file: name=/app/testfile mode=600 state=touch
[root@dnsclient ansible]# vim filecopy-role.yml
– hosts: web
remote_user: root
roles:
– role: filecopy
[root@dnsclient ansible]# ansible-playbook filecopy-role.yml ?-C ??試運行
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/91295