持續集成之ansible基礎


1.ansible的發展與簡介
純手工階段--》腳本階段--》工具階段(腳本:不能集中獲得日志,傳入變量的不太靈活)
  -一款簡單的自動化工具
  -無代理(無需要所要管理系統上安裝任何軟件,ssh和python就可以,windows安裝powershell)
  -開源,免費使用

2.ansible功能實現
  -應用部署
  -配置管理
  -任務自動化
特點:
  -無客戶端
  -簡單,易懂
  -擴展性強,可管理從幾十台到數千台節點
  -ssh連接,安全
  -強大社區,大量module,role拿來即用
  -冪等性,不會重復執行

3.puppet vs saltstack vs ansible
saltstack: 依賴於agent,量上升后,管理的難度會上升

 

4.Ansible工作原理
描述:模塊包含inventory、api、modules、plugins,inventory是目標機器的管理清單,保存了SSH連接的信息,可以在模塊上操作取決於modules,是執行任務的管理模塊,playbook是劇本文件,用戶可以通過訪問清單來調用module實現任務執行,也可以通過playbook對任務進行編排,api是可以與其他系統做整合
Inventory: 定義ansible被管理主機的清單
modules: 包括ansible自帶的核心模塊及自定義模塊
playbooks: 劇本,定義ansible多任務配置文件,由ansible自動執行
api: python接口,提供二次開發及系統整合能力
plugins: 完成模塊功能補充,包括連接插件、郵件插件等

 

5.Ansible安裝

[root@ansible1 ~]# yum install epel-release
[root@ansible1 ~]# yum install ansible -y
[root@ansible1 ~]# ansible --version
ansible 2.4.2.0
  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 = /bin/ansible
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] 

 

6.ansible文件組成

-可執行文件:/usr/bin/ansible*
-配置文件目錄:/etc/ansible/
    -ansible-config
    -hosts
    -roles/
-Python lib文件:/usr/lib/python2.7/site-packages/ansible
-Help文檔

注意:host_key_checking一般用於ssh的認證,要關閉,如訪問一台新的主機會提示輸入yes/or,實際是把遠程主機的公鑰復制到本地,
      會對管理帶來很大的麻煩
[root@ansible1 ~]# vim /etc/ansible/ansible.cfg   ##修改配置文件不用重啟進度
host_key_checking = False

  

7.Ansible ad-hoc

ad-hoc:完成實時的,一次性的,簡單的工作
ad-hoc語法規則
    ansible -i iventory   group   -m   module   -a     "mod_args"
                主機文件   組名         模塊             模塊參數
[root@ansible1 inventory]# ansible -i reid reid -m ping
10.0.1.5 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
10.0.1.6 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
                
常用參數:
-i 指定inventory文件
-m 指定模塊
-a 參數命令
-u 指定訪問用戶
-k 輸入密碼
-K 輸入sudo密碼
-f 指定並行數量(默認5)

  

8.Ansible Inventory模塊
ansible通過讀取默認的主機清單配置/etc/ansible/hosts,可以同時連接到多個遠程主機上執行任務,也可以同時使用多個清單文件,甚至可以動態地或者從雲資源中拉取清單,inventory可以通過[startend]指定連續的地址,ssh連接時指定參數如非默認端口

[root@ansible1 ~]# mkdir ansible_test/inventory -pv
[root@ansible1 inventory]# pwd
/root/ansible_test/inventory
[root@ansible1 inventory]# cat reid 
[reid]
10.0.1.5
10.0.1.6
[reid:vars]
ansible_ssh_user=root
ansible_ssh_pass=reid

  

9.ansible常用執行命令
ansible_module_address

[root@ansible1 ~]# ansible-doc -l
[root@ansible1 ~]# ansible-doc shell
    -ping 用於檢測遠程主機是否存活
    -shell 在遠程主機上執行shell命令
    -script 在遠端機器執行本地腳本
    -yum/apt 用於安裝軟件包
    -service 用於管理服務
    -file 用於配置文件屬性
    -copy 復制文件到遠程主機
    -setup 查看遠程主機的基本信息

ping 檢查目標主機是否存活(目標是否可達,信息是否正確)

[root@ansible1 inventory]# cat reid
[reid]
10.0.1.8   ##不存在ip
10.0.1.6
[reid:vars]
ansible_ssh_user=roor   #錯的賬戶
ansible_ssh_pass=reid
[root@ansible1 inventory]# ansible -i reid reid -m ping
10.0.1.8 | UNREACHABLE! => {  
    "changed": false,      ##ip不可達
    "msg": "Failed to connect to the host via ssh: ssh: connect to host 10.0.1.8 port 22: No route to host\r\n", 
    "unreachable": true
}
10.0.1.6 | UNREACHABLE! => {
    "changed": false, 
    "msg": "Authentication failure.",   ##認證有問題
    "unreachable": true
}

shell 在目標主機執行shell命令
chdir: 運行shell之前cd到某個目錄

[root@ansible1 inventory]# ansible -i reid reid -m script -a "/root/ansible_test/script/test.sh" 
10.0.1.5 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 10.0.1.5 closed.\r\n", 
    "stdout": "ansible test\r\n", 
    "stdout_lines": [
        "ansible test"
    ]
}
10.0.1.6 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 10.0.1.6 closed.\r\n", 
    "stdout": "ansible test\r\n", 
    "stdout_lines": [
        "ansible test"
    ]
}

apt/yum 模塊分別用於管理ubuntut和redhat系列軟件包

    -name 軟件包名
    -state 軟件包的狀態(present/installed/absent/removed)

安裝(`present' or `installed', `latest')

