ansible部署 應用 模塊

Top

1 案例1:環境準備

1.1 問題

本案例要求準備ansible的基礎環境:html

  • 啓動6臺虛擬機
  • 禁用selinux和firewalld
  • 編輯/etc/hosts
  • 配置yum擴展源並在管理節點安裝ansible

1.2 方案

此方案須要準備六臺主機,1臺管理主機,5臺託管主機,以實現批量程序部署,批量運行命令等功能,具體要求如表-1所示:python

表-1linux

1.3 步驟

實現此案例須要按照以下步驟進行。web

步驟一:基礎環境準備正則表達式

1)啓動6臺虛擬機,因爲已經講過怎麼建立,這裏再也不在案例裏體現sql

2)真機配置yum倉庫shell

  1. [root@room9pc01 ~]# tar -xf ansible_soft.tar.xz
  2. [root@room9pc01 ~]# cd ansible_soft/
  3. [root@room9pc01 ansible_soft]# mkdir /var/ftp/ansible
  4. [root@room9pc01 ansible_soft]# cp * /var/ftp/ansible
  5. [root@room9pc01 ansible_soft]# createrepo /var/ftp/ansible
  6. Spawning worker 0 with 1 pkgs
  7. Spawning worker 1 with 1 pkgs
  8. Spawning worker 2 with 1 pkgs
  9. Spawning worker 3 with 1 pkgs
  10. Spawning worker 4 with 1 pkgs
  11. Spawning worker 5 with 1 pkgs
  12. Workers Finished
  13. Saving Primary metadata
  14. Saving file lists metadata
  15. Saving other metadata
  16. Generating sqlite DBs
  17. Sqlite DBs complete

3)修改主機名(容易區分,6臺機器都須要修改)這裏以ansible主機爲例子apache

  1. [root@localhost ~]# echo ansible > /etc/hostname
  2. [root@localhost ~]# hostname ansible

4)配置ip(6臺機器都須要配置),這裏以ansible主機爲例子json

  1. [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
  2. # Generated by dracut initrd
  3. DEVICE="eth0"
  4. ONBOOT="yes"
  5. IPV6INIT="no"
  6. IPV4_FAILURE_FATAL="no"
  7. NM_CONTROLLED="no"
  8. TYPE="Ethernet"
  9. BOOTPROTO="static"
  10. IPADDR=192.168.1.51
  11. PREFIX=24
  12. GATEWAY=192.168.1.254
  13. [root@localhost ~]# systemctl restart network
  14. [root@localhost ~]# ifconfig
  15. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  16. inet 192.168.1.51 netmask 255.255.255.0 broadcast 192.168.1.255
  17. ether 52:54:00:b2:69:9e txqueuelen 1000 (Ethernet)
  18. RX packets 234 bytes 16379 (15.9 KiB)
  19. RX errors 0 dropped 36 overruns 0 frame 0
  20. TX packets 31 bytes 2618 (2.5 KiB)
  21. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

5)配置yum客戶端,在管理節點ansible上面配置vim

  1. [root@ansible ~]# vim /etc/yum.repos.d/local.repo
  2. [local_repo]
  3. name=CentOS-$releasever - Base
  4. baseurl="ftp://192.168.1.254/system"
  5. enabled=1
  6. gpgcheck=1
  7. [local]
  8. name=local
  9. baseurl="ftp://192.168.1.254/ansible"
  10. enabled=1
  11. gpgcheck=0
  12. [root@ansible ~]# yum clean all
  13. [root@ansible ~]# yum repolist
  14. [root@ansible ~]# yum -y install ansible
  15. [root@ansible ~]# ansible --version
  16. ansible 2.4.2.0        //顯示版本說明安裝成功
  17. config file = /etc/ansible/ansible.cfg
  18. configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  19. ansible python module location = /usr/lib/python2.7/site-packages/ansible
  20. executable location = /usr/bin/ansible
  21. python version = 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

6)請在6臺主機上面配置/etc/hosts,這裏以ansible主機爲例子

  1. [root@ansible ansible]# cat /etc/hosts
  2. 192.168.1.51 ansible
  3. 192.168.1.52 web1
  4. 192.168.1.53 web2
  5. 192.168.1.54 db1
  6. 192.168.1.55 db2
  7. 192.168.1.56 cache

2 案例2:主機定義與分組:

2.1 問題

本案例要求:

  • 熟悉ansible配置文件
  • 定義主機,分組和子組練習
  • 自定義文件,多配置路徑練習

2.2 步驟

實現此案例須要按照以下步驟進行。

步驟一:ansible.cfg配置文件

  1. [root@ansible ~]# cd /etc/ansible/
  2. [root@ansible ansible]# ls
  3. ansible.cfg hosts roles
  4. [root@ansible ansible]# vim ansible.cfg
  5. #inventory = /etc/ansible/hosts     //指定分組文件路徑,主機的分組文件hosts
  6. [selinux]        //組名稱,selinux的相關選項在這個下面配置
  7. ...
  8. [colors]        //組名稱,colors的相關選項在這個下面配置
  9. ...

步驟二:定義主機,分組和子組練習

1)靜態主機的定義

  1. [root@ansible ansible]# vim hosts
  2. [web]
  3. web1
  4. web2
  5. [db]
  6. db[1:2]                     //1:2爲db1到db2兩臺主機,1:20爲db1到db20多臺主機
  7. [other]
  8. cache
  9. [root@ansible ansible]# ansible web --list-host //顯示web組的主機
  10. hosts (2):
  11. web1
  12. web2
  13. [root@ansible ansible]# ansible db --list-host        
  14. hosts (2):
  15. db1
  16. db2
  17. [root@ansible ansible]# ansible other --list-host
  18. hosts (1):
  19. cache
  20. [root@ansible ansible]# ansible all --list-host //顯示全部組的主機
  21. hosts (5):
  22. web1
  23. web2
  24. cache
  25. db1
  26. db2

2)直接測試

  1. [root@ansible ansible]# ansible cache -m ping        
  2. //測試是否能夠鏈接,若失敗顏色爲紅色
  3. cache | UNREACHABLE! => {
  4. "changed": false,
  5. "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname cache: Name or service not known\r\n",
  6. "unreachable": true
  7. }

3)修改後測試

  1. [root@ansible ansible]# vi hosts
  2. [other]
  3. cache ansible_ssh_user="root" ansible_ssh_pass="a"
  4. [root@ansible ansible]# ansible other -m ping //測試成功,顏色爲綠色
  5. cache | SUCCESS => {
  6. "changed": false,
  7. "ping": "pong"
  8. }

4)不檢測主機的sshkey,在第一次鏈接的時候不用輸入yes

  1. [root@ansible ansible]# vim ansible.cfg
  2. 61 host_key_checking = False
  3. [root@ansible ansible]# vim hosts
  4. [web]
  5. web1
  6. web2
  7. [web:vars] //web組:變量(vars不改),web組的多臺機器共用一個用戶名和密碼
  8. ansible_ssh_user="root"
  9. ansible_ssh_pass="a"
  10. [root@ansible ansible]# ansible web -m ping
  11. web2 | SUCCESS => {
  12. "changed": false,
  13. "ping": "pong"
  14. }
  15. web1 | SUCCESS => {
  16. "changed": false,
  17. "ping": "pong"
  18. }

步驟三:定義子組

  1. [root@ansible ansible]# vi hosts
  2. [app:children] //指定子分組(app可改:children不改),web,db是提早分好的組
  3. web
  4. db
  5. [app:vars]
  6. ansible_ssh_user="root"
  7. ansible_ssh_pass="a"
  8. [root@ansible ansible]# ansible app --list-host        //查看
  9. hosts (4):
  10. web1
  11. web2
  12. db1
  13. db2
  14. [root@ansible ansible]# ansible app -m ping        //測試
  15. web1 | SUCCESS => {
  16. "changed": false,
  17. "ping": "pong"
  18. }
  19. web2 | SUCCESS => {
  20. "changed": false,
  21. "ping": "pong"
  22. }
  23. db1 | SUCCESS => {
  24. "changed": false,
  25. "ping": "pong"
  26. }
  27. db2 | SUCCESS => {
  28. "changed": false,
  29. "ping": "pong"
  30. }

