1、inventory介紹php
前面咱們介紹過ansible的hosts文件是存放被管理主機的,被管理主機比較少的狀況下,直接在hosts中定義便可,可是之後很定會管理多臺主機,而ansible可管理的主機集合就叫作inventory。在ansible中,描述你主機的默認方法是將它們列在一個文本文件中,這個文件叫inventory文件。html
一個簡單的inventory文件可能只包含一組主機名的列表,以下:node
ftp.testansible.com samba.testansible.com mail.testansible.com
一、inventory的行爲參數python
有時候咱們須要在ansible inventory文件中描述咱們的主機,咱們須要定義主機名,以及ansible的ssh客戶端能夠鏈接到的端口(22,2222,22300)等,那麼ansible將這些變量命名爲inventory的行爲參數,以下:linux
名稱 默認值 描述 ansible_ssh_host 主機的名字 SSH目的主機名或IP ansible_ssh_port 22 SSH目的端口 ansible_ssh_user root SSH登陸使用的用戶名 ansible_ssh_pass none SSH認證所使用的密碼 ansible_connection smart ansible使用何種鏈接模式鏈接到主機 ansible_ssh_private_key_file none SSH認證所使用的私鑰 ansible_shell_type sh 命令所使用的shell ansible_python_interpreter /usr/bin/python 主機上的python解釋器
ansible默認的inventory文件是/etc/ansible/hostsnginx
能夠自定義動態的inventory文件,(--inventory-file等同於-i path)以下web
#ansible --inventory-file=/root/iptables/iptableshosts iptables -m script -a '/root/iptables/check_iptables.sh'
若是有多臺服務器的話,想併發運行,可使用-f參數,默認是併發5正則表達式
#ansible --inventory-file=/root/iptables/iptableshosts iptables -f 6 -m script -a '/root/iptables/check_iptables.sh'
二、主機和主機組定義方式:
redis
直接定義一臺或者2臺server
shell
#vim /etc/ansible/hosts 192.168.100.10 192.168.100.11
定義一個組,能夠是ip也能夠是解析好的域名
[web] 192.168.100.10 192.168.100.11 [httpd] 192.168.100.12 192.168.100.13
嵌套定義組
[apache] http1.test.com http2.test.com [nginx] nginx1.test.com nginx2.test.com [webservers:children] apache nginx
三、主機變量和組變量
主機變量:針對單主機的特性化要求,經過內置變量實現,以下:
[webservers] web1.test.com http_port=80 ansible_ssh_port=12345
羣組變量
語法:[<group name>:vars] 在inventory中指定羣組變量,以下:
[all:vars] ntp_server=ntp.centos.com [production] test1 test2 test3 [production:vars] db_primary_port=22 [groupservers] web1.test.com web2.test.com [groupservers:vars] ntp_server=ntp.test.com admin_user=tom
四、嵌套組和組變量
[apache] http1.test.com http2.test.com [nginx] nginx1.test.com nginx2.test.com [webservers:children] apache nginx [webservers:vars] ntp_server=ntp.test.com
五、變量分離
變量除了能夠和inventory一塊兒存儲在INI配置文件中,也能夠獨立出來。
當咱們要爲很是多的主機和主機組分別設置不一樣的變量時,用如上的方式就顯得比較笨拙了,就須要用到group_vars和host_vars 變量了。Ansible在運行任何yml文件以前,都會去搜索與Hosts文件同一個目錄下的兩個用於定義變量的目錄group_vars和host_vars,因此,咱們能夠在這兩個目錄下放一些使用YAML語法編輯的定義變量的文件,並以對應的主機名和主機組名來命名這些文件,這樣在運行ansible時
ansible會自動去這兩個目錄下讀取針對不一樣主機和主機組的變量定義
好比對主機組group設置變量:
File: /etc/ansible/group_vars/group admin_user: tom
對主機host1設置變量
File: /etc/ansible/host_vars/host1 admin_user: john
除此以外,咱們還能夠在group_vars和host_vars兩個文件夾下定義all文件來一次性地爲全部的主機組和主機定義變量。如何巧妙使用主機變量和組變量有些時候,咱們在運行ansible任務時,可能須要從一臺遠程主機上面獲取另外一臺遠程主機的變量信息,這時一個神奇的變量hostvars能夠幫咱們實現這一個需求。變量hostvars包含了指定主機上面所定義的全部變量。
好比咱們想獲取host1上面的變量admin_user的內容,在任意主機上直接使用以下代碼便可:
{{ hostvars['host1']['admin_user'] }}
Ansible提供了一些很是有用的內置變量,這裏咱們列舉幾個經常使用的:
a、groups: 包含了全部Hosts文件裏面主機組的一個列表
b、group_names: 包含了當前主機所在的全部主機組名的一個列表
c、inventory_hostname: 經過Hosts文件定義主機的主機名和ansible_home不必定相同
d、play_hosts: 將會執行當前任務的全部主機
六、yml文件中使用變量的一個例子:
--- - hosts: all user: root vars: GETURL:"http://192.168.24.14/sa" TARFILE:"sa" TMPPATCH:"/tmp" SHFILE:"os.sh" tasks: - name: Download `TARFILE`.tar.gz package get_url: url="`GETURL`/`TARFILE`.tar.gz" dest=`TMPPATCH` sha256sum=b6f482b3c26422299f06524086d1f087e1d93f2748be18542945bca4c2df1569 tags: -downsa - name: tarzxvf `TARFILE`.tar.gz file shell: tar zxvf "`TMPPATCH`/`TARFILE`.tar.gz" -C `TMPPATCH` tags: -tarxsa - name: Run`SHFILE` script shell: "`TMPPATCH`/`TARFILE`/`SHFILE`" tags: -runsa
2、ansible經常使用模塊介紹:
ansible的每一個模塊用法可使用#ansible-doc MOD 來查看,以下範例:
#ansible-doc copy less 436 Copyright (C) 1984-2009 Mark Nudelman less comes with NO WARRANTY, to the extent permitted by law. For information about the terms of redistribution, see the file named README in the less distribution. Homepage: http://www.greenwoodsoftware.com/less > COPY The [copy] module copies a file on the local box to remote locations. Use the [fetch] module to copy files from remote locations to the local box. If you need variable interpolation in copied files, use the [template] module. Options (= is mandatory): - backup ……………………還有不少能夠顯示,此處省略
查看所支持的模塊,可使用ansible-doc -l 查看,以下:
#ansible-doc -l less 436 Copyright (C) 1984-2009 Mark Nudelman less comes with NO WARRANTY, to the extent permitted by law. For information about the terms of redistribution, see the file named README in the less distribution. Homepage: http://www.greenwoodsoftware.com/less a10_server Manage A10 Networks AX/SoftAX/Thunder/vThunder devices a10_service_group Manage A10 Networks AX/SoftAX/Thunder/vThunder devices a10_virtual_server Manage A10 Networks AX/SoftAX/Thunder/vThunder devices acl Sets and retrieves file ACL information. add_host add a host (and alternatively a group) to the ansible-playbook in-memory inventory airbrake_deployment Notify airbrake about app deployments alternatives Manages alternative programs for common commands apache2_module enables/disables a module of the Apache2 webserver apt Manages apt-packages ……………………還有不少模塊能夠顯示,這裏省略
一、遠程命令模塊
command、script 、shell
Examples:
# Example from Ansible Playbooks
- script: /some/local/script.sh --some-arguments 1234
例如:
#ansible host1 -m command -a "free -m" #能夠簡寫爲ansible host1 -a "free -m" #ansible host1 -m script -a "/home/test.sh 12 34" #ansible host1 -m shell -a "/home/test.sh"
好比在服務端執行:
#ansible web -m shell -a "/root/test.sh 3 4 " 10.0.90.25 | success | rc=0 >> 7 20151119-171933 10.0.90.24 | success | rc=0 >> 7 20151119-171933 注:test.sh 在客戶端服務器/root目錄,內容是前兩個位置變量相加,並打印出當前時間。
一個簡單的建立目錄的例子,建立/usr/local/src/data/log 目錄,以下:
#cat test1.sh #!/bin/bash if [ -z $1 ] || [ -z $2 ];then echo "Wrong,Please input two args" echo "Usage `basename $0` arguments arguments" exit 6 fi mkdir -pv /usr/local/src/$1/$2 #cat createdir.yml --- - hosts: "{{ host }}" user: "{{ user }}" gather_facts: True tasks: - name: Create Dir in client server script: /etc/ansible/test1.sh data log 執行: #ansible-playbook createdir.yml -e "host=web user=root"
一個批量修改root密碼的例子:
shell腳本 #cat alter.sh #!/bin/bash ##modify passwd## echo 'root:1234567890' |chpasswd if [ $? -eq 0 ] then echo "Change password for root OK!!!" else echo "Change password for root failure!!!" fi ansible的yml文件 #cat modify_all_password.yml --- - hosts: all user: root gather_facts: True tasks: - name: Modify root passwd in all client script: /etc/ansible/roles/alter.sh 而後執行: #ansible-playbook modify_all_password.yml
ansible-playbook能夠跟不少參數,可使用--help查看
#ansible-playbook --help Usage: ansible-playbook playbook.yml Options: --ask-become-pass ask for privilege escalation password -k, --ask-pass ask for connection password --ask-su-pass ask for su password (deprecated, use become) -K, --ask-sudo-pass ask for sudo password (deprecated, use become) --ask-vault-pass ask for vault password -b, --become run operations with become (nopasswd implied) --become-method=BECOME_METHOD privilege escalation method to use (default=sudo), valid choices: [ sudo | su | pbrun | pfexec | runas | doas ] …………………… 還有不少,此處省略。 這裏介紹一些經常使用的 --inventory=PATH (-i PATH): 指定inventory文件,默認文件是/etc/ansible/hosts --verbose(-v): 顯示詳細輸出,也可使用-vvvv顯示精確到每分鐘的輸出 --extra-vars=VARS(-e VARS): 定義在playbook使用的變量,格式爲:"key=value,key=value" --forks=NUM ( -f NUM): 指定併發執行的任務數,默認爲5,根據服務器性能,調大這個值可提升ansible執行效率 --connection=TYPE ( -c TYPE):指定鏈接遠程主機的方式,默認爲ssh,設爲local時,剛只在本地執行playbook,建議不作修改 --check: 檢測模式,playbook中定義的全部任務將在每臺遠程主機上進行檢測,但並不直正執行
二、copy模快
實現服務端向目標主機拷貝文件,相似於scp功能:-m copy -a "command"
例如:
#ansible host1 -m copy -a "src=/root/php-5.5.24-1.ele.el6.x86_64.rpm dest=/usr/local/src owner=root group=root mode=0755" 查看客戶端文件是否存在 #ansible host1 -m shell -a "ls -l /usr/local/src" 172.16.29.193 | success | rc=0 >> total 10264 -rw-r--r--. 1 root root 10507544 May 30 02:40 php-5.5.24-1.ele.el6.x86_64.rpm
三、stat模塊:
獲取遠程文件狀態信息,包括atime,ctime,mtime,md5,uid,gid等信息
# ansible 10.0.90.25 -m stat -a "path=/etc/sysctl.conf" 10.0.90.25 | SUCCESS => { "changed": false, "stat": { "atime": 1459270210.5650001, "checksum": "a27c7ce2e6002c37f3cb537ad997c6da7fd76480", "ctime": 1441217442.5749998, "dev": 2051, "exists": true, "gid": 0, "gr_name": "root", "inode": 1181554, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "md5": "c97839af771c8447b9fc23090b4e8d0f", "mode": "0644", "mtime": 1413471211.0, "nlink": 1, "path": "/etc/sysctl.conf", "pw_name": "root", "rgrp": true, "roth": true, "rusr": true, "size": 1150, "uid": 0, "wgrp": false, "woth": false, "wusr": true, "xgrp": false, "xoth": false, "xusr": false } }
#ansible web -m setup ##顯示全部的ansible默認變量信息
四、get_url 模塊
實如今遠程主機下載指定URL到本地,支持sha256sum文件校驗
例如:
#ansible host1 -m get_url -a "url=http://www.baidu.com dest=/tmp/index.html mode=0440 force=yes" 172.16.29.193 | success >> { "changed": true, "checksum": "8bc43056c39fbb882cf5d7b0391d70b6e84096c6", "dest": "/tmp/index.html", "gid": 0, "group": "root", "md5sum": "324aa881293b385d2c0b355cf752cff9", "mode": "0440", "msg": "OK (unknown bytes)", "owner": "root", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "sha256sum": "", "size": 93299, "src": "/tmp/tmp3WI5fE", "state": "file", "uid": 0, "url": "http://www.baidu.com" }
五、yum 模塊
linux 平臺軟件包管理操做,常見的有yum,apt
例如:
#ansible host1 -m yum -a "name=vsftpd state=latest" Ubuntu系列: #ansible host1 -m apt -a "pkg=vsftpd state=latest" yum 模塊的一些用法: - name: install the latest version of Apache yum: name=httpd state=latest - name: remove the Apache package yum: name=httpd state=absent - name: install the latest version of Apache from the testing repo yum: name=httpd enablerepo=testing state=present - name: install one specific version of Apache yum: name=httpd-2.2.29-1.4.amzn1 state=present - name: upgrade all packages yum: name=* state=latest - name: install the nginx rpm from a remote repo yum: name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present - name: install nginx rpm from a local file yum: name=/usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present - name: install the 'Development tools' package group yum: name="@Development tools" state=present - name: install the 'Gnome desktop' environment group yum: name="@^gnome-desktop-environment" state=present
六、cron 模塊
遠程主機crontab 配置,以下:
#ansible host1 -m cron -a "name='crontab test' minute=0 hour=5,2 job='ls -alh > /dev/null'" 172.16.29.193 | success >> { "changed": true, "jobs": [ "crontab test" ] } 效果以下: #Ansible: crontab test 0 5,2 * * * ls -alh > /dev/null
一個簡單的yml例子添加cron:
#cat add_cron.yml --- - hosts: web_crontab remote_user: root gather_facts: True tasks: - name: add ntp server cron job cron: name="local network ntpserver" minute="*/12" hour="*" job="/usr/sbin/ntpdate 10.0.18.1 > /root/ntp.log" 執行: #ansible-playbook add_cron.yml
移除一個cron任務:
#cat del_cron.yml --- - hosts: web remote_user: root gather_facts: false tasks: - name: del an old crontab job cron: name="local network ntpserver" state=absent 執行: #ansible-playbook del_cron.yml
小案例: 假若有一個db備份腳本,須要推到全部db服務器上,並加入crontab,每隔1分鐘執行一次,須要定義playbook結構的yml文件
#tree cronjob/ cronjob/ └── tasks ├── crontest.yml └── main.yml yml文件以下: #cat test-cron.yml --- - name: cron jobs test hosts: "{{ host }}" remote_user: "{{ user }}" gather_facts: True roles: - cronjob tasks目錄中的crontest.yml #cat crontest.yml #copy cron job to client server - copy: src=/tmp/test_time.sh dest=/usr/local/src/test_time.sh owner=root group=root mode=0755 #add cron job - cron: name="test time jobs" minute="*/1" hour="*" job="/usr/local/src/test_time.sh >> /tmp/time.log" tasks目錄中的main.yml #cat main.yml - include: crontest.yml 執行: ansible-playbook test-cron.yml --extra-vars "host=172.16.29.193 user=root" --單臺執行 客戶端crontab效果以下: #Ansible: test time jobs */1 * * * * /usr/local/src/test_time.sh >> /tmp/time.log
以上有點複雜,整合進一個簡單的yml文件:
#cat test-cron.yml --- - hosts: host1 remote_user: root gather_facts: True tasks: - name: copy cron job file to client server copy: src=/tmp/test_time.sh dest=/usr/local/src/test_time.sh owner=root group=root mode=0755 - name: add cron job cron: name="test time jobs" minute="*/2" hour="*" job="/usr/local/src/test_time.sh >> /tmp/time.log" 執行: #ansible-playbook test-cron.yml 效果: #Ansible: test time jobs */2 * * * * /usr/local/src/test_time.sh >> /tmp/time.log
啓用10個並行進程數執行playbook:
#ansible-playbook host1 nginx.yml -f 10
七、file模塊
##設置文件的屬性,一個很經常使用的模塊
相關選項以下:
force:須要在兩種狀況下強制建立軟連接,一種是源文件不存在,但以後會創建的狀況下;另外一種是目標軟連接已存在,須要先取消以前的軟鏈,而後建立新的軟鏈,有兩個選項:yes|no group:定義文件/目錄的屬組 mode: 定義文件/目錄的權限 owner:定義文件/目錄的屬主 path: 必選項,定義文件/目錄的路徑 recurse:遞歸設置文件的屬性,只對目錄有效 src:被連接的源文件路徑,只應用於state=link的狀況 dest:被連接到的路徑,只應用於state=link的狀況 state: directory:若是目錄不存在,就建立目錄 file:即便文件不存在,也不會被建立 link:建立軟連接 hard:建立硬連接 touch:若是文件不存在,則會建立一個新的文件,若是文件或目錄已存在,則更新其最後修改時間 absent:刪除目錄、文件或者取消連接文件
好比:
- file: src=/tmp/test1 dest=/home/test2 state=link - file: path=/root/test.txt state=touch - file: path=/root/tomcat state=directory owner=root group=root mode=0755
八、mount 模塊:遠程主機分區掛載
#ansible host1 -m mount -a "name=/mnt/data src=/dev/sd0 fstype=ext3 opts=ro state=present"
九、service 模塊:遠程主機系統服務管理
#ansible host1 -m service -a "name=httpd stete=restarted"
十、sysctl 模塊:遠程linux主機sysctl配置
sysctl: name=kernel.panic value=3 sysctl_file=/etc/sysctl.conf checks=before reload=yes 如下是定義在yml格式文件中的例子: - sysctl: name=net.ipv4.tcp_rmem 'value=4096 87380 16777216' state=present - sysctl: name=net.ipv4.tcp_wmem 'value=4096 65536 16777216' state=present - sysctl: name=net.ipv6.conf.lo.disable_ipv6 value=1 state=present
十一、lineinfile模塊
Ensure a particular line is in a file, or replace an existing line using a back-referenced regular expression 確保特定的行在文件中,或使用反向引用正則表達式替換現有的行
用法:Examples
- lineinfile: dest=/etc/selinux/config regexp=^SELINUX= line=SELINUX=enforcing - lineinfile: dest=/etc/sudoers state=absent regexp="^%wheel" - lineinfile: dest=/etc/hosts regexp='^127\.0\.0\.1' line='127.0.0.1 localhost' owner=root group=root mode=0644 - lineinfile: dest=/etc/httpd/conf/httpd.conf regexp="^Listen " insertafter="^#Listen " line="Listen 8080" - lineinfile: dest=/etc/services regexp="^# port for http" insertbefore="^www.*80/tcp" line="# port for http by default" # Add a line to a file if it does not exist, without passing regexp - lineinfile: dest=/tmp/testfile line="192.168.1.99 foo.lab.net foo" # Fully quoted because of the ': ' on the line. See the Gotchas in the YAML docs. - lineinfile: "dest=/etc/sudoers state=present regexp='^%wheel' line='%wheel ALL=(ALL) NOPASSWD: ALL'" - lineinfile: dest=/opt/jboss-as/bin/standalone.conf regexp='^(.*)Xms(\d+)m(.*)$' line='\1Xms${xms}m\3' backrefs=yes # Validate the sudoers file before saving - lineinfile: dest=/etc/sudoers state=present regexp='^%ADMIN ALL\=' line='%ADMIN ALL=(ALL) NOPASSWD:ALL' validate='visudo -cf %s'
一個例子:
#cat linetest.yml --- - hosts: web user: root gather_facts: flase tasks: - name: change /etc/hosts lineinfile: dest=/etc/hosts line='test line 1\ntest line 2\ntest line 3\ntest line 4' 上面的例子是在/etc/hosts中添加4行,若是你只須要添加一行,line='test line 1'便可!
十二、blockinfile模塊 Insert/update/remove a text block surrounded by marker lines
注意:dest後面的路徑和文件名必定要存在,不然會報錯。
Examples: Insert/update/remove a text block surrounded by marker lines - name: insert/update "Match User" configuation block in /etc/ssh/sshd_config blockinfile: dest: /etc/ssh/sshd_config block: | Match User ansible-agent PasswordAuthentication no - name: insert/update eth0 configuration stanza in /etc/network/interfaces (it might be better to copy files into /etc/network/interfaces.d/) blockinfile: dest: /etc/network/interfaces block: | iface eth0 inet static address 192.168.0.1 netmask 255.255.255.0 - name: insert/update HTML surrounded by custom markers after <body> line blockinfile: dest: /var/www/html/index.html marker: "<!-- {mark} ANSIBLE MANAGED BLOCK -->" insertafter: "<body>" content: | <h1>Welcome to `ansible_hostname`</h1> <p>Last updated on `ansible_date_time`.`iso8601`</p> - name: remove HTML as well as surrounding markers ##此文件實現將上面添加的內容刪除 blockinfile: dest: /var/www/html/index.html marker: "<!-- {mark} ANSIBLE MANAGED BLOCK -->" content: "" - name: insert/update "Match User" configuation block in /etc/ssh/sshd_config blockinfile: dest: /etc/hosts block: | `item`.`name` `item`.`ip` marker: "# {mark} ANSIBLE MANAGED BLOCK `item`.`name`" with_items: - { name: host1, ip: 10.10.1.10 } - { name: host2, ip: 10.10.1.11 } - { name: host3, ip: 10.10.1.12 }
第一個例子:在/etc/hosts文件中添加3行
#cat blockinfile.yml --- - hosts: web user: root tasks: - name: this is test blockinfile module blockinfile: dest: /etc/hosts block: | this is a test line 1 this is a test line 2 this is a test line 3 結果: #cat /etc/hosts # BEGIN ANSIBLE MANAGED BLOCK this is a test line 1 this is a test line 2 this is a test line 3 # END ANSIBLE MANAGED BLOCK
第二個例子:建立一個index.html文件,並寫入html格式內容
#cat blockinfile.yml --- - hosts: web user: root tasks: - name: create a test file file: path=/root/index.html state=touch - name: this is test blockinfile module blockinfile: dest: /root/index.html marker: "<!-- {mark} ansible managed block -->" insertafter: "<body>" content: | <h1>welcome to `ansible_hostname`</h1> <p>Last updated on `ansible_date_time`.`iso8601`</p> 結果: #cat index.html <!-- BEGIN ansible managed block --> <h1>welcome to http</h1> <p>Last updated on 2016-05-19T03:17:35Z</p> <!-- END ansible managed block -->
注意:上面yml文件中ansible_hostname和ansible_date_time.iso8601變量是ansible默認的系統變量,這個變量能夠經過-m setup查看到!
因此上面的ansible_date_time.iso8601能夠改成iso8601_basic_short(人類可讀)
結果就變成了以下:由於當前時間就是#Thu May 19 11:33:29 CST 2016
<p>Last updated on 20160519T113219</p>
第三個例子:index.html已經存在,而且內容以下:
#cat index.html <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
建立ansible的playbook文件以下:
#cat blockinfile.yml --- - hosts: 10.0.90.25 user: root gather_facts: True tasks: - name: this is test blockinfile module blockinfile: dest: /root/index.html marker: "<!-- {mark} ansible managed block -->" insertafter: "<body>" content: | <h1>welcome to `ansible_hostname`</h1> <p>Last updated on `ansible_date_time`.`iso8601_basic_short`</p> 執行以後,再查看index.html #cat index.html <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <!-- BEGIN ansible managed block --> ###從這裏開始 <h1>welcome to http</h1> <p>Last updated on 20160519T115013</p> <!-- END ansible managed block --> ###在這裏結束 <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
第四個例子:使用變量
#cat blockinfile.yml --- - hosts: 10.0.90.25 user: root gather_facts: True tasks: - name: this is test blockinfile module blockinfile: dest: /etc/hosts block: | `item`.`name` `item`.`ip` marker: "# {mark} ansible managed block `item`.`name`" with_items: - { name: host1, ip: 10.10.1.10} - { name: host2, ip: 10.10.1.11} - { name: host3, ip: 10.10.1.12} 執行後,結果以下: #cat /etc/hosts # BEGIN ansible managed block host1 host1 10.10.1.10 # END ansible managed block host1 # BEGIN ansible managed block host2 host2 10.10.1.11 # END ansible managed block host2 # BEGIN ansible managed block host3 host3 10.10.1.12 # END ansible managed block host3
不足之處,請多多指出!