# ansible的安裝方式有多種,比如編譯安裝、git方式和pip安裝等,這里使用yum方式安裝,此種方式需要現(xiàn)有epel源
[root@ansible ~]#yum install epel-release -y
[root@ansible ~]#yum install ansible -y
[root@ansible ~]#ansible --version
ansible 2.9.1
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Aug 7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
[defaults]
#inventory = /etc/ansible/hosts # 主機(jī)列表配置文件
#library = /usr/share/my_modules/ # 庫文件存放目錄
#remote_tmp = $HOME/.ansible/tmp #臨時py命令文件存放在遠(yuǎn)程主機(jī)目錄
#local_tmp = $HOME/.ansible/tmp # 本機(jī)的臨時命令執(zhí)行目錄
#forks = 5 # 默認(rèn)并發(fā)數(shù)
#sudo_user = root # 默認(rèn)sudo 用戶
#ask_sudo_pass = True #每次執(zhí)行ansible命令是否詢問ssh密碼
#ask_pass = True
#remote_port = 22
#host_key_checking = False # 檢查對應(yīng)服務(wù)器的host_key,建議取消注釋
#log_path=/var/log/ansible.log #日志文件,建議啟用
#module_name = command #默認(rèn)模塊,可以修改為shell模塊
ntp.aliyun.com
[webservers]
www1.abc.com:2222
www2.abc.com
[dbservers]
db1.abc.com
db2.abc.com
[websrvs]
www[1:100].example.com
[dbsrvs]
db-[a:f].example.com
[appsrvs]
10.0.0.[1:100]
ansible-doc [options] [module...]
-l, --list #列出可用模塊
-s, --snippet #顯示指定模塊的playbook片段
#列出所有模塊
ansible-doc -l
#查看指定模塊幫助用法
ansible-doc ping
#查看指定模塊幫助用法
ansible-doc -s ping
ansible <host-pattern> [-m module_name] [-a args]
--version #顯示版本
-m module #指定模塊,默認(rèn)為command
-v #詳細(xì)過程 –vv -vvv更詳細(xì)
--list-hosts #顯示主機(jī)列表,可簡寫 --list
-k, --ask-pass #提示輸入ssh連接密碼,默認(rèn)Key驗(yàn)證
-C, --check #檢查,并不執(zhí)行
-T, --timeout=TIMEOUT #執(zhí)行命令的超時時間,默認(rèn)10s
-u, --user=REMOTE_USER #執(zhí)行遠(yuǎn)程執(zhí)行的用戶
-b, --become #代替舊版的sudo 切換
--become-user=USERNAME #指定sudo的runas用戶,默認(rèn)為root
-K, --ask-become-pass #提示輸入sudo時的口令
用于匹配被控制的主機(jī)的列表
All :表示所有Inventory中的所有主機(jī)
# all
ansible all -m ping
# *:通配符
ansible "*" -m ping
ansible 192.168.1.* -m ping
# 或關(guān)系
ansible "websrvs:appsrvs" -m ping
ansible "192.168.1.1:192.168.1.2" -m ping
# 邏輯與
#在websrvs組并且在dbsrvs組中的主機(jī)
ansible "websrvs:&dbsrvs" –m ping
#邏輯非
#在websrvs組,但不在dbsrvs組中的主機(jī)
#注意:此處為單引號
ansible 'websrvs:!dbsrvs' –m ping
#綜合邏輯
ansible 'websrvs:dbsrvs:&appsrvs:!ftpsrvs' –m ping
#正則表達(dá)式
ansible "websrvs:&dbsrvs" –m ping
ansible "~(web|db).*\.abc\.com" –m ping
1.加載自己的配置文件 默認(rèn)/etc/ansible/ansible.cfg
2.加載自己對應(yīng)的模塊文件,如:command
3.通過ansible將模塊或命令生成對應(yīng)的臨時py文件,并將該文件傳輸至遠(yuǎn)程服務(wù)器的對應(yīng)執(zhí)行用戶$HOME/.ansible/tmp/ansible-tmp-數(shù)字/XXX.PY文件
4.給文件+x執(zhí)行
5.執(zhí)行并返回結(jié)果
6. 刪除臨時py文件,退出
[root@ansible ~]#grep -A 14 '\[colors\]' /etc/ansible/ansible.cfg
[colors]
#highlight = white
#verbose = blue
#warn = bright purple
#error = red
#debug = dark gray
#deprecate = purple
#skip = cyan
#unreachable = red
#ok = green
#changed = yellow
#diff_add = green
#diff_remove = red
#diff_lines = cyan
綠色:執(zhí)行成功并且不需要做改變的操作
黃色:執(zhí)行成功并且對目標(biāo)主機(jī)做變更
紅色:執(zhí)行失敗
#以zhangsan用戶執(zhí)行ping存活檢測
ansible all -m ping -u zhangsan -k
#以zhangsan sudo至root執(zhí)行ping存活檢測
ansible all -m ping -u zhangsan -k -b
#以zhangsan sudo至lisi用戶執(zhí)行ping存活檢測
ansible all -m ping -u zhangsan -k -b --become-user=lisi
#以zhangsan sudo至root用戶執(zhí)行l(wèi)s
ansible all -m command -u zhangsan -a 'ls /root' -b --become-user=root -k -K
#列出所有已安裝的galaxy
ansible-galaxy list
#安裝galaxy
ansible-galaxy install geerlingguy.redis
#刪除galaxy
ansible-galaxy remove geerlingguy.redis
ansible-playbook hello.yml
cat hello.yml
---
# hello world yml file
- hosts: websrvs
remote_user: root
tasks:
- name: hello world
command: /usr/bin/wall hello world
ansible-vault [create|decrypt|edit|encrypt|rekey|view]
ansible-vault encrypt hello.yml #加密
ansible-vault decrypt hello.yml #解密
ansible-vault view hello.yml #查看
ansible-vault edit hello.yml #編輯加密文件
ansible-vault rekey hello.yml #修改口令
ansible-vault create new.yml #創(chuàng)建新文件
可交互執(zhí)行命令,支持tab補(bǔ)全,ansible 2.0+新增
執(zhí)行用戶@當(dāng)前操作的主機(jī)組 (當(dāng)前組的主機(jī)數(shù)量)[f:并發(fā)數(shù)]$
設(shè)置并發(fā)數(shù): forks n 例如: forks 10
切換組: cd 主機(jī)組 例如: cd web
列出當(dāng)前組主機(jī)列表: list
列出所有的內(nèi)置命令: ?或help
[root@ansible ansible]#ansible-console
Welcome to the ansible console.
Type help or ? to list commands.
root@all (3)[f:5]$ list
192.168.7.71
192.168.7.72
192.168.7.73
root@all (3)[f:5]$ cd websrvs
root@websrvs (3)[f:5]$ list
192.168.7.71
192.168.7.72
192.168.7.73
root@websrvs (3)[f:5]$ yum name=httpd state=present
[root@ansible ~]#ansible websrvs -m command -a 'echo 123.com | passwd --stdin zhangsan'
[root@ansible ~]#ansible websrvs -a 'wall echo hello'
[root@ansible ~]#ansible websrvs -m shell -a 'echo 123.com | passwd --stdin zhangsan'
[root@ansible ~]#ansible websrvs -m script -a '/data/test.sh'
#如目標(biāo)存在,默認(rèn)覆蓋,此處指定先備份
[root@ansible ~]#ansible websrvs -m copy -a " src=/data/test.sh dest=/tmp/test2.sh owner=zhangsan mode=600 backup=yes"
#指定內(nèi)容,直接生成目標(biāo)文件
[root@ansible ~]#ansible websrvs -m copy -a "content='test content\n' dest=/tmp/test.txt"
#復(fù)制/etc/下的文件,不包括/etc/目錄自身
ansible srv -m copy -a " src=/etc/ dest=/backup"
# 注:如果目標(biāo)目錄不存在會自動創(chuàng)建
[root@ansible ~]#ansible websrvs -m fetch -a 'src=/tmp/test.txt dest=/data'
# 創(chuàng)建空文件
[root@ansible ~]#ansible websrvs -m file -a 'path=/data/test.sh state=touch owner=hechunping mode=755'
# 創(chuàng)建目錄
[root@ansible ~]#ansible websrvs -m file -a 'path=/data/dir1 state=directory owner=hechunping group=hechunping'
# 創(chuàng)建軟連接
[root@ansible ~]#ansible websrvs -m file -a 'src=/data/test.sh dest=/data/test.sh-link state=link'
實(shí)現(xiàn)有兩種用法:
1.將ansible主機(jī)上的壓縮包傳到遠(yuǎn)程主機(jī)后解壓縮至特定目錄,設(shè)置copy=yes
2.將遠(yuǎn)程主機(jī)上的某個壓縮包解壓縮到指定路徑下,設(shè)置copy=no
# 將ansible服務(wù)器上的壓縮文件解壓縮到被控機(jī)上
[root@ansible ~]#ansible websrvs -m unarchive -a 'src=/data/test.sh.tar.gz dest=/usr/local'
# 解壓被控機(jī)上的壓縮文件到它本地
[root@ansible ~]#ansible 192.168.7.72 -m unarchive -a 'src=/data/test.sh.tar.gz dest=/usr/local copy=no mode=0777'
# 解壓網(wǎng)絡(luò)壓縮文件到被控機(jī)
[root@ansible ~]#ansible websrvs -m unarchive -a 'src=https://nginx.org/download/nginx-1.14.2.tar.gz dest=/usr/local copy=no'
[root@ansible ~]#ansible websrvs -m archive -a 'path=/var/log/ dest=/data/log.tar.bz2 format=bz2 owner=hechunping mode=0600'
[root@ansible ~]#ansible 192.168.7.71 -m hostname -a 'name=web'
支持時間:minute,hour,day,month,weekday
# 創(chuàng)建任務(wù),周一至周五每天2:30執(zhí)行mysql_backup.sh腳本
ansible dbsrvs -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup
mysql" job=/root/mysql_backup.sh'
# 禁用計(jì)劃任務(wù)
ansible dbsrvs -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup
mysql" job=/root/mysql_backup.sh disabled=yes'
# 啟用計(jì)劃任務(wù)
ansible dbsrvs -m cron -a 'hour=2 minute=30 weekday=1-5 name="backup
mysql" job=/root/mysql_backup.sh disabled=no'
# 刪除任務(wù)
ansible dbsrvs -m cron -a "name='backup mysql' state=absent"
# 安裝
[root@ansible ~]#ansible websrvs -m yum -a 'name=redis state=present'
# 刪除
[root@ansible ~]#ansible websrvs -m yum -a 'name=redis state=absent'
# 啟動,并設(shè)置開機(jī)啟動
[root@ansible ~]#ansible websrvs -m service -a 'name=redis state=started enabled=yes'
# 停止
[root@ansible ~]#ansible websrvs -m service -a 'name=redis state=stopped'
# 重啟
[root@ansible ~]#ansible websrvs -m service -a 'name=redis state=restarted'
# 創(chuàng)建
[root@ansible ~]#ansible websrvs -m user -a 'name=user1 comment="test user" uid=2048 home=/app/user1 group=root'
# 刪除用戶及其及目錄等數(shù)據(jù)
[root@ansible ~]#ansible websrvs -m user -a 'name=user1 state=absent remove=yes'
# 創(chuàng)建
[root@ansible ~]#ansible websrvs -m group -a 'name=nginx gid=88 system=yes'
# 刪除
[root@ansible ~]#ansible websrvs -m group -a 'name=nginx state=absent'
ansible websrvs -m setup
ansible websrvs -m setup -a "filter=ansible_nodename"
ansible websrvs -m setup -a "filter=ansible_hostname"
ansible websrvs -m setup -a "filter=ansible_domain"
ansible websrvs -m setup -a "filter=ansible_memtotal_mb"
ansible websrvs -m setup -a "filter=ansible_memory_mb"
ansible websrvs -m setup -a "filter=ansible_memfree_mb"
ansible websrvs -m setup -a "filter=ansible_os_family"
ansible websrvs -m setup -a "filter=ansible_distribution_major_version"
ansible websrvs -m setup -a "filter=ansible_distribution_version"
ansible websrvs -m setup -a "filter=ansible_processor_vcpus"
ansible websrvs -m setup -a "filter=ansible_all_ipv4_addresses"
ansible websrvs -m setup -a "filter=ansible_architecture"
1.playbook 劇本是由一個或多個“play”組成的列表
2.play的主要功能在于將預(yù)定義的一組主機(jī),裝扮成事先通過ansible中的task定義好的角色。Task實(shí)際是調(diào)用ansible的一個module,將多個play組織在一個playbook中,即可以讓它們聯(lián)合起來,按事先編排的機(jī)制執(zhí)行預(yù)定義的動作
3.Playbook文件是采用YAML語言編寫的
one.example.com
one.example.com:two.example.com
192.168.1.50
192.168.1.*
Websrvs:dbsrvs #或者,兩個組的并集
Websrvs:&dbsrvs #與,兩個組的交集
websrvs:!dbsrvs #在websrvs組,但不在dbsrvs組
- hosts: websrvs:dbsrvs
- hosts: websrvs
remote_user: root
tasks:
- name: test connection
ping:
remote_user: zhangsan
sudo: yes #默認(rèn)sudo為root
sudo_user: lisi #sudo為lisi
1.play的主體部分是task list,task list中有一個或多個task,各個task按次序逐個在hosts中指定的所有主機(jī)上執(zhí)行,即在所有主機(jī)上完成第一個task后,再開始第二個task
2.task的目的是使用指定的參數(shù)執(zhí)行模塊,而在模塊參數(shù)中可以使用變量。模塊執(zhí)行是冪等的,這意味著多次執(zhí)行是安全的,因?yàn)槠浣Y(jié)果均一致
3.每個task都應(yīng)該有其name,用于playbook的執(zhí)行結(jié)果輸出,建議其內(nèi)容能清晰地描述任務(wù)執(zhí)行步驟。如果未提供name,則action的結(jié)果將用于輸出
1)action:module arguments
2)module:arguments (推薦使用)
---
- hosts: websrvs
remote_user: root
tasks:
- name: install httpd
yum: name=httpd
- name: start httpd
service: name=httpd state=started enabled=yes
某任務(wù)的狀態(tài)在運(yùn)行后為changed時,可通過"notify"通知給相應(yīng)的handlers
任務(wù)可以通過"tags"打標(biāo)簽,可在ansible-playbook命令上使用-t指定進(jìn)行調(diào)用
### 安裝Apache ###
#SHELL腳本實(shí)現(xiàn)
#!/bin/bash
yum install --quiet -y httpd
# 復(fù)制配置文件
cp /tmp/httpd.conf /etc/httpd/conf/httpd.conf
cp/tmp/vhosts.conf /etc/httpd/conf.d/
# 啟動Apache,并設(shè)置開機(jī)啟動
systemctl enable --now httpd
#Playbook實(shí)現(xiàn)
---
- hosts: websrvs
remote_user: root
tasks:
- name: "安裝Apache"
yum: name=httpd
- name: "復(fù)制配置文件"
copy: src=/tmp/httpd.conf dest=/etc/httpd/conf/
- name: "復(fù)制配置文件"
copy: src=/tmp/vhosts.conf dest=/etc/httpd/conf.d/
- name: "啟動Apache,并設(shè)置開機(jī)啟動"
service: name=httpd state=started enabled=yes
ansible-playbook <filename.yml> ... [options]
--check -C #只檢測可能會發(fā)生的改變,但不真正執(zhí)行操作
--list-hosts #列出運(yùn)行任務(wù)的主機(jī)
--list-tags #列出tag
--list-tasks #列出task
--limit 主機(jī)列表 #只針對主機(jī)列表中的主機(jī)執(zhí)行
-v -vv -vvv #顯示過程
ansible-playbook file.yml --check #只檢測
ansible-playbook file.yml
ansible-playbook file.yml --limit websrvs
#install_httpd.yml
---
- hosts: websrvs
remote_user: root
tasks:
- name: "安裝httpd"
yum: name=httpd state=present
- name: "復(fù)制httpd.conf配置文件"
copy: src=files/httpd.conf dest=/etc/httpd/conf
- name: "啟動httpd,并設(shè)置開機(jī)啟動"
service: name=httpd state=started enabled=yes
#remove_httpd.yml
---
- hosts: websrvs
remote_user: root
tasks:
- name: remove httpd package
yum: name=httpd state=absent
- name: remove apache user
user: name=apache state=absent
- name: remove data file
file: name=/etc/httpd state=absent
---
# 創(chuàng)建MySQL組和用戶
- hosts: dbsrvs
remote_user: root
tasks:
- {name: 創(chuàng)建組, group: name=mysql system=yes gid=306}
- name: 創(chuàng)建用戶
user: name=mysql shell=/sbin/nologin system=yes group=mysql uid=306 create_home=no
[root@ansible ~]#ll /data/ansible/files/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
-rw-r--r-- 1 root root 403177622 Dec 4 13:05 /data/ansible/files/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
[root@ansible ~]#cat /data/ansible/files/my.cnf
[mysqld]
socket=/tmp/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1
log-bin
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld_safe]
log-error=/var/log/mysqld.log
[root@ansible ~]#cat /data/ansible/install_mysql.yml
---
# install mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
- hosts: websrvs
remote_user: root
tasks:
- name: "安裝依賴包"
yum: name=libaio,perl-Data-Dumper,perl-Getopt-Long
- name: "創(chuàng)建mysql組"
group: name=mysql gid=306
- name: "創(chuàng)建mysql用戶"
user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes create_home=no home=/data/mysql
- name: "將本地二進(jìn)制安裝包解壓縮到目標(biāo)主機(jī)的/usr/local目錄下,并修改屬主、屬組為root"
unarchive: src=/data/ansible/files/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz dest=/usr/local owner=root group=root
- name: "為解壓縮的二進(jìn)制包目錄創(chuàng)建軟連接"
file: src=/usr/local/mysql-5.6.46-linux-glibc2.12-x86_64 dest=/usr/local/mysql state=link
- name: "初始化數(shù)據(jù)庫"
shell: chdir=/usr/local/mysql ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
tags: data
- name: "復(fù)制本地my.cnf配置文件到被控主機(jī)"
copy: src=/data/ansible/files/my.cnf dest=/etc/my.cnf
- name: "復(fù)制本地的mysql服務(wù)腳本到被控主機(jī)的/etc/init.d目錄下"
shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
- name: "啟動mysql服務(wù),并且設(shè)置開機(jī)啟動"
shell: /etc/init.d/mysqld start;chkconfig --add mysqld;chkconfig mysqld on
tags: service
- name: "將mysql的二進(jìn)制可執(zhí)行程序添加到PATH變量"
copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
- name: "執(zhí)行安全加固腳本"
script: /data/ansible/files/secure_mysql.sh
tags: script
Handlers本質(zhì)是task list ,其中的task與前述的task并沒有本質(zhì)上的不同,用于當(dāng)關(guān)注的資源發(fā)生變化時,才會采取一定的操作.Notify對應(yīng)的action可用于在每個play的最后被觸發(fā),這樣可避免多次有改變發(fā)生時每次都執(zhí)行指定的操作,僅在所有的變化發(fā)生完成后一次性地執(zhí)行指定操作。在notify中列出的操作稱為handler,也即notify中調(diào)用handler中定義的操作
---
- hosts: websrvs
remote_user: root
tasks:
- name: "安裝httpd"
yum: name=httpd state=present
- name: "復(fù)制httpd.conf配置文件,并且重啟httpd"
copy: src=files/httpd.conf dest=/etc/httpd/conf
notify: restart httpd
- name: "啟動httpd,并設(shè)置開機(jī)啟動"
service: name=httpd state=started enabled=yes
handlers:
- name: restart httpd
service: name=httpd state=restarted
[root@ansible ansible]#cat install_httpd.yml
---
- hosts: websrvs
remote_user: root
tasks:
- name: "安裝httpd"
yum: name=httpd state=present
- name: "復(fù)制httpd.conf配置文件,并且重啟httpd"
copy: src=files/httpd.conf dest=/etc/httpd/conf
tags: conf
- name: "啟動httpd,并設(shè)置開機(jī)啟動"
service: name=httpd state=started enabled=yes
tags: service
[root@ansible ansible]#ansible-playbook -t conf,service install_httpd.yml
key=value
http_port=80
通過{{ variable_name }} 調(diào)用變量,且變量名前后建議加空格,有時用"{{ variable_name }}"才生效
ansible-playbook -e varname=value
vars:
- var1: value1
- var2: value2
- hosts: all
vars_files:
- vars.yml
主機(jī)(普通)變量:主機(jī)組中主機(jī)單獨(dú)定義,優(yōu)先級高于公共變量
組(公共)變量:針對主機(jī)組中所有主機(jī)定義同一變量
[root@ansible ansible]#cat var.yml
---
# var.yml
- hosts: websrvs
remote_user: root
tasks:
- name: "創(chuàng)建日志文件"
file: name=/var/log/{{ ansible_fqdn }} state=touch
[root@ansible ansible]#ansible-playbook var.yml
[root@ansible ansible]#cat var.yml
---
# var.yml
- hosts: websrvs
remote_user: root
tasks:
- name: "安裝包變量"
yum: name={{ pkname }} state=absent
[root@ansible ansible]#ansible-playbook -e pkname=httpd var.yml
[root@ansible ansible]#cat var.yml
---
- hosts: websrvs
remote_user: root
vars:
- username: user1
- groupname: group1
tasks:
- name: create group
group: name={{ groupname }} state=present
- name: create user
user: name={{ username }} state=present
[root@ansible ansible]#ansible-playbook var.yml
可以在一個獨(dú)立的playbook文件中定義變量,在另一個playbook文件中引用變量文件中的變量,比playbook中定義的變量優(yōu)化級高
[root@ansible ansible]#cat vars.yml
---
# variables file
var1: vsftpd
var2: nginx
[root@ansible ansible]#cat var2.yml
---
- hosts: websrvs
remote_user: root
vars_files: vars.yml
tasks:
- name: "vsftpd日志文件"
file: name=/data/{{ var1 }}.log state=touch
- name: "nginx日志文件"
file: name=/data/{{ var2 }}.log state=touch
在inventory主機(jī)清單文件中為指定的主機(jī)定義變量以便于在playbook中使用
[websrvs]
www1.abc.com http_port=80
www2.abc.com http_port=8080
在inventory主機(jī)清單文件中賦予給指定組內(nèi)所有主機(jī)在playbook中可用的變量
[websrvs]
www1.abc.com
www2.abc.com
[websrvs:vars]
ntp_server=ntp.aliyun.com
[root@ansible ansible]#vim /etc/ansible/hosts
[websrvs]
192.168.7.71 hname=ansible
192.168.7.72 hname=web1
192.168.7.73 hname=web2
[websrvs:vars]
http_port=808
mark="-"
[root@ansible ansible]#ansible websrvs –m hostname –a 'name={{ hname }}{{ mark }}{{ http_port }}'
# 命令行指定變量
[root@ansible ansible]#ansible websrvs -e http_port=8000 –m hostname –a 'name={{ hname }}{{ mark }}{{ http_port }}'
jinja2語言使用字面量,有下面形式:
字符串:使用單引號或雙引號
數(shù)字:整數(shù),浮點(diǎn)數(shù)
列表:[item1,item2,...]
元組:(item1,item2,...)
字典:{key1:value1,key2:value2,...}
布爾型:true/false
算術(shù)運(yùn)算:+, -, *, /, //, %, **
比較操作:==, !=, >, >=, <, <=
邏輯運(yùn)算:and,or,not
流表達(dá)式:For,If,When
jinja2相關(guān)說明:
字面量:
表達(dá)式最簡單的形式就是字面量。字面量表示諸如字符串和數(shù)值的 Python 對象。如“Hello World”
雙引號或單引號中間的一切都是字符串。無論何時你需要在模板中使用一個字符串(比如函數(shù)調(diào)用、過濾器或只是包含或繼承一個模板的參數(shù)),如42,42.23
數(shù)值可以為整數(shù)和浮點(diǎn)數(shù)。如果有小數(shù)點(diǎn),則為浮點(diǎn)數(shù),否則為整數(shù)。在 Python 里, 42 和 42.0 是不一樣的
算術(shù)運(yùn)算:
Jinja 允許用計(jì)算值。支持下面的運(yùn)算符
+:把兩個對象加到一起。通常對象是素質(zhì),但是如果兩者是字符串或列表,你可以用這 種方式來銜接它們。無論如何這不是選的連接字符串的方式!連接字符串見 ~ 運(yùn)算符。 {{ 1 + 1 }} 等于 2
-:用第一個數(shù)減去第二個數(shù)。 {{ 3 - 2 }} 等于 1
/:對兩個數(shù)做除法。返回值會是一個浮點(diǎn)數(shù)。 {{ 1 / 2 }} 等于 {{ 0.5 }}
//:對兩個數(shù)做除法,返回整數(shù)商。 {{ 20 // 7 }} 等于 2
%:計(jì)算整數(shù)除法的余數(shù)。 {{ 11 % 7 }} 等于 4
*:用右邊的數(shù)乘左邊的操作數(shù)。 {{ 2 * 2 }} 會返回 4 。也可以用于重 復(fù)一個字符串多次。 {{ ‘=’ * 80 }}
會打印 80 個等號的橫條\
**:取左操作數(shù)的右操作數(shù)次冪。 {{ 2**3 }} 會返回 8
比較操作符
== 比較兩個對象是否相等
!= 比較兩個對象是否不等
> 如果左邊大于右邊,返回 true
>= 如果左邊大于等于右邊,返回 true
< 如果左邊小于右邊,返回 true
<= 如果左邊小于等于右邊,返回 true
邏輯運(yùn)算符
對于 if 語句,在 for 過濾或 if 表達(dá)式中,它可以用于聯(lián)合多個表達(dá)式
and 如果左操作數(shù)和右操作數(shù)同為真,返回 true
or 如果左操作數(shù)和右操作數(shù)有一個為真,返回 true
not 對一個表達(dá)式取反
(expr)表達(dá)式組
true / false true 永遠(yuǎn)是 true ,而 false 始終是 false
template功能:可以根據(jù)和參考模塊文件,動態(tài)生成相類似的配置文件
template文件必須存放于templates目錄下,且命名為 .j2 結(jié)尾
yaml/yml 文件需和templates目錄平級,目錄結(jié)構(gòu)如下:
./
├── temnginx.yml
└── templates
└── nginx.conf.j2
# 準(zhǔn)備templates/nginx.conf.j2文件
[root@ansible ansible]#cat temnginx.yml
---
- hosts: websrvs
remote_user: root
tasks:
- name: "利用template模板文件配置被控主機(jī)"
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
[root@ansible ansible]#ansible-playbook temnginx.yml
#修改templates/nginx.conf.j2文件
[root@ansible ansible]#vim templates/nginx.conf.j2
worker_processes {{ ansible_processor_vcpus }};
[root@ansible ansible]#cat temnginx.yml
---
- hosts: websrvs
remote_user: root
tasks:
- name: "利用template模板文件配置被控主機(jī)"
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
[root@ansible ansible]#ansible-playbook temnginx.yml
[root@ansible ansible]#vim templates/nginx.conf.j2
worker_processes {{ ansible_processor_vcpus * 2}};
worker_processes {{ ansible_processor_vcpus + 2}};
[root@ansible ansible]#cat temnginx1.yml
---
- hosts: websrvs
remote_user: root
vars:
nginx_vhosts:
- listen: 8081
server_name: www.hechunping.tech
tasks:
- name: "template模板文件配置被控主機(jī)"
template: src=nginx.conf1.j2 dest=/data/nginx.conf
[root@ansible ansible]#cat templates/nginx.conf1.j2
{% for vhost in nginx_vhosts %}
server {
listen {{ vhost.listen }};
server_name {{ vhost.server_name }};
}
{% endfor %}
# 生成結(jié)果
[root@ansible ansible]#ansible-playbook temnginx1.yml
[root@ansible ansible]#cat /data/nginx.conf
server {
listen 8081;
server_name www.hechunping.tech;
}
[root@ansible ansible]#cat temnginx3.yml
---
- hosts: websrvs
remote_user: root
vars:
nginx_vhosts:
- listen: 8081
server_name: "web1.hechunping.tech"
root: "/var/www/nginx/web1"
- listen: 8082
server_name: "web2.hechunping.tech"
root: "/var/www/nginx/web2"
- listen: 8083
server_name: "web3.hechunping.tech"
root: "/var/www/nginx/web3"
tasks:
- name: "template模板文件配置被控主機(jī)"
template: src=nginx.conf3.j2 dest=/data/nginx2.conf
[root@ansible ansible]#cat templates/nginx.conf3.j2
{% for vhost in nginx_vhosts %}
server {
listen {{ vhost.listen }};
server_name {{ vhost.server_name }};
root {{ vhost.root }};
}
{% endfor %}
# 生成結(jié)果
[root@ansible ansible]#ansible-playbook temnginx3.yml
[root@ansible ansible]#cat /data/nginx2.conf
server {
listen 8081;
server_name web1.hechunping.tech;
root /var/www/nginx/web1;
}
server {
listen 8082;
server_name web2.hechunping.tech;
root /var/www/nginx/web2;
}
server {
listen 8083;
server_name web3.hechunping.tech;
root /var/www/nginx/web3;
}
[root@ansible ansible]#cat temnginx4.yml
---
- hosts: websrvs
remote_user: root
vars:
nginx_vhosts:
- web1:
listen: 8080
root: "/var/www/nginx/web1"
- web2:
listen: 8080
server_name: "web2.hechunping.tech"
root: "/var/www/nginx/web2"
tasks:
- name: "template模板文件配置被控主機(jī)"
template: src=nginx.conf2.j2 dest=/data/nginx3.conf
[root@ansible ansible]#cat templates/nginx.conf2.j2
{% for vhost in nginx_vhosts %}
server{
listen {{ vhost.listen }};
{% if vhost.server_name is defined %}
server_name {{ vhost.server_name }};
{% endif %}
root {{ vhost.root }}
}
{% endfor %}
#生成結(jié)果
[root@ansible ansible]#ansible-playbook temnginx4.yml
[root@ansible ansible]#cat /data/nginx3.conf
server{
listen 8080;
root /var/www/nginx/web1
}
server{
listen 8080;
server_name web2.hechunping.tech;
root /var/www/nginx/web2
}
[root@ansible ansible]#cat when.yml
---
- hosts: websrvs
remote_user: root
tasks:
- name: "打印hello redhat"
command: /usr/bin/wall hello redhat
when: ansible_os_family == "RedHat"
迭代:當(dāng)有需要重復(fù)性執(zhí)行的任務(wù)時,可以使用迭代機(jī)制
對迭代項(xiàng)的引用,固定變量名為"item"
要在task中使用with_items給定要迭代的元素列表
---
- hosts: 192.168.7.71
remote_user: root
tasks:
- name: "備份文件"
copy: src=/data/{{ item }} dest=/backup/{{ item }}
with_items:
- nginx.conf
- nginx2.conf
#上面語句的功能等同于下面的語句
- name: "備份/data/nginx.conf文件到/backup目錄"
copy: src=/data/nginx.conf dest=/backup
- name: "備份/data/nginx2.conf文件到/backup目錄"
copy: src=/data/nginx2.conf dest=/backup
---
- hosts: websrvs
remote_user: root
tasks:
- name: "創(chuàng)建組"
group: name={{ item }} state=present
with_items:
- a1
- a2
- a3
- name: "創(chuàng)建用戶"
user: name={{ item.name }} group={{ item.group }} state=present
with_items:
- { name: 'a1',group: 'a1' }
- { name: 'a2',group: 'a2' }
- { name: 'a3',group: 'a3' }
角色是ansible自1.2版本引入的新特性,用于層次性、結(jié)構(gòu)化地組織playbook。roles能夠根據(jù)層次型結(jié)構(gòu)自動裝載變量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令即可。簡單來講,roles就是通過分別將變量、文件、任務(wù)、模板及處理器放置于單獨(dú)的目錄中,并可以便捷地include它們的一種機(jī)制。角色一般用于基于主機(jī)構(gòu)建服務(wù)的場景中,但也可以是用于構(gòu)建守護(hù)進(jìn)程等場景中
roles
├── httpd
├── mysql
├── nginx
└── redis
playbook.yml
roles/
project/
tasks/
handlers/
files/
templates/
vars/
defaults/
meta/
/roles/project/ :項(xiàng)目名稱,有以下子目錄
files/ :存放由copy或script模塊等調(diào)用的文件
templates/:template模塊查找所需要模板文件的目錄
tasks/:定義task,role的基本元素,至少應(yīng)該包含一個名為main.yml的文件;其它的文件需要在此文件中通過include進(jìn)行包含
handlers/:至少應(yīng)該包含一個名為main.yml的文件;其它的文件需要在此文件中通過include進(jìn)行包含
vars/:定義變量,至少應(yīng)該包含一個名為main.yml的文件;其它的文件需要在此文件中通過include進(jìn)行包含
meta/:定義當(dāng)前角色的特殊設(shè)定及其依賴關(guān)系,至少應(yīng)該包含一個名為main.yml的文件,其它文件需在此文件中通過include進(jìn)行包含
default/:設(shè)定默認(rèn)變量時使用此目錄中的main.yml文件
(1) 創(chuàng)建以roles命名的目錄
(2) 在roles目錄中分別創(chuàng)建以各角色名稱命名的目錄,如webservers等
(3) 在每個角色命名的目錄中分別創(chuàng)建files、handlers、meta、tasks、templates和vars目錄;用不到的目錄可以創(chuàng)建為空目錄,也可以不創(chuàng)建
(4) 在playbook文件中,調(diào)用各角色
role_httpd.yml
roles/
├── httpd
│?? ├── files
│?? │?? ├── httpd.conf
│?? │?? └── index.html
│?? ├── handlers
│?? │?? └── main.yml
│?? └── tasks
│?? ├── config.yml
│?? ├── index.yml
│?? ├── install.yml
│?? ├── main.yml
│?? └── service.yml
---
- hosts: websrvs
remote_user: root
roles:
- mysql
- memcached
- nginx
---
- hosts: all
remote_user: root
roles:
- mysql
- { role: nginx, username: nginx }
---
- hosts: all
remote_user: root
roles:
- { role: nginx, username: nginx, when: ansible_distribution_major_version == '7' }
#nginx-role.yml
---
- hosts: websrvs
remote_user: root
roles:
- { role: nginx ,tags: [ 'nginx','web' ], when: ansible_distribution_major_version == "6" }
- { role: httpd ,tags: [ 'httpd','web' ] }
- { role: mysql ,tags: [ 'mysql','db' ] }
- { role: mariadb ,tags: [ 'mariadb','db' ] }
[root@ansible ansible]#ansible-playbook --tags="nginx,httpd,mysql" nginx-role.yml
[root@ansible ansible]#pwd
/data/ansible
[root@ansible ansible]#ls roles/httpd/
files handlers tasks
# 創(chuàng)建角色
[root@ansible ansible]#cd roles/httpd/
[root@ansible httpd]#cat tasks/main.yml
- include: install.yml
- include: config.yml
- include: index.yml
- include: service.yml
[root@ansible httpd]#cat tasks/install.yml
- name : install httpd package
yum:
name: httpd
[root@ansible httpd]#cat tasks/config.yml
- name: config file
copy:
src: httpd.conf
dest: /etc/httpd/conf
backup: yes
[root@ansible httpd]#cat tasks/index.yml
- name: index.html
copy:
src: index.html
dest: /var/www/html
[root@ansible httpd]#cat tasks/service.yml
- name: start service
service:
name: httpd
state: started
enabled: yes
[root@ansible httpd]#cat handlers/main.yml
- name: restart
service:
name: httpd
state: restarted
# 在files目錄下準(zhǔn)備兩個文件
[root@ansible httpd]#ls files/
httpd.conf index.html
[root@ansible httpd]#tree ./
./
├── files
│?? ├── httpd.conf
│?? └── index.html
├── handlers
│?? └── main.yml
└── tasks
├── config.yml
├── index.yml
├── install.yml
├── main.yml
└── service.yml
3 directories, 8 files
# 在playbook中調(diào)用角色
[root@ansible httpd]#cat ../../role_httpd.yml
---
# httpd role
- hosts: appsrvs
remote_user: root
roles:
- role: httpd
# 運(yùn)行playbook
[root@ansible httpd]#ansible-playbook ../../role_httpd.yml
[root@ansible ~]#ls /data/ansible/roles/nginx/
files handlers tasks templates vars
# 創(chuàng)建task文件
[root@ansible ~]#cd /data/ansible/roles/nginx/
[root@ansible nginx]#cat tasks/main.yml
- include: install.yml
- include: config.yml
- include: file.yml
- include: service.yml
[root@ansible nginx]#cat tasks/install.yml
- name: install
yum: name=nginx
[root@ansible nginx]#cat tasks/config.yml
- name: config file for centos7
template:
src: nginx7.conf.j2
dest: /etc/nginx/nginx.conf
when: ansible_distribution_major_version=="7"
notify: restart
- name: config file for centos8
template:
src: nginx8.conf.j2
dest: /etc/nginx/nginx.conf
when: ansible_distribution_major_version=="8"
notify: restart
[root@ansible nginx]#cat tasks/file.yml
- name: index.html
copy:
src: index.html
dest: /usr/share/nginx/html
[root@ansible nginx]#cat tasks/service.yml
- name: start service
service:
name: nginx
state: started
enabled: yes
# 創(chuàng)建handlers文件
[root@ansible nginx]#cat handlers/main.yml
- name: restart
service:
name: nginx
state: restarted
# 創(chuàng)建兩個template文件
[root@ansible nginx]#cat templates/nginx7.conf.j2
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user {{user}}; #修改此行
worker_processes {{ansible_processor_vcpus**2}}; #修改此行
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
[root@ansible nginx]#cat templates/nginx8.conf.j2
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes {{ansible_processor_vcpus+2}}; #修改此行
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
# 創(chuàng)建變量文件
[root@ansible nginx]#cat vars/main.yml
user: daemon
# 在files目錄下創(chuàng)建index.html文件
[root@ansible nginx]#cat files/index.html
<h2>hello nginx </h2>
# 目錄結(jié)構(gòu)如下
[root@ansible nginx]#tree ./
./
├── files
│?? └── index.html
├── handlers
│?? └── main.yml
├── tasks
│?? ├── config.yml
│?? ├── file.yml
│?? ├── install.yml
│?? ├── main.yml
│?? └── service.yml
├── templates
│?? ├── nginx7.conf.j2
│?? └── nginx8.conf.j2
└── vars
└── main.yml
5 directories, 10 files
# 在playbook中調(diào)用角色
[root@ansible nginx]#cat ../../role_nginx.yml
---
# nginx role
- hosts: appsrvs
roles:
- role: nginx
# 運(yùn)行playbook
[root@ansible nginx]#ansible-playbook /data/ansible/role_nginx.yml
[root@ansible nginx]#cat /data/ansible/role_httpd_nginx.yml
---
- hosts: appsrvs
roles:
- { role: httpd,tags: [httpd,web], when: ansible_distribution_major_version=="7" }
- { role: nginx,tags: [nginx,web], when:
ansible_distribution_major_version=="8" }
[root@ansible nginx]#ansible-playbook -t nginx /data/ansible/role_httpd_nginx.yml
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當(dāng)前題目:運(yùn)維自動化工具之Ansible-創(chuàng)新互聯(lián)
URL分享:http://aaarwkj.com/article0/cchsio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、電子商務(wù)、服務(wù)器托管、軟件開發(fā)、動態(tài)網(wǎng)站、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容