Ansible playbooks常用模塊案例操作


打開git bash 連接ansible服務器,然后進入deploy用戶

#ssh root@192.168.96.188

進入python3.6虛擬環境

#su - deploy

#source .py3-a2.5-env/bin/activate

加載ansible 2.5版本

#source .py3-a2.5-env/ansible/hacking/env-setup -q

驗證ansible加載效果

#ansible-playbook --version

1、File模塊

登錄到目標主機進行預配置工作

#ssh root@test.example.com

創建兩個系統用戶

# useradd foo
# useradd deploy

 

登出,回到ansible的主機,進入到test_playbooks目錄。編輯主任務文件,添加測試任務。保存退出

# vi roles/testbox/tasks/main.yml

- name: create a file            # 創建文件file
  file: 'path=/root/foo.txt state=touch mode=0755 owner=foo group=foo'
 
#path為文件路徑 #state為所用命令 #mode 為文件權限 #owner 為設置的系統用戶名稱 #group 為宿主

執行測試任務

# ansible-playbook -i inventory/testenv ./deploy.yml

查看文件是否創建成功

# ssh root@test.example.com ls -l /root/foo.txt

創建安裝nginx需要的文件,復制下面的腳本,進行保存

# vi roles/testbox/files/nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

2、Copy模塊

先創建一個files目錄,在目錄下創建一個腳本文件,添加一下內容,保存退出

# mkdir roles/testbox/files

# vi roles/testbox/files/foo.sh

echo "This is a test script"

編輯主任務配置文件,保存退出。

 # vi roles/testbox/tasks/main.yml

- name: copy a file
  copy: 'remote_src=no src=roles/testbox/files/foo.sh dest=/root/foo.sh mode=0644 force=yes'

#remote_src 定義當前拷貝任務是將ansible本地server文件傳送到目標主機中

#src 本地文件 傳送  #dest 目標主機文件

#mode 設置文件權限  #force 定義拷貝任務強制執行

編輯好文件之后,執行任務

# ansible-playbook -i inventory/testenv ./deploy.yml

3、Stat模塊、Debug模塊

編輯主任務配置文件,添加以下內容

#  vi roles/testbox/tasks/main.yml

# 獲取遠程foo.sh的文件狀態信息
- name: check if foo.sh exists
  stat: 'path=/root/foo.sh'
  register: script_stat
# 將stat文件信息,放到when的判斷語句中,如果判斷成功,dubug輸出foo.sh exists
- debug: msg="foo.sh exists"
  when: script_stat.stat.exists

編輯好文件之后,執行任務

# ansible-playbook -i inventory/testenv ./deploy.yml

4、Command/Shell模塊

編輯主任務配置文件,添加以下內容

#  vi roles/testbox/tasks/main.yml

# 遠程執行foo.sh腳本
- name: run the script
  command: 'sh /root/foo.sh'

編輯好文件之后,執行任務

# ansible-playbook -i inventory/testenv ./deploy.yml

5、Template模塊、Packaging模塊、Service模塊

添加一些參數到testenv的文件當中,添加如下參數

# vi vi inventory/testenv

server_name=test.example.com
port=80
user=deploy
worker_processes=4
max_open_file=65505
root=/www

創建templates目錄,然后創建一個nginx.conf.j2的模塊文件,添加配置信息

# mkdir roles/testbox/templates

# vi roles/testbox/templates/nginx.conf.j2

# For more information on configuration, see:
user              {{ user }};                  # user變量
worker_processes  {{ worker_processes }};      # 變量

error_log  /var/log/nginx/error.log;

pid        /var/run/nginx.pid;

events {
    worker_connections  {{ max_open_file }};   #變量
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    # Load config files from the /etc/nginx/conf.d directory
    # The default server is in conf.d/default.conf
    #include /etc/nginx/conf.d/*.conf;
    server {
        listen       {{ port }} default_server;      # 端口變量
        server_name  {{ server_name }};          #服務器名稱變量

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   {{ root }};          # root變量
            index  index.html index.htm;
        }

        error_page  404              /404.html;
        location = /404.html {
            root   /usr/share/nginx/html;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

    }

}

編輯主任務配置文件,添加以下下內容

#  vi roles/testbox/tasks/main.yml

# 將模板寫入目標主機配置文件
- name: write the nginx config file
  template: src=roles/testbox/templates/nginx.conf.j2 dest=/etc/nginx/conf.d/default.conf

# yum安裝nginx
- name: ensure nginx is at the latest version
  yum: pkg=nginx state=latest

# 啟動nginx服務
- name: start nginx service
  service: name=nginx state=started

編輯好文件之后,執行任務

# ansible-playbook -i inventory/testenv ./deploy.yml

檢查nginx.conf.j2文件的參數變量,是否寫入nginx主配置文件

# ssh root@test.example.com cat /etc/nginx/conf.d/default.conf

檢查遠程主機nginx是否啟動

# ssh root@test.example.com ps -ef  | grep nginx

main.yml文件

- name: Print server name and user to remote testbox
  shell: "echo 'Currently {{ user }} is logining {{ server_name }}' > {{ output }}"
- name: create a file
  file: 'path=/root/foo.txt state=touch mode=0755 owner=foo group=foo'
- name: copy a file
  copy: 'remote_src=no src=roles/testbox/files/foo.sh dest=/root/foo.sh mode=0644 force=yes'
- name: check if foo.sh exists
  stat: 'path=/root/foo.sh'
  register: script_stat
- debug: msg="foo.sh exists"
  when: script_stat.stat.exists
- name: run the script
  command: 'sh /root/foo.sh'
- name: Create a directory if it does not exist
  file: 'path=/etc/nginx state=directory mode=0755'
- name: write the nginx config file
  template: src=roles/testbox/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
- name: copy a file
  copy: 'remote_src=no src=roles/testbox/files/nginx.repo dest=/etc/yum.repos.d/nginx.repo mode=0644 force=yes'
- name: ensure nginx is at the latest version
  yum: pkg=nginx state=latest
- name: start nginx service
  service: name=nginx state=started



- name: Print server name and user to remote testbox
  shell: "echo 'Currently {{ user }} is logining {{ server_name }}' > {{ output }}"

  # 遠程創建文件
- name: create a files                                                                             
  file: 'path=/root/foo.txt state=touch mode=0755 owner=foo group=foo'

  # 將本地的文件拷貝到遠程主機
- name: copy a files                                                                               
  copy: 'remote_src=no src=roles/testbox/files/foo.sh dest=/root/foo.sh mode=0644 force=yes'

  # 獲取文件狀態
- name: check if foo.sh exists
  stat: 'path=/root/foo.sh'
  register: script_stat

  # 判斷文件是否存在
- debug: msg="foo.sh exists"
  when: script_stat.stat.exists

  # 遠程執行腳本文件
- name: run the script
  command: 'sh /root/foo.sh'

  # 創建一個nginx的目錄
- name: Create a directory if it does not exist
  file: 'path=/etc/nginx state=directory mode=0755'

  # 從本地模板中寫入nginx.conf文件
- name: write the nginx config file
  template: src=roles/testbox/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf

  # 拷貝本地nginx安裝需要的腳本
- name: copy a file
  copy: 'remote_src=no src=roles/testbox/files/nginx.repo dest=/etc/yum.repos.d/nginx.repo mode=0644 force=yes'

  # yum安裝nginx
- name: ensure nginx is at the latest version
  yum: pkg=nginx state=latest

  # 啟動nginx
- name: start nginx service
  service: name=nginx state=started


免責聲明!

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



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