步驟四:多路徑練習

自定義的ansible文件只在當前路徑生效

1)多路徑

  1. [root@ansible ~]# mkdir aaa
  2. [root@ansible ~]# cd aaa/
  3. [root@ansible aaa]# vim myhost
  4. [app1]
  5. web1
  6. db1
  7. [app2]
  8. web2
  9. db2
  10. [app:children]
  11. app1
  12. app2
  13. [other]
  14. cache
  15. [app:vars]
  16. ansible_ssh_user="root"
  17. ansible_ssh_pass="a"
  18. [root@ansible aaa]# touch ansible.cfg
  19. [root@ansible aaa]# grep -Ev "^#|^$" /etc/ansible/ansible.cfg
  20. [defaults]
  21. roles_path = /etc/ansible/roles:/usr/share/ansible/roles
  22. host_key_checking = False
  23. [inventory]
  24. [privilege_escalation]
  25. [paramiko_connection]
  26. [ssh_connection]
  27. [persistent_connection]
  28. [accelerate]
  29. [selinux]
  30. [colors]
  31. [diff]
  32. [root@ansible aaa]# vim ansible.cfg
  33. [defaults]
  34. inventory = myhost
  35. host_key_checking = False

2)測試結果

  1. [root@ansible aaa]# ansible app1 -m ping
  2. web1 | SUCCESS => {
  3. "changed": false,
  4. "ping": "pong"
  5. }
  6. db1 | SUCCESS => {
  7. "changed": false,
  8. "ping": "pong"
  9. }
  10. [root@ansible aaa]# ansible app -m ping
  11. web1 | SUCCESS => {
  12. "changed": false,
  13. "ping": "pong"
  14. }
  15. db1 | SUCCESS => {
  16. "changed": false,
  17. "ping": "pong"
  18. }
  19. db2 | SUCCESS => {
  20. "changed": false,
  21. "ping": "pong"
  22. }
  23. web2 | SUCCESS => {
  24. "changed": false,
  25. "ping": "pong"
  26. }
  27. [root@ansible aaa]# ansible app --list-host
  28. hosts (4):
  29. web1
  30. db1
  31. web2
  32. db2
  33. [root@ansible aaa]# cd
  34. [root@ansible ~]# ansible app1 --list-host //切換到別的目錄,測試失敗
  35. [WARNING]: Could not match supplied host pattern, ignoring: app1
  36. [WARNING]: No hosts matched, nothing to do
  37. hosts (0):

3 案例3:動態主機

3.1 問題

本案例要求:

  • 腳本輸出主機列表

3.2 步驟

實現此案例須要按照以下步驟進行。

步驟一:腳本輸出主機列表

  1. [root@ansible ~]# cd aaa
  2. [root@ansible aaa]# vim host.py
  3. #!/usr/bin/python
  4. import json
  5. hostlist = {}
  6. hostlist["bb"] = ["192.168.1.52", "192.168.1.53"]
  7. hostlist["192.168.1.54"] = {
  8. "ansible_ssh_user":"root","ansible_ssh_pass":"pwd"
  9. }
  10. hostlist["aa"] = {
  11. "hosts" : ["192.168.1.55", "192.168.1.56"],
  12. "vars" : {
  13. "ansible_ssh_user":"root","ansible_ssh_pass":"pwd"
  14. }
  15. }
  16. print(json.dumps(hostlist))
  17. [root@ansible aaa]# chmod 755 ./host.py

步驟二:腳本輸出樣例(這樣寫輸出的結果有些亂)

  1. [root@ansible aaa]# ./host.py
  2. {"aa": {"hosts": ["192.168.1.55", "192.168.1.56"], "vars": {"ansible_ssh_user": "root", "ansible_ssh_pass": "a"}}, "192.168.1.54": {"ansible_ssh_user": "root", "ansible_ssh_pass": "a"}, "bb": ["192.168.1.52", "192.168.1.53"]}

步驟三:能夠用shell腳本輸出

  1. [root@ansible aaa]# vim my.sh
  2. #!/bin/bash
  3. echo '
  4. { "aa": {
  5. "hosts":
  6. ["192.168.1.55", "192.168.1.56"],
  7. "vars": {
  8. "ansible_ssh_user": "root",
  9. "ansible_ssh_pass": "a"}
  10. },
  11. }'
  12. [root@ansible aaa]# chmod 755 my.sh
  13. [root@ansible aaa]# ./my.sh
  14. { "aa": {
  15.     "hosts":
  16.         ["192.168.1.55", "192.168.1.56"],
  17. "vars": {
  18.         "ansible_ssh_user": "root",
  19.         "ansible_ssh_pass": "a"}
  20. },
  21. }
  22. [root@ansible aaa]# vim ansible.cfg
  23. [defaults]
  24. inventory = my.sh
  25. host_key_checking = False
  26. [root@ansible aaa]# ansible aa -m ping
  27. 192.168.1.55 | SUCCESS => {
  28. "changed": false,
  29. "ping": "pong"
  30. }
  31. 192.168.1.56 | SUCCESS => {
  32. "changed": false,
  33. "ping": "pong"
  34. }

步驟二:批量執行

1)查看負載

  1. [root@ansible aaa]# ansible app -m command -a 'uptime'    
  2. db1 | SUCCESS | rc=0 >>
  3. 11:35:52 up 1:59, 2 users, load average: 0.00, 0.01, 0.01
  4. web1 | SUCCESS | rc=0 >>
  5. 11:35:52 up 2:00, 2 users, load average: 0.00, 0.01, 0.02
  6. db2 | SUCCESS | rc=0 >>
  7. 11:35:53 up 1:59, 2 users, load average: 0.00, 0.01, 0.03
  8. web2 | SUCCESS | rc=0 >>
  9. 11:35:52 up 1:59, 2 users, load average: 0.00, 0.01, 0.02

2)查看時間

  1. [root@ansible aaa]# ansible app -m command -a 'date +%F\ %T'
  2. db1 | SUCCESS | rc=0 >>
  3. 2018-09-06 11:42:18
  4. web1 | SUCCESS | rc=0 >>
  5. 2018-09-06 11:42:18
  6. web2 | SUCCESS | rc=0 >>
  7. 2018-09-06 11:42:18
  8. db2 | SUCCESS | rc=0 >>
  9. 2018-09-06 11:42:19

4 案例4:批量部署證書文件

4.1 問題

本案例要求:

  • 建立一對密鑰
  • 給全部主機部署密鑰

4.2 步驟

實現此案例須要按照以下步驟進行。

步驟一:批量部署證書文件,給全部主機部署密鑰

1)建立密鑰

  1. [root@ansible aaa]# cd /root/.ssh/
  2. [root@ansible .ssh]# vi /etc/ansible/hosts
  3. [web]
  4. web1
  5. web2
  6. [db]
  7. db[1:2]
  8. [other]
  9. cache
  10. [root@ansible .ssh]# ansible all -m ping //直接ping會報錯
  11. [root@ansible .ssh]# ssh-keygen -t rsa -b 2048 -N '' //建立密鑰

2)給全部主機部署密鑰

  1. [root@ansible .ssh]# ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(< /root/.ssh/id_rsa.pub)'" -k
  2. SSH password:        //輸入密碼
  3. [root@ansible .ssh]# ansible all -m ping //成功
  4. web2 | SUCCESS => {
  5. "changed": false,
  6. "ping": "pong"
  7. }
  8. db2 | SUCCESS => {
  9. "changed": false,
  10. "ping": "pong"
  11. }
  12. web1 | SUCCESS => {
  13. "changed": false,
  14. "ping": "pong"
  15. }
  16. cache | SUCCESS => {
  17. "changed": false,
  18. "ping": "pong"
  19. }
  20. db1 | SUCCESS => {
  21. "changed": false,
  22. "ping": "pong"
  23. }
  24. [root@ansible .ssh]# ssh web1        //不須要輸入密碼,能夠直接登錄
  25. Last login: Thu Sep 6 11:49:00 2018 from 192.168.1.51
  26. [root@web1 ~]#

5 案例5:練習模塊

5.1 問題

本案例要求:

  • 練習使用command , shell , raw, script模塊

5.2 步驟

實現此案例須要按照以下步驟進行。

步驟一:練習模塊

ansible-doc //模塊的手冊,至關於man

ansible-doc -l //列出全部模塊

ansible-doc 模塊名 //查看指定模塊的幫助信息

1)ping模塊

  1. [root@ansible .ssh]# ansible web1 -m ping
  2. web1 | SUCCESS => {
  3. "changed": false,
  4. "ping": "pong"
  5. }

2)command模塊

  1. [root@ansible .ssh]# ansible web1 -m command -a 'chdir=/tmp touch f1' //建立成功
  2. [root@web1 ~]# cd /tmp/
  3. [root@web1 tmp]# ls        //在web1上面查看
  4. f1

3)shell模塊

  1. [root@ansible .ssh]# ansible web1 -m shell -a 'chdir=/tmp touch f2' //建立成功
  2. [root@web1 ~]# cd /tmp/
  3. [root@web1 tmp]# ls    //在web1上面查看
  4. f2

4)raw模塊

  1. [root@ansible .ssh]# ansible web1 -m raw -a 'chdir=/tmp touch f3'
  2. //文件能夠建立,但沒法切換目錄,文件在用戶家目錄下生成
  3. web1 | SUCCESS | rc=0 >>
  4. Shared connection to web1 closed.
  5. [root@web1 tmp]# cd /root/
  6. [root@web1 ~]# ls        //在web1上面查看
  7. f3

5)script模塊

對於太複雜的命令,能夠寫個腳本,而後用script模塊執行

在web1主機上建立zhangsan3用戶,修改zhangsan3的密碼爲123456,設置zhangsan3第一次登錄必須修改密碼

用命令寫:

  1. [root@ansible .ssh]# ansible web1 -m shell -a 'useradd zhangsan3'
  2. [root@ansible .ssh]# ansible web1 -m shell -a 'echo 123456 | passwd --stdin zhangsan3'
  3. [root@ansible .ssh]# ssh -l zhangsan3 web1
  4. zhangsan3@web1's password: //輸入zhangsan3的密碼
  5. [root@ansible .ssh]# ansible web1 -m shell -a 'chage -d 0 zhangsan3'
  6. [root@ansible .ssh]# ssh -l zhangsan3 web1

用腳本寫,script模塊執行:

  1. [root@ansible .ssh]# vim user.sh
  2. #!/bin/bash
  3. useradd zhangsan3
  4. echo 123456 | passwd --stdin zhangsan3
  5. chage -d 0 zhangsan3
  6. echo
  7. [root@ansible .ssh]# ansible web1 -m script -a './user.sh'
  8. web1 | SUCCESS => {
  9. "changed": true,
  10. "rc": 0,
  11. "stderr": "Shared connection to web1 closed.\r\n",
  12. "stdout": "Changing password for user zhangsan3.\r\npasswd: all authentication tokens updated successfully.\r\n\r\n",
  13. "stdout_lines": [
  14. "Changing password for user zhangsan3.",
  15. "passwd: all authentication tokens updated successfully.",
  16. ""
  17. ]
  18. }
  19. [root@ansible .ssh]# ssh -l lisi web1
  20. lisi@web1's password:
  21. You are required to change your password immediately (root enforced)
  22. Last login: Thu Sep 6 14:51:33 2018 from 192.168.1.51
  23. WARNING: Your password has expired.
  24. You must change your password now and login again!
  25. Changing password for user lisi.
  26. Changing password for lisi.
  27. (current) UNIX password:

6 案例6:模塊練習

6.1 問題

本案例要求:

  • 使用copy模塊同步數據
  • 使用lineinfile模塊編輯文件
  • 使用replace模塊修改文件

6.2 步驟

實現此案例須要按照以下步驟進行。

步驟一:模塊練習

1)使用copy模塊同步數據

src:要複製到進程主機的文件在本地的地址,能夠是絕對路徑,也能夠是相對路徑。若是路徑是一個目錄,它將遞歸複製。在這種狀況下,若是路徑使用"/"來結尾,則只複製目錄裏的內容,若是沒有使用"/"來結尾,則包含目錄在內的整個內容所有複製,相似於rsync

dest:必選項。進程主機的絕對路徑,若是源文件是一個目錄,那麼該路徑也必須是個目錄

backup:在覆蓋以前將原文件備份,備份文件包含時間信息。有兩個選項:yes|no

force:若是目標主機包含該文件,但內容不一樣,若是設置爲yes,則強制覆蓋,若是爲no,則只有當目標主機的目標位置不存在該文件時,才複製。默認爲yes

  1. [root@ansible .ssh]# ansible all -m shell -a 'cat /etc/resolv.conf'
  2. //查看/etc/resolv.conf
  3. cache | SUCCESS | rc=0 >>
  4. ; generated by /usr/sbin/dhclient-script
  5. nameserver 192.168.1.254
  6. search localhost
  7. db2 | SUCCESS | rc=0 >>
  8. ; generated by /usr/sbin/dhclient-script
  9. nameserver 192.168.1.254
  10. search localhost
  11. web1 | SUCCESS | rc=0 >>
  12. ; generated by /usr/sbin/dhclient-script
  13. nameserver 192.168.1.254
  14. search localhost
  15. web2 | SUCCESS | rc=0 >>
  16. ; generated by /usr/sbin/dhclient-script
  17. nameserver 192.168.1.254
  18. search localhost
  19. db1 | SUCCESS | rc=0 >>
  20. ; generated by /usr/sbin/dhclient-script
  21. nameserver 192.168.1.254
  22. search localhost
  23. [root@ansible .ssh]# vi /etc/resolv.conf
  24. nameserver 172.40.1.10
  25. [root@ansible .ssh]# ansible all -m copy -a 'src=/etc/resolv.conf dest=/etc/resolv.conf' //複製本機的resolv.conf到其餘主機
  26. [root@ansible .ssh]# ansible all -m shell -a 'cat /etc/resolv.conf'
  27. //查看有nameserver 172.40.1.10
  28. [root@ansible ~]# mkdir aa
  29. [root@ansible ~]# ansible all -m copy -a 'src=/root/aa dest=/root/a.log'
  30. //複製本機的目錄/root/aa到其餘機器的/root/a.log,複製目錄只能少數批量執行同步
  31. [root@ansible ~]# ansible all -m shell -a 'ls -ld /root'
  32. db2 | SUCCESS | rc=0 >>
  33. dr-xr-x---. 4 root root 167 Sep 6 11:48 /root
  34. web2 | SUCCESS | rc=0 >>
  35. dr-xr-x---. 4 root root 167 Sep 6 11:48 /root
  36. cache | SUCCESS | rc=0 >>
  37. dr-xr-x---. 4 root root 177 Sep 6 14:35 /root
  38. db1 | SUCCESS | rc=0 >>
  39. dr-xr-x---. 4 root root 167 Sep 6 11:48 /root
  40. web1 | SUCCESS | rc=0 >>
  41. dr-xr-x---. 4 root root 177 Sep 6 14:35 /root

2)使用lineinfile模塊編輯文件

以行爲基礎,整行修改(整行被替換掉)

  1. [root@ansible ~]# ansible cache -m lineinfile \
  2. -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 \
  3. regexp="^ONBOOT=" line="ONBOOT=\"no\""'
  4. cache | SUCCESS => {
  5. "backup": "",
  6. "changed": true,
  7. "msg": "line replaced"
  8. }

3)使用replace模塊修改文件

修改文件的某一部分(替換一行中匹配的內容),以正則表達式匹配爲基礎修改

  1. [root@ansible ~]# ansible cache -m replace -a \
  2. 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 \
  3. regexp="^(ONBOOT=).*" replace="\1\"yes\""'
  4. cache | SUCCESS => {
  5. "changed": true,
  6. "msg": "1 replacements made"
  7. }

7 案例7:綜合練習

7.1 問題

本案例要求:

  • 安裝Apache並修改監聽端口爲8080
  • 修改ServerName配置,執行apachectl -t命令不報錯
  • 設置默認主頁hello world
  • 啓動服務並設開機自啓

7.2 步驟

實現此案例須要按照以下步驟進行。

步驟一:熟悉模塊

1)yum模塊

  1. [root@ansible ~]# ansible other -m yum -a 'name="lrzsz" state=removed'
  2. //lrzsz軟件包名,removed=absent刪除
  3. [root@ansible ~]# ansible other -m yum -a 'name="lrzsz,lftp" state=installed'
  4. //安裝多個軟件包,不寫state默認爲安裝

2)service模塊

  1. [root@ansible ~]# ansible other -m service -a 'name="sshd" enabled="yes" state="started"' //sshd服務名,開機啓動同時啓動這個服務

3)setup模塊

filter 過濾指定的關鍵字(能夠過濾到咱們須要的信息)

  1. [root@ansible ~]# ansible cache -m setup -a 'filter=os'
  2. cache | SUCCESS => {
  3. "ansible_facts": {},
  4. "changed": false
  5. }
  6. [root@ansible ~]# ansible cache -m setup -a 'filter=ansible_distribution'
  7. cache | SUCCESS => {
  8. "ansible_facts": {
  9. "ansible_distribution": "CentOS"
  10. },
  11. "changed": false
  12. }

步驟二:安裝Apache

1)安裝Apache服務設置開機自啓

  1. [root@ansible ~]# ansible cache -m yum -a 'name=httpd state=installed'
  2. [root@ansible ~]# ansible cache -m service -a 'name=httpd enabled=yes state=started'

2)修改端口號爲8080

  1. [root@ansible ~]# ssh cache
  2. Last login: Thu Sep 6 15:30:33 2018 from 192.168.1.51
  3. [root@cache ~]# cat /etc/httpd/conf/httpd.conf | grep Listen
  4. Listen 80
  5. [root@ansible ~]# ansible cache -m lineinfile -a 'path="/etc/httpd/conf/httpd.conf" regexp="^Listen " line="Listen 8080"'cache | SUCCESS => {
  6. "backup": "",
  7. "changed": true,
  8. "msg": "line replaced"
  9. }
  10. [root@ansible ~]# ssh cache
  11. Listen 8080

步驟三:修改ServerName配置,執行apachectl -t命令不報錯

1)沒有修改以前

  1. [root@cache ~]# apachectl -t //有報錯
  2. AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.1.56. Set the 'ServerName' directive globally to suppress this message
  3. Syntax OK

2)修改以後

  1. [root@ansible ~]# ansible cache -m lineinfile -a 'path="/etc/httpd/conf/httpd.conf" regexp="^ServerName " line="ServerName 0.0.0.0"'
  2. cache | SUCCESS => {
  3. "backup": "",
  4. "changed": true,
  5. "msg": "line added"
  6. }
  7. [root@ansible ~]# ssh cache
  8. Last login: Thu Sep 6 15:36:08 2018 from 192.168.1.51
  9. [root@cache ~]# apachectl -t
  10. Syntax OK

步驟四:設置默認主頁爲hello world

  1. [root@ansible ~]# ansible cache -m copy -a 'src=/root/index.html dest=/var/www/html/index.html' ///root/index.html這個頁面能夠本身寫
  2. cache | SUCCESS => {
  3. "changed": true,
  4. "checksum": "22596363b3de40b06f981fb85d82312e8c0ed511",
  5. "dest": "/var/www/html/index.html",
  6. "gid": 0,
  7. "group": "root",
  8. "md5sum": "6f5902ac237024bdd0c176cb93063dc4",
  9. "mode": "0644",
  10. "owner": "root",
  11. "size": 12,
  12. "src": "/root/.ansible/tmp/ansible-tmp-1536219767.29-30682157793478/source",
  13. "state": "file",
  14. "uid": 0
  15. }
相關文章
相關標籤/搜索