Ansible是一個綜合的強大的管理工具,他能夠對多臺主機安裝操做系統,併爲這些主機安裝不一樣的應用程序,也能夠通知指揮這些主機完成不一樣的任務.查看多臺主機的各類信息的狀態等,ansible均可以經過模塊的方式來完成。node
Ansible 在管理節點將 Ansible 模塊經過 SSH 協議(或者 Kerberos、LDAP)推送到被管理端執 行,執行完以後自動刪除,可使用 SVN 等來管理自定義模塊及編排。python
Ansible的安裝方式有不少種,經常使用的安裝方法是基於yum或者源碼,若是是基於yum安裝,須要配置epel源,而後直接執行下面命令安裝mysql
yum -y install ansible
Ansible 經過讀取默認的主機清單配置/etc/ansible/hosts,能夠同時鏈接到多個遠程主機上執行任務, 默認路徑能夠經過修改 ansible.cfg 的 hostfile 參數指定路徑。linux
對於/etc/ansible/hosts最簡單的定義格式像下面:nginx
# 中括號中的名字表明組名,能夠根據本身的需求將龐大的主機分紅具備標識的組,如上面分了兩個組webservers和dbservers組; # 主機(hosts)部分可使用域名、主機名、IP地址表示;固然使用前二者時,也須要主機能反解析到相應的IP地址,通常此類配置中多使用IP地址; mail.yanruogu.com [webservers] web1.yanruogu.com web2.yanruogu.com [dbservers] db1.yanruogu.com db2.yanruogu.com
若是某些主機的SSH運行在自定義的端口上,ansible使用Paramiko進行ssh鏈接時,不會使用你SSH配置文件中列出的端口,可是若是修改ansible使用openssh進行ssh鏈接時將會使用:git
192.168.1.1:3091
假如你想要爲某些靜態IP設置一些別名,能夠這樣作:web
# web1別名就指代了IP爲192.168.1.2,ssh鏈接端口爲3333的主機 web1 ansible_ssh_port = 3333 ansible_ssh_host = 192.168.1.2
# 下面指定了從web1到web50,webservers組共計50臺主機;databases組有db-a到db-f共6臺主機 [webservers] www[01:50].yanruogu.com [databases] db-[a:f].yanruogu.com
如下是Hosts部分中常常用到的變量部分:算法
ansible_ssh_host #用於指定被管理的主機的真實IP ansible_ssh_port #用於指定鏈接到被管理主機的ssh端口號,默認是22 ansible_ssh_user #ssh鏈接時默認使用的用戶名 ansible_ssh_pass #ssh鏈接時的密碼 ansible_sudo_pass #使用sudo鏈接用戶時的密碼 ansible_sudo_exec #若是sudo命令不在默認路徑,須要指定sudo命令路徑 ansible_ssh_private_key_file #祕鑰文件路徑,祕鑰文件若是不想使用ssh-agent管理時可使用此選項 ansible_shell_type #目標系統的shell的類型,默認sh ansible_connection #SSH 鏈接的類型: local , ssh , paramiko,在 ansible 1.2 以前默認是 paramiko ,後來智能選擇,優先使用基於 ControlPersist 的 ssh (支持的前提) ansible_python_interpreter #用來指定python解釋器的路徑,默認爲/usr/bin/python 一樣能夠指定ruby 、perl 的路徑 ansible_*_interpreter #其餘解釋器路徑,用法與ansible_python_interpreter相似,這裏"*"能夠是ruby或才perl等其餘語言
下面是一個簡單的示例:sql
# 指定了三臺主機,三臺主機的用密碼分別是P@ssw0rd、12345六、45789,指定的ssh鏈接的用戶名分別爲root、breeze、bernie,ssh 端口分別爲2二、2二、3055 ,這樣在ansible命令執行的時候就不用再指令用戶和密碼等了 [test] 192.168.1.1 ansible_ssh_user=root ansible_ssh_pass='P@ssw0rd' 192.168.1.2 ansible_ssh_user=breeze ansible_ssh_pass='123456' 192.168.1.3 ansible_ssh_user=bernie ansible_ssh_port=3055 ansible_ssh_pass='456789'
變量也能夠經過組名,應用到組內的全部成員:shell
# test組中包含兩臺主機,經過對test組指定vars變動,相應的host1和host2至關於相應的指定了ntp_server和proxy變量參數值 [test] host1 host2 [test:vars] ntp_server=192.168.1.10 proxy=192.168.1.20
下面是一個示例,指定了一個武漢組有web一、web2;隨州組有web三、web4主機;又指定了一個湖北組,同時包含武漢和隨州;同時爲該組內的全部主機指定了2個vars變量。設定了一個組中國組,包含湖北、湖南。
[wuhan] web1 web2 [suizhou] web4 web3 [hubei:children] wuhan suizhou [hubei:vars] ntp_server=192.168.1.10 zabbix_server=192.168.1.10 [china:children] hubei hunan
注:vars變量在ansible ad-hoc部分中基本用不到,主要用在ansible-playbook中。
把Patterns 直接理解爲正則實際是不徹底準確的,正常的理解爲patterns意味着在ansible中管理哪些主機,也能夠理解爲,要與哪臺主機進行通訊。在探討這個問題以前咱們先看下ansible的用法:
ansible <pattern_goes_here> -m <module_name> -a <arguments>
直接上一個示例:
# 對webservers 組或主機重啓httpd服務 ,其中webservers 就是Pattern部分 ansible webservers -m service -a "name=httpd state=restarted"
之因此上面說Pattern(模式)能夠理解爲正則,主要針對下面常常用到的用法而言的:
利用通配符還能夠指定一組具備規則特徵的主機或主機名,冒號表示or---邏輯或:
web1.yanruogu.com
web1.yanruogu.com:web2.yanruogu.com
192.168.1.1
192.168.1.*
固然,這裏的*通配符也能夠用在前面,如:
*.yanruogu.com *.com webservers1[0] #表示匹配 webservers1 組的第 1 個主機 webservers1[0:25] #表示匹配 webservers1 組的第 1 個到第 25 個主機(官網文檔是":"表示範圍,測試發現應該使用"-",注意不要和匹配多個主機組混淆)
Ansible默認安裝好後有一個配置文件/etc/ansible/ansible.cfg,該配置文件中定義了ansible的主機的默認配置部分,如默認是否須要輸入密碼、是否開啓sudo認證、action_plugins插件的位置、hosts主機組的位置、是否開啓log功能、默認端口、key文件位置等等。
具體以下:
[defaults] # some basic default values... hostfile = /etc/ansible/hosts \\指定默認hosts配置的位置 # library_path = /usr/share/my_modules/ remote_tmp = $HOME/.ansible/tmp pattern = * forks = 5 poll_interval = 15 sudo_user = root \\遠程sudo用戶 #ask_sudo_pass = True \\每次執行ansible命令是否詢問ssh密碼 #ask_pass = True \\每次執行ansible命令時是否詢問sudo密碼 transport = smart remote_port = 22 module_lang = C gathering = implicit host_key_checking = False \\關閉第一次使用ansible鏈接客戶端是輸入命令提示 log_path = /var/log/ansible.log \\須要時能夠自行添加。chown -R root:root ansible.log system_warnings = False \\關閉運行ansible時系統的提示信息,通常爲提示升級 # set plugin path directories here, separate with colons action_plugins = /usr/share/ansible_plugins/action_plugins callback_plugins = /usr/share/ansible_plugins/callback_plugins connection_plugins = /usr/share/ansible_plugins/connection_plugins lookup_plugins = /usr/share/ansible_plugins/lookup_plugins vars_plugins = /usr/share/ansible_plugins/vars_plugins filter_plugins = /usr/share/ansible_plugins/filter_plugins fact_caching = memory [accelerate] accelerate_port = 5099 accelerate_timeout = 30 accelerate_connect_timeout = 5.0 # The daemon timeout is measured in minutes. This time is measured # from the last activity to the accelerate daemon. accelerate_daemon_timeout = 30
若是在對以前未鏈接的主機進行連結時報錯以下:
ansible test -a 'uptime' 192.168.1.1| FAILED =>Using a SSH password instead of a key is not possible because HostKey checking is enabled and sshpass does not support this.Please add this host's fingerprint to your known_hosts file to manage this host. 192.168.1.2 | FAILED => Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host.
是因爲在本機的文件中並有串,ssh第一次鏈接的時候通常會提示輸入yes 進行確認爲將key字符串加入到文件中。~/.ssh/known_hostsfingerprint key~/.ssh/known_hosts
方法1:
在進行ssh鏈接時,可使用-o參數將StrictHostKeyChecking設置爲no,使用ssh鏈接時避免首次鏈接時讓輸入yes/no部分的提示。經過查看ansible.cfg配置文件,發現以下行:
[ssh_connection] # ssh arguments to use # Leaving off ControlPersist will result in poor performance, so use # paramiko on older platforms rather than removing it #ssh_args = -o ControlMaster=auto -o ControlPersist=60s
能夠啓用ssh_args 部分,使用下面的配置,避免上面出現的錯誤:
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no
方法2:
在ansible.cfg配置文件中,也會找到以下配置:
# uncomment this to disable SSH key host checking host_key_checking = False
默認host_key_checking部分是註釋的,經過找開該行的註釋,一樣也能夠實現跳過ssh 首次鏈接提示驗證部分。但在實際測試中,彷佛並無效果
默認ansible 執行的時候,並不會輸出日誌到文件,不過在ansible.cfg 配置文件中有以下行:
log_path = /var/log/ansible.log
默認log_path這行是註釋的,打開該行的註釋,全部的命令執行後,都會將日誌輸出到/var/log/ansible.log
文件。
ansible -i /etc/ansible/hosts -u root -m command -a "ls /mnt"
Ad-Hoc 是指ansible下臨時執行的一條命令,而且不須要保存的命令,對於複雜的命令會使用playbook。Ad-hoc的執行依賴於模塊,ansible官方提供了大量的模塊。 如:command、raw、shell、file、cron等,具體能夠經過ansible-doc -l 進行查看 。可使用ansible-doc -s module來查看某個模塊的參數,也可使用ansible-doc help module來查看該模塊更詳細的信息。
一個ad-hoc命令的執行,須要按如下格式進行執行:
ansible 主機或組-m 模塊名-a '模塊參數' ansible參數
命令執行模塊包含以下 四個模塊:
raw模塊和comand、shell 模塊不一樣的是其沒有chdir、creates、removes參數,chdir參數的做用就是先切到chdir指定的目錄後,再執行後面的命令,這在後面不少模塊裏都會有該參數 。
command模塊包含以下選項:
chdir示例:
# 三個命令都會返回執行成功的狀態。不過實際上只有前兩個文件會被建立成功。使用raw模塊的執行的結果文件事實上也被正常建立了,不過不是在chdir指定的目錄,而是在當前執行用戶的家目錄 ansible 192.168.1.1 -m command -a 'chdir=/tmp/test.txt touch test.file' ansible 192.168.1.1 -m shell -a 'chdir=/tmp/test.txt touch test2.file' ansible 192.168.1.1 -m raw -a 'chdir=/tmp/text.txt touch test3.file'
creates與removes示例:
ansible 192.168.1.1 -a 'creates=/tmp/server.txt uptime' #當/tmp/server.txt文件存在時,則不執行uptime指令 ansible 192.168.1.1 -a 'removes=/tmp/server.txt uptime' #當/tmp/server.txt文件不存在時,則不執行uptime指令
示例:
#要執行的腳本文件script.sh內容以下: #/bin/bash ifconfig df -hT # 執行ansible指令: ansible 10.212.52.252 -m script -a 'script.sh' |egrep '>>|stdout'
測試主機是不是通的,用法很簡單,不涉及參數:
ansible test -m ping
setup模塊,主要用於獲取主機信息,在playbooks裏常常會用到的一個參數gather_facts就與該模塊相關。setup模塊下常用的一個參數是filter參數,具體使用示例以下:
# 查看主機內存信息 ansible 10.212.52.252 -m setup -a 'filter=ansible_*_mb' # 查看地接口爲eth0-2的網卡信息 ansible 10.212.52.252 -m setup -a 'filter=ansible_eth[0-2]' # 將全部主機的信息輸入到/tmp/facts目錄下,每臺主機的信息輸入到主機名文件中(/etc/ansible/hosts裏的主機名) ansible all -m setup --tree /tmp/facts
file模塊主要用於遠程主機上的文件操做,file模塊包含以下選項:
使用示例:
ansible test -m file -a "src=/etc/fstab dest=/tmp/fstab state=link" ansible test -m file -a "path=/tmp/fstab state=absent" ansible test -m file -a "path=/tmp/test state=touch"
複製文件到遠程主機,copy模塊包含以下選項:
示例以下:
ansible test -m copy -a "src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644" ansible test -m copy -a "src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes" ansible test -m copy -a "src=/mine/sudoers dest=/etc/sudoers validate='visudo -cf %s'"
用於管理服務
該模塊包含以下選項:
使用示例:
ansible test -m service -a "name=httpd state=started enabled=yes" asnible test -m service -a "name=foo pattern=/usr/bin/foo state=started" ansible test -m service -a "name=network state=restarted args=eth0"
用於管理計劃任務
包含以下選項:
示例:
ansible test -m cron -a 'name="a job for reboot" special_time=reboot job="/some/job.sh"' ansible test -m cron -a 'name="yum autoupdate" weekday="2" minute=0 hour=12 user="root"' ansible test -m cron -a 'backup="True" name="test" minute="0" hour="5,2" job="ls -alh > /dev/null"' ansilbe test -m cron -a 'cron_file=ansible_yum-autoupdate state=absent'
使用yum包管理器來管理軟件包,其選項有:
config_file
:yum的配置文件disable_gpg_check
:關閉gpg_checkdisablerepo
:不啓用某個源enablerepo
:啓用某個源name
:要進行操做的軟件包的名字,也能夠傳遞一個url或者一個本地的rpm包的路徑state
:狀態(present,absent,latest)示例以下:
ansible test -m yum -a 'name=httpd state=latest' ansible test -m yum -a 'name="@Development tools" state=present' ansible test -m yum -a 'name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present'
user模塊是請求的是useradd, userdel, usermod三個指令,goup模塊請求的是groupadd, groupdel, groupmod 三個指令。
使用示例:
user: name=johnd comment="John Doe" uid=1040 group=admin user: name=james shell=/bin/bash groups=admins,developers append=yes user: name=johnd state=absent remove=yes user: name=james18 shell=/bin/zsh groups=developers expires=1422403387 #生成密鑰時,只會生成公鑰文件和私鑰文件,和直接使用ssh-keygen指令效果相同,不會生成authorized_keys文件 user: name=test generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa
須要說明的是,在指定password參數時,不能使用明文密碼,由於後面這一串密碼會被直接傳送到被管理主機的/etc/shadow文件中,因此須要先將密碼字符串進行加密處理。而後將獲得的字符串放到password中便可。
echo "123456" | openssl passwd -1 -salt $(< /dev/urandom tr -dc '[:alnum:]' | head -c 32) -stdin $1$4P4PlFuE$ur9ObJiT5iHNrb9QnjaIB0 #使用上面的密碼建立用戶 ansible all -m user -a 'name=foo password="$1$4P4PlFuE$ur9ObJiT5iHNrb9QnjaIB0"'
不一樣的發行版默認使用的加密方式可能會有區別,具體能夠查看/etc/login.defs文件確認,centos 6.5版本使用的是SHA512加密算法。
group: name=somegroup state=present
使用rsync同步文件,其參數以下:
使用示例:
src=some/relative/path dest=/some/absolute/path rsync_path="sudo rsync" src=some/relative/path dest=/some/absolute/path archive=no links=yes src=some/relative/path dest=/some/absolute/path checksum=yes times=no src=/tmp/helloworld dest=/var/www/helloword rsync_opts=--no-motd,--exclude=.git mode=pull
在塊設備上建立文件系統
經常使用選項:
示例:
ansible test -m filesystem -a 'fstype=ext2 dev=/dev/sdb1 force=yes' ansible test -m filesystem -a 'fstype=ext4 dev=/dev/sdb1 opts="-cc"'
配置掛載點
選項:
示例:
name=/mnt/dvd src=/dev/sr0 fstype=iso9660 opts=ro state=present name=/srv/disk src='LABEL=SOME_LABEL' state=present name=/home src='UUID=b3e48f45-f933-4c8e-a700-22a159ec9077' opts=noatime state=present ansible test -a 'dd if=/dev/zero of=/disk.img bs=4k count=1024' ansible test -a 'losetup /dev/loop0 /disk.img' ansible test -m filesystem 'fstype=ext4 force=yes opts=-F dev=/dev/loop0' ansible test -m mount 'name=/mnt src=/dev/loop0 fstype=ext4 state=mounted opts=rw'
該模塊主要用於從http、ftp、https服務器上下載文件(相似於wget),主要有以下選項:
示例:
get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf mode=0440
get_url: url=http://example.com/path/file.conf dest=/etc/foo.conf sha256sum=b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
用於解壓文件,模塊包含以下選項:
示例以下:
- unarchive: src=foo.tgz dest=/var/lib/foo - unarchive: src=/tmp/foo.zip dest=/usr/local/bin copy=no - unarchive: src=https://example.com/example.zip dest=/usr/local/bin copy=no
ansbile-playbook是一系列ansible命令的集合,利用yaml 語言編寫。playbook命令根據自上而下的順序依次執行。同時,playbook開創了不少特性,它能夠容許你傳輸某個命令的狀態到後面的指令,如你能夠從一臺機器的文件中抓取內容並附爲變量,而後在另外一臺機器中使用,這使得你能夠實現一些複雜的部署機制,這是ansible命令沒法實現的。
playbook經過ansible-playbook命令使用,它的參數和ansible命令相似,如參數-k(–ask-pass) 和 -K (–ask-sudo) 來詢問ssh密碼和sudo密碼,-u指定用戶,這些指令也能夠經過規定的單元寫在playbook 。
ansible-playbook的簡單使用方法: ansible-playbook example-play.yml 。
下面是一個簡單的ansible-playbook示例,能夠了解其構成:
# cat user.yml - name: create user hosts: all user: root gather_facts: false 不須要setup vars: user:"test" tasks: - name: create user user: name="{{ user }}"
上面的playbook 實現的功能是新增一個用戶:
一樣,若是想實現把這個新增的用戶刪除,只需將該playbook文件的最後一行替換爲以下行再執行相應的playbook便可:
user: name="{{ user }}" state=absent remove=yes
經過ansible-playbook實現對多臺主機同時同時安裝apache。須要注意的是,多臺被管理主機的操做系統可能不相同,而致使apache包名不一樣,假設同時存在CentOS和Debian兩種操做系統,具體playbook內容以下:
# cat install_apache.yml - hosts: all remote_user: root gather_facts:True tasks: - name: install apache on CentOS yum: name=httpd state=present when: ansible_os_family =="CentOS" - name: install apache on Debian yum: name=apache2 state=present when: ansible_os_family =="Debian"
上面使用了when語句,同時也開啓了gather_facts setup
模塊,這裏的ansible_os_family
變量和就是直接使用的setup模塊獲取的信息。若是有大量主機,就在運行的時候加上-f而後選擇一個合適的併發主機數量便可。
playbook是由一個或多個"play"組成的列表。play的主要功能在於將事先歸併爲一組的主機裝扮成事先經過ansible中的task定義好的角色。從根本上來說所謂task無非是調用ansible的一個module。將多個play組織在一個playbook中便可以讓它們聯同起來按事先編排的機制同唱一臺大戲。其主要有如下四部分構成:
而其對應的目錄層爲五個(通常狀況下,可視狀況而變化),以下:
playbook中的每個play的目的都是爲了讓某個或某些主機以某個指定的用戶身份執行任務。
remote_user
:用於指定遠程主機上的執行任務的用戶。不過remote_user
也可用於各task中。也能夠經過指定其經過sudo的方式在遠程主機上執行任務其可用於play全局或某任務。此外甚至能夠在sudo時使用sudo_user指定sudo時切換的用戶。sudo_user
:若是設置user爲breeze,sudo爲yes,sudo_user爲bernie時,則breeze用戶在執行任務時會得到bernie用戶的權限示例:
- hosts: webnodes tasks: - name: test ping connection: remote_user: test sudo: yes
play的主體部分是任務列表。
任務列表中的各任務按次序逐個在hosts中指定的全部主機上執行即在全部主機上完成第一個任務後再開始第二個。在自上而下運行某playbook時若是中途發生錯誤,全部已執行任務都將回滾所以在更正playbook後從新執行一次便可。
task的目的是使用指定的參數執行模塊,而在模塊參數中可使用變量。模塊執行是冪等的,這意味着屢次執行是安全的,由於其結果均一致。每一個task都應該有其name用於 playbook的執行結果輸出,建議其內容儘量清晰地描述任務執行步驟。若是未提供name則action的結果將用於輸出。
定義task的可使用"action: module options"或"module: options"的格式,推薦使用後者以實現向後兼容。若是action一行的內容過多也可以使用在行首使用幾個空白字符進行換行。
tasks: - name: make sure apache is running service: name=httpd state=running
須要說明的是,在衆多模塊中只有command和shell模塊僅須要給定一個列表而無需使用「key=value」格式。例如:
tasks: - name: disable selinux command: /sbin/setenforce 0
若是命令或腳本的退出碼不爲零可使用以下方式替代:
tasks: - name: run this command and ignore the result shell: /usr/bin/somecommand || /bin/true
可使用ignore_errors來忽略錯誤信息:
tasks: - name: run this command and ignore the result shell: /usr/bin/somecommand ignore_errors: True
用於當關注的資源發生變化時採起必定的操做。
"notify"這個action可用於在每一個play的最後被觸發,這樣能夠避免屢次有改變發生時每次都執行指定的操做,取而代之僅在全部的變化發生完成後一次性地執行指定操做。
在notify中列出的操做稱爲handler也即notify中調用handler中定義的操做。
在notify中定義內容必定要和tasks中定義的 - name 內容同樣,這樣才能達到觸發的效果,不然會不生效。
- name: template configuration file template: src=template.j2 dest=/etc/foo.conf notify: - restart memcached - restart apache
handler是task列表,這些task與前述的task並無本質上的不一樣:
handlers: - name: restart memcached service: name=memcached state=restarted - name: restart apache service: name=apache state=restarted
playbook的模塊與在ansible命令行下使用的模塊有一些不一樣。這主要是由於在playbook中會使用到一些facts變量和一些經過setup模塊從遠程主機上獲取到的變量。有些模塊無法在命令行下運行,就是由於它們須要這些變量。並且即便那些能夠在命令行下工做的模塊也能夠經過playbook的模塊獲取一些更高級的功能。
在實際應用中,咱們的配置文件有些地方可能會根據遠程主機的配置的不一樣而有稍許的不一樣,template可使用變量來接收遠程主機上setup收集到的facts信息,針對不一樣配置的主機,定製配置文件。用法大體與copy模塊相同。
經常使用參數:
官方簡單示例:
- template: src=/mytemplates/foo.j2 dest=/etc/file.conf owner=bin group=wheel mode=0644 - template: src=/mytemplates/foo.j2 dest=/etc/file.conf owner=bin group=wheel mode="u=rw,g=r,o=r" - template: src=/mine/sudoers dest=/etc/sudoers validate='visudo -cf %s'
named.conf配置文件的jinja2模板示例:
options { listen-on port 53 { 127.0.0.1; {% for ip in ansible_all_ipv4_addresses %} {{ ip }}; {% endfor %} }; listen-on-v6 port 53 { ::1; }; directory "/var/named"; dump-file "/var/named/data/cache_dump.db"; statistics-file "/var/named/data/named_stats.txt"; memstatistics-file "/var/named/data/named_mem_stats.txt"; }; zone "." IN { type hint; file "named.ca"; }; include "/etc/named.rfc1912.zones"; include "/etc/named.root.key"; {# Variables for zone config #} {% if 'authorativenames' in group_names %} {% set zone_type = 'master' %} {% set zone_dir = 'data' %} {% else %} {% set zone_type = 'slave' %} {% set zone_dir = 'slaves' %} {% endif %} zone "internal.example.com" IN { type {{ zone_type }}; file "{{ zone_dir }}/internal.example.com"; {% if 'authorativenames' not in group_names %} masters { 192.168.2.2; }; {% endif %} };
playbook的引用該模板配置文件的方法示例:
- name: Setup BIND host: allnames tasks: - name: configure BIND template: src=templates/named.conf.j2 dest=/etc/named.conf owner=root group=named mode=0640
set_fact
set_fact
模塊能夠自定義facts,這些自定義的facts能夠經過template或者變量的方式在playbook中使用。若是你想要獲取一個進程使用的內存的百分比,則必須經過set_fact來進行計算以後得出其值,並將其值在playbook中引用。
下面是一個配置mysql innodb buffer size的示例:
- name: Configure MySQL hosts: mysqlservers tasks: - name: install MySql yum: name=mysql-server state=installed - name: Calculate InnoDB buffer pool size set_fact: innodb_buffer_pool_size_mb="{{ ansible_memtotal_mb / 2 }}" - name: Configure MySQL template: src=templates/my.cnf dest=/etc/my.cnf owner=root group=root mode=0644 notify: restart mysql - name: Start MySQL service: name=mysqld state=started enabled=yes handlers: - name: restart mysql service: name=mysqld state=restarted
my.cnf的配置示例:
# {{ ansible_managed }} [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Configure the buffer pool innodb_buffer_pool_size = {{ innodb_buffer_pool_size_mb|int }}M [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid