Ansible-playbook简单应用的几个实例
①ansible-playbook的循環(huán):
重復(fù)執(zhí)行某任務(wù);對迭代項(xiàng)的引用,固定變量名為“item”,而后要在task中使用with_items給定要迭代的元素列表,列表方法:字符串/字典(類似json)
- hosts: allremote_user: roottasks:- name: add some groupsgroup: name={{ item }} state=presentwith_items:- demo1- demo2- demo3- name: add some usersuser: name={{ item.name }} group={{ item.group }} state=present //模塊中調(diào)用的相關(guān)參數(shù)賦值時(shí)前后均無空格with_items:- { name: 'user1',group: 'demo1'} - { name: 'user2',group: 'demo2'}- { name: 'user3',group: 'demo3'} ////此處key后面的:與value之間要有一個(gè)空格,否則為語法錯(cuò)誤②ansible-playbook的條件判斷:
相當(dāng)于編程語言中的if語句,不過在ansible中要使用when語句,且要定義在tasks中
- hosts: allremote_user: roottasks:- name: Install httpd conf file to httpd2.2template: src=file/httpd.conf.c6.j2 dest=/etc/httpd/conf/httpd.confwhen: ansible_distribution_major_version == "6"- name: Install httpd conf file to httpd2.4template: src=file/httpd.conf.c7.j2 dest=/etc/httpd/conf/httpd.confwhen: ansible_distribution_major_version == "7"- name: Start httpdservice: name=httpd state=started enabled=true③ansible-playbook限定執(zhí)行范圍:
當(dāng)playbook指定的一批主機(jī)中有個(gè)別主機(jī)需進(jìn)行變更時(shí),不用修改playbook本身,可通過一些命令選項(xiàng)直接進(jìn)行限定ansible-playbook的命令執(zhí)行范圍。
--limit選項(xiàng):
ansible-playbook test1.yaml --limit node2 //此時(shí),ansbile-playbook中的hosts即便定義了all,也不會(huì)在node2這個(gè)組上執(zhí)行playbook或者直接在playbook的yaml文件中顯式定義hosts要執(zhí)行的主機(jī),如:
指定單臺(tái)主機(jī):www.a.com
指定多臺(tái)主機(jī):www.a.com,www,b.com
指定一組主機(jī):dbserver
也可以在執(zhí)行playbook前先查看受影響的主機(jī)有哪些:
[root@node1 work]# ansible-playbook test2.yaml --list-hosts playbook: test2.yamlplay #1 (all): all TAGS: []pattern: [u'all']hosts (2):node3node2④ansible加密模塊Vault
在執(zhí)行某些任務(wù)時(shí),難免會(huì)觸及到一些密碼或敏感數(shù)據(jù),此時(shí)需要對相關(guān)任務(wù)進(jìn)行加密,ansible自帶的Vault可滿足大部分需求。
使用ansible-vault命令給文件加密:
[root@node1 work]# ansible-vault encrypt test5.yaml //加密命令 New Vault password: Confirm New Vault password: Encryption successful此時(shí)查看test5.yaml的內(nèi)容則顯示:
[root@node1 work]# cat test5.yaml $ANSIBLE_VAULT;1.1;AES256 32366237663533633838613431653433653061396630346633396232393265376138626630646633 6635646462346665613963303061323164623265303331610a633537393239633832383334386338 39393932633163303136353934343061363330313663633535626138613537633465326232383663 3036336337333163390a623733323635653536316335323663363736303733303362353839356164 38643665363131316631646166396634616131343835366261356130343061356438363530636364 61353764383636386438636662373665613031623366396364306262396536656362336161313630 33323437623435646133643831656433653061316439323931326134386263653665633037393037 62303865383165336362且直接使用ansible-playbook執(zhí)行此文件會(huì)報(bào)錯(cuò),需要先解密。這里列出ansible-vault命令的幾個(gè)常用的選項(xiàng):
ansible-vault命令的其他幾個(gè)常用選項(xiàng):edit:用于編輯ansible-vault加密過的文件rekey:重新修改已被加密文件的密碼create:創(chuàng)建一個(gè)新文件,并直接對其進(jìn)行加密view:查看經(jīng)過加密的文件decrypt:解密文件? 也可以在當(dāng)前登錄用戶的家目錄下的.ansible目錄中創(chuàng)建一個(gè)文件用于存儲(chǔ)密碼,修改這些文件的權(quán)限為600,在運(yùn)行時(shí),使用如下命令進(jìn)行:
ansible-playbook test5.yaml --vault-password-file /root/.ansible/vault_pass.txt // --vault-password-file PATH/TO/PASSWD_FILE?⑤簡單基于roles來一鍵部署LAMP環(huán)境
首先看一下php的角色目錄結(jié)構(gòu):
[root@node1 roles]# tree php/ php/ ├── default ├── files │?? └── php-fpm.conf ├── handlers │?? └── main.yaml ├── meta ├── tasks │?? └── main.yaml ├── templates │?? └── www.conf.j2 └── vars7 directories, 4 files如果使用yum安裝的php-fpm的話,其配置文件被切分成了兩部分默認(rèn)的話,如果基于默認(rèn)配置的話,則無需修改php-fpm.conf,直接修改www.conf即可。
tasks/main.yaml的內(nèi)容如下:
- name: install phpyum: name=php,php-fpm state=present - name: create php-fpm groupgroup: name=www state=present - name: create php-fpm useruser: name=www group=www state=present - name: install conf file1copy: src=php-fpm.conf dest=/etc/php-fpm.conf - name: install conf filetemplate: src=www.conf.j2 dest=/etc/php-fpm.d/www.confnotify: restart php-fpmtags: init conf file - name: start php-fpmservice: name=php-fpm state=startedhandlers/main.yaml的內(nèi)容如下:
- name: restart php-fpmservice: name=php-fpm state=restartedtemplates/www.conf.j2的內(nèi)容如下:
listen = {{ ansible_eno16777736.ipv4.address }}:9000//僅做測試演示的話,只修改此項(xiàng)即可。再來看下httpd的角色目錄結(jié)構(gòu):
[root@node1 httpd]# tree . ├── default ├── files │?? └── index.php //php測試頁 ├── handlers │?? └── main.yaml ├── meta ├── tasks │?? └── main.yaml ├── templates │?? ├── httpd.conf.j2 //httpd的主配置文件 │?? └── php.conf.j2 //此處是基于虛擬主機(jī)的php配置 └── vars└── main.yaml7 directories, 6 filestasks/main.yaml的內(nèi)容如下:
- name: install htppdyum: name=httpd state=present - name: install conf filetemplate: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf src=php.conf.j2 dest=/etc/httpd/conf.d/php.conf.j2notify: restart httpdtags: change conf file - name: init index.phpcopy: src=index.php dest=/var/www/html/index.php - name: start httpdservice: name=httpd state=started enabled=truetemplates/php.conf.j2的內(nèi)容如下(基于httpd2.4的配置):
DirectoryIndex index.php <VirtualHost *:80>ServerName www.phptest.comDocumentRoot "/var/www/html"ProxyRequests OffProxyPassMatch ^/(.*\.php)$ fcgi://localhost:9000/var/www/html/$1<Directory "/var/www/html">Options NoneAllowOverride NoneRequire all granted</Directory> </VirtualHost>mysql的角色目錄結(jié)構(gòu)大同小異,可自行基于rpm包安裝或者其他安裝方式定義角色文件即可。
寫好角色后,再定義一個(gè)任務(wù),跑一下即可,此處任務(wù)文件為/etc/ansible/work/role_lamp.yaml,內(nèi)容如下:
- hosts: tworemote_user: rootroles:- httpd- php- mysql跑完后,如無報(bào)錯(cuò),且測試頁能訪問即可。本例只是簡單的陳述了下大體的角色架構(gòu),里面還有很多需要細(xì)細(xì)雕琢之處,還需各位多多閱讀官方文檔或其他相關(guān)ansible的書籍,深入學(xué)習(xí)。
轉(zhuǎn)載于:https://www.cnblogs.com/trymybesttoimp/p/7216860.html
總結(jié)
以上是生活随笔為你收集整理的Ansible-playbook简单应用的几个实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 服务器端发送邮件签名采用Data URI
- 下一篇: CentOS 7 使用Google-Au