刪除(`absent' or `removed')

[root@ansible1 inventory]# ansible -i /root/ansible_test/inventory/reid reid -m yum -a "name=httpd state=present"
\n\nComplete!\n

service 管理目標機器的狀態

    -name: 必選項,服務名稱
    -state: 當前服務執行啟動、停止、重啟(started,stopped,restarted,reloaded)
    -enabled: 是否開機啟動 yes|no
    -runlevel: 運行級別
    -arguments: 給命令行提供一些選項
[root@ansible1 inventory]# ansible -i reid reid -m service -a "name=httpd state=started"

copy 將文件從ansible管理機拷貝到目標機器(空的文件夾不生效)

    -src 源文件
    -dest 目標路徑
    -backup 覆蓋之前,是否備份原文件
    -owner 設定文件/目錄的屬主
    -group 設定文件/目錄的屬組
    -mode  設定文件/目錄的權限

[root@ansible1 ansible_test]# mkdir file
[root@ansible1 ansible_test]# echo hello > file/hello_world
[root@ansible1 inventory]# ansible -i reid reid -m copy -a "src=/root/ansible_test/file/hello_world dest=/tmp owner=1002 group=1002 mode=777"
[root@ansible2 ~]# ll /tmp/hello_world 
-rwxrwxrwx 1 1002 1002 6 Mar 13 06:19 /tmp/hello_world
[root@ansible3 ~]# ll /tmp/hello_world 
-rwxrwxrwx 1 1002 1002 6 Mar 13 06:19 /tmp/hello_world

file 操作目標機器文件屬性,新建/刪除文件,文件夾及鏈接文件

    -group:定義文件/目錄的屬組
    -owner: 定義文件/目錄的屬主
    -mode: 定義文件/目錄的權限
    -path: 必選項,定義文件/目錄的路徑
    -state:定義文件狀態(directory/link/absent/touch/hard)
[root@ansible1 ansible_test]# ansible -i inventory/reid reid -m file -a "path=/tmp/file state=directory"
[root@ansible2 ~]# ls -ld /tmp/file
drwxr-xr-x 2 root root 6 Mar 16 02:00 /tmp/file

setup 搜索系統信息
場景使用:CMDB可以使用setup模塊來獲取硬件信息

-搜集主機的所有系統信息 
     ansible all -m setup
 -搜索系統信息並以主機名為文件名分別保存在/tmp/facts
     ansible all -m setup --tree /tmp/facts
-搜集和內存相關的信息
     ansible all -m setup -a "filter=ansibel_*_mb"
-搜集網卡信息
     ansible all -m setup -a "filter=ansible_eth[0-2]"

[root@ansible1 ansible_test]# ansible -i inventory/reid reid -m setup

  

10.Ansible Playbook(劇本)
Playbooks 與 adhoc 相比,是一種完全不同的運用 ansible 的方式,是非常之強大的.
簡單來說,playbooks 是一種簡單的配置管理系統與多機器部署系統的基礎.與現有的其他系統有不同之處,且非常適合於復雜應用的部署.
Playbooks 可用於聲明配置,更強大的地方在於,在 playbooks 中可以編排有序的執行過程,甚至於做到在多組機器間,來回有序的執行特別指定的步驟.並且可以同步或異步的發起任務.
  -以YAML文件作為載體
  -描述多個任務的集合
  -使用目標主機按照既定順序執行任務以達到期望狀態
  -通過ansible-playbook命令執行

 

11.YAML語言
一種人性化數據格式定義語言
YAML: YAML Ain't a Markup Language
-結構感強,可讀性好
-通用的數據串行化格式

(1)、語法要求
  -以'---'作為起始句
  -大小寫敏感
  -使用縮進表示層級關系
  -縮進時不允許使用Tab鍵許使用空格
  -縮進的空格數目不重要,相同層級元素左側對齊即可

(2)、數據類型
  -對象:鍵值對集合,又稱為映射(mapping)/哈希(hashes)/字典(dictionary)
  -數組:一組按次序排列的值,又稱為序列(sequence)/列表(list)
  -純量(scalars):單個的、不可再分的值,組成數組和字典的數

(3)、YAML語法檢查
Python

python -c 'import yaml,sys;print yaml.load(sys.stdin)'<test.yaml

YAML Lint
link_address

Ansible命令工具

ansible-playbook test.yaml --syntax-check

  

12.Play組成部分


-Target section: 定義將要執行playbook的遠程主機組及屬性
-Variable section: 定義playbook運行時需要使用的變量
-Task section: 定義將要在遠程主機上執行的任務列表

Target section:

-hosts: 定義遠程的主機組
-remote_user: 執行該任務組的用戶,用什么用戶執行
-sudo: 如果設置為yes,以sudo權限執行命令,一般在ubuntu上,默認是root沒密碼
-gather_facts: 默認收集setup模塊傳遞來的變量,可禁止

Variable section:

-vars: 直接寫入對象形式的變量名及其值
-var_files: 引入變量所在的文件,變量都寫在文件中

系統變量:
-Setup模塊(默認gather_facts=yes) 包括組和主機里所有信息

[root@ansible1 ~]# ansible -i ansible_test/inventory/reid reid -m setup

自定義變量:
-Iventory文件
-Playbook文件
-變量文件

在playbook中引用變量:{{var}}雙大括號

Task section:
-由任務組成的列表,其中包含任務名,模塊以及模塊參數

運行Playbook: -i指定inventory文件,后面不接組名

ansible-playbook -i hosts playbook.yml
ansible-playbook -i hosts playbook.yml --syntax-check #檢查語法 
ansible-playbook -i hosts playbook.yml --list-hosts #顯示作用於的機器 
ansible-playbook -i hosts playbook.yml --list-tasks #所執行的任務

  

13.Playbook實例
1).搭建http服務:要求在目標機器安裝Http服務,修改主面,使用能顯示自己的IP及主機名,運行httpd並設置為開機啟動
步驟:
  a.安裝httpd包
  b.將Index.html到遠端服務器
  c.使用setup獲取的變量,修改index.html的內容
  d.啟動Httpd服務
先清除之前的安裝包

[root@ansible1 ~]# ansible -i ansible_test/inventory/reid reid -m yum -a "name=httpd state=absent"

編寫Yaml

[root@ansible1 ~]# cat /root/ansible_test/yaml/httpd.yml 
---
- hosts: reid
  gather_facts: yes
  remote_user: root
  tasks:
    - name: install apache
      yum:
        name: httpd
        state: present
    - name: copy index
      copy:
        src: ../file/index.html
        dest: /var/www/html
    - name: edit index
      shell: sed -i 's/ipaddr/{{ ansible_default_ipv4.address }}/;s/hostname/{{ ansible_hostname }}/' /var/www/html/index.html
    - name: start service
      service:
        name: httpd
        state: started
        enabled: yes 

提供index頁面

[root@ansible1 ansible_test]# cat /root/ansible_test/file/index.html
<html>
<body>
<h1>hello, my ip is ipaddr,my name is hostname.</h1>
</body>
</html>

執行playbook,-v詳細輸出

[root@ansible1 ~]# ansible-playbook -i /root/ansible_test/inventory/reid /root/ansible_test/yaml/httpd.yml -v

運行結果顏色說明
-黃色:執行成功,目錄機器狀態改變
-綠色:執行成功,目標機器狀態不改變
-紅色:執行失敗

  

14.Playbook高級技巧
  -with_items: 任務循環
  -when: 條件判斷
  -register: 變量注冊
  -template: jinja2模板
  -include: 任務引用
  -tags: 任務標簽

with_items
描述:在一個任務task中,遍歷with_items內列表中的元素(由純量或對象集合組成),循環執行任務,直到遍歷結束,with_items本身是一個對象,它的值由列表組成,列表中的元素是純量和對象的集合
如:在playbook中通過yum模塊來安裝很多包,但只能接收一個包名,所以只能在Playbook里寫多個Yum的任務,來安裝每個包,這樣會讓playbook顯得很繁瑣和笨重,這時可以通過with_items來構建循環,把要安裝的包放在一個列表,只需要一個yum任務對列表里的包名進行遍歷執行安裝,直到所有包安裝完成.

變量引用
  -純量: {{item}}
  -對象: {{item.vars}}


純量遍歷

對象遍歷

實例

[root@ansible1 yaml]# cat dict.yml 
---
- hosts: all
  tasks:
    - name: create folder
      file:
        dest: /tmp/{{ item }}
        state: directory
      with_items:
        - test_folder1
        - test_folder2
[root@ansible1 yaml]# ansible-playbook dict.yml --syntax-check
[root@ansible1 yaml]# ansible-playbook -i /root/ansible_test/inventory/reid dict.yml -v
[root@ansible2 ~]# ls /tmp/test_folder*
/tmp/test_folder1:
/tmp/test_folder2:

  

register
描述:將任務運行結果注冊為變量,多用於將shell執行結果注冊
  -命令: register: vars
    -任務結果是字典,其中包含:
    -changed 狀態是否改變
    -failed 是否失敗
    -stdout 標准輸出
    -stderr 標准錯誤

 [root@ansible1 yaml]# cat register.yml 
---
- hosts: all
  tasks:
    - name: test output
      shell: echo 'hello'

#把所有結果注冊成運行變量
[root@ansible1 yaml]# cat register.yml 
---
- hosts: all
  tasks:
    - name: test output
      shell: echo 'hello'
      register: test
    - name: test output
      shell: echo {{ test }}              ##也就是引用上一個shell的結果
[root@ansible1 yaml]# cat register.yml 
---
- hosts: all
  tasks:
    - name: test output
      shell: echo 'hello'
      register: test
    - name: test output
      shell: echo {{ test.stdout }}     ##直接引用輸出的結果hello

  

when
描述:根據變量判斷任務是否執行和register是最佳搭配,變量包括setup、自定義、register中的變量,如通過系統變量來獲取操作系統類型,返回是redhat就使用yum,返回ubuntu就使用apt安裝,通過register來獲取軟件版本,因為有些是要通過shell來獲取的,可以把這些命令返回的結果注冊在一個變量里,判斷軟件版本是否某個版本用於對其作升級


變量判斷形式:注意使用雙引號

-變量是否等於某個值
when: "vars == 'str"
-變量是否有某個值 
when: "'str' in vars"
-變量是否存在
when: vars

實例:判斷目標機器是否ansible2,是的就執行一個echo

[root@ansible1 yaml]# cat when.yml 
---
- hosts: all
  tasks:
    - name: get hostname
      shell: hostname
      register: hostname
    - name: echo
      shell: echo 'I''m ok'
      when: " hostname.stdout == 'ansible2'"
[root@ansible1 yaml]# ansible-playbook -i /root/ansible_test/inventory/reid when.yml -v
TASK [echo] ***********************************************************************************************
skipping: [10.0.1.6] => {"changed": false, "skip_reason": "Conditional result was False"}
changed: [10.0.1.5] => {"changed": true, "cmd": "echo 'I''m ok'", "delta": "0:00:00.006962", "end": "2018-03-21 06:33:50.032821", "rc": 0, "start": "2018-03-21 06:33:50.025859", "stderr": "", "stderr_lines": [], "stdout": "Im ok", "stdout_lines": ["Im ok"]}

  

template
描述:使用jinja2模板,將模板文件從ansible控制機發送到控制機,並對變量賦值,使用場景是有時可能需要修改很多機器的系統和配置文件,一般修改后傳到目標機器或者在目標機器直接修改,這兩種都不能很好的識別ansible的變量,使用shell可能會很繁瑣,所以使用template可以使用變量替代修改項,直接在目標機器替換值,這樣可以非常方便發送自定義的配置文件,還可以使用if和for循環來靈活制定


參數
  -src: 本地jinjia2模板的template文件位置
  -dest: 遠程節點上的絕對路徑,用於放置template文件
  -mode/owner/group/backup
例子

- template:
     src: /mytemplates/foo.conf.j2  #通常j2結尾
     dest: /etc/foo.conf
     mode: 644

需求:創建模板文件,分配到兩台機器上

[root@ansible1 ~]# cat ansible_test/inventory/reid
[reid]
10.0.1.5 name=client1     #
10.0.1.6 name=client2     #會使用到name
[reid:vars]
ansible_ssh_user=root
ansible_ssh_pass=reid

[root@ansible1 ~]# mkdir ansible_test/template

[root@ansible1 ~]# vim ansible_test/template/test.conf.j2
hostname: {{ name }}             ##引用inventory的name
ssh_port: {{ sshport }}
http_port: {{ httpport }}

[root@ansible1 ~]# cat ansible_test/yaml/template.yml
---
- hosts: all
  vars:
    - sshport: 8022
    - httpport: 8080
  tasks:
    - name: create config
      template:
        src: /root/ansible_test/template/test.conf.j2
        dest: /tmp/

[root@ansible1 ~]# ansible-playbook -i /root/ansible_test/inventory/reid /root/ansible_test/yaml/template.yml -v
[root@ansible2 ~]# cat /tmp/test.conf.j2 
hostname: client1   ##inventory中的變量
ssh_port: 8022
http_port: 8080

  

Include
描述:調用外部任務文件,便於任務集統一更新,維護,如果服務器集群越來越大,playbook越來越多,如修改服務器密碼ssh,會放在許多的playbook中,因為這些playbook需要使用這些功能,一旦發生密碼要更新和配置改變,就要修改每個playbook里的配置,這種強度和容錯率是無法估計的,使用include就可以直接調用外部任務文件

- hosts: all
  tasks:
    - include: /ansible/tasks/change_pwd.yml

任務文件只有task section

- name: aaa
  module 1:
- name: bbb
  module 2:
- name: ccc
  module 3:

實例:任務文件只是任務級,沒有host section

[root@ansible1 ~]# cat ansible_test/yaml/when.yml   ##把兩個tasks放在一個新的yml中
---
- hosts: all
  tasks:
    - name: get hostname
      shell: hostname
      register: hostname
    - name: echo
      shell: echo 'I''m ok'
      when: " hostname.stdout == 'ansible2'"
[root@ansible1 ~]# cat ansible_test/yaml/hostname.yml
- name: get hostname
  shell: hostname
  register: hostname
- name: echo
  shell: echo 'I''m ok'
  when: " hostname.stdout == 'ansible2'"

在when.yml中使用include

[root@ansible1 ~]# cat ansible_test/yaml/when.yml
---
- hosts: all
  tasks:
    - include: hostname.yml     #可以使用絕對路徑
[root@ansible1 ~]# ansible-playbook -i ansible_test/inventory/reid ansible_test/yaml/when.yml -v
TASK [echo] *************************************************************************************************************************
skipping: [10.0.1.6] => {"changed": false, "skip_reason": "Conditional result was False"}
changed: [10.0.1.5] => {"changed": true, "cmd": "echo 'I''m ok'", "delta": "0:00:00.005941", "end": "2018-03-21 07:34:17.458820", "rc": 0, "start": "2018-03-21 07:34:17.452879", "stderr": "", "stderr_lines": [], "stdout": "Im ok", "stdout_lines": ["Im ok"]}

  

tags
描述:為任務打標簽,執行playbook可以選擇或路過指定tag任務,如果Playbook實現很多功能,建議相同功能的打同一個標簽,后期方便調用

- name: print sth
  shell: echo 'hello'
  tags: aaa
- 只執行帶aaa標簽的任務
  ansible-playbook test.yml --tags aaa
- 只執行帶aaa,bbb標簽的任務
  ansible-playbook test.yml --tags aaa,bbb
- 跳過帶aaa標簽的任務
  ansible-play test.yml --skip-tags aaa

實例:新一個Playbook,寫兩個任務,打tags,再執行任務來查看

[root@ansible1 ~]# vim ansible_test/yaml/tag.yml
---
- hosts: all
  tasks:
    - name: task1
      shell: echo 'hello reid'
      tags: task1
    - name: task2
      shell: echo 'hello reid2'
      tags: task2

順序執行

[root@ansible1 ~]# ansible-playbook -i ansible_test/inventory/reid ansible_test/yaml/tag.yml -v

只執行task1

[root@ansible1 ~]# ansible-playbook -i ansible_test/inventory/reid ansible_test/yaml/tag.yml --tags task1 -v

跳過

[root@ansible1 ~]# ansible-playbook -i ansible_test/inventory/reid ansible_test/yaml/tag.yml --skip-tags task1 -v

  

15.實戰playbook
搭建HTTP,NFS服務器
-要求:在目標機器中安裝apche&nfs服務,修改主頁使用其能顯示自己的IP,OS版本,主機名以及創建時間,在目標服務器建立兩rw和ro屬性的文件夾並將其通過nfs共享,啟動apache&nfs服務
-步驟:
  1.針對不同系統,安裝apache
  2.將當前的時間注冊成變量(register)
  3.使用模板文件在目標機器生成Index.html
  4.啟動apache服務
  5.include nfs安裝yml文件
  6.在Nfs yml文件里針對不同系統安裝nfs服務
  7.新建不同權限的共享文件夾
  8.添加文件夾到nfs exports
  9.啟動nfs服務

[root@ansible1 ~]# vim ansible_test/yaml/general.yml

 1 ---
 2 - hosts: all
 3   tasks:
 4     - name: install apache in centos
 5       yum:
 6         name: httpd
 7         state: present
 8       when: "ansible_distribution == 'CentOS'"
 9       tags: apache
10     - name: install apache in ubuntu
11       apt:
12         name: apache2
13         state: present
14       when: "ansible_distribution == 'Ubuntu'"
15       tags: apache
16     - name: regisger time
17       shell: date
18       register: cur_time
19       tags: apache
20     - name: transfer file
21       template:
22         src: ../template/index.html.j2
23         dest: /var/www/html/index.html
24       tags: apache
25     - name: start apache
26       service: 
27         name: httpd
28         state: started
29       when: "ansible_distribution == 'CentOS'"
30       tags: apache
31     - name: start apache in ubuntu
32       service:
33         name: apache2
34         state: started
35       when: "ansible_distribution == 'Ubuntu'"
36       tags: apache
37     - include: nfs.yml
38       tags: nfs
View Code

[root@ansible1 ~]# cat ansible_test/yaml/nfs.yml

 1 ---
 2 - name: install nfs in centos
 3   yum:
 4     name: nfs-utils
 5     state: present
 6   when: "ansible_distribution == 'CentOS'"
 7 - name: install nfs in ubuntu
 8   apt:
 9     name: nfs-kernel-server
10     state: present
11   when: "ansible_distribution == 'Ubuntu'"
12 - name: create folder
13   file:
14     dest: "/{{ item.folder }}"
15     mode: "{{ item.mode }}"
16     state: directory
17   with_items:
18     - { folder: data_rw, mode: 777 }
19     - { folder: data_ro, mode: 755 }
20 - name: export folder
21   shell: echo '/{{ item }} *(rw,sync,no_subtree_check,no_root_squash)' >> /etc/exports
22   with_items:
23     - data_rw
24     - data_ro
25 - name: start nfs service in centos
26   service: 
27     name: nfs
28     state: started
29   when: "ansible_distribution == 'CentOS'"
30 - name: start nfs service in ubuntu
31   service:
32     name: nfs-kernel-service
33     state: started
34   when: "ansible_distribution == 'Ubuntu'"
View Code

掛載

[root@ansible1 ~]# mkdir /data_rw /data_ro
[root@ansible1 ~]# mount -t nfs 10.0.1.5:/data_rw /data_rw
[root@ansible1 ~]# mount -t nfs 10.0.1.6:/data_ro /data_ro

[root@ansible1 ~]# df -h
Filesystem         Size  Used Avail Use% Mounted on
10.0.1.5:/data_rw   30G  1.4G   29G   5% /data_rw
10.0.1.6:/data_ro   30G  1.3G   29G   5% /data_ro
[root@ansible1 ~]# ls -ld /data_r*
drwxr-xr-x 2 root root  6 Mar 22 01:44 /data_ro  ##777
drwxrwxrwx 2 root root 18 Mar 22 01:58 /data_rw  ##755

[root@ansible1 ~]# ls -ld /data_r*
drwxr-xr-x 2 root root  6 Mar 22 01:44 /data_ro
drwxrwxrwx 2 root root 18 Mar 22 01:58 /data_rw

測試

[root@ansible1 ~]# touch /data_rw/reid
[root@ansible2 ~]# ls /data_rw
reid

 

16.Ansible Roles

    -用於實現某特定功能(高級的include)
  -是變量,模板,文件,任務的集合
  -強依賴文件目錄結構
  -默認目錄是:/etc/ansible/roles
  -使用:如有docker和k8s兩個role

 ---
 - hosts: all
   roles:
     - docker
     - k8s

從ansible galaxy網站下載roles : ansible_web   ansible_explore

下載role(如ceph.ceph-defaults:ceph是username,ceph-defaults是rolename,-p是保存的位置)

ansible-galaxy install -p ./role username.rolename

查看role的具體信息

ansible-galaxy info username.rolename

  

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM