一、常用模塊介紹
1、File模塊
#在目標主機創建文件或目錄,並賦予其系統權限; - name:create a file file: ‘path=/root/foo.txt state=touch mode=0755 owner=foo group=foo'
2、Copy模塊
# 實現Ansible服務端到目標主機的文件傳送
- name:copy a file copy: 'remote_src=no src=roles/testbox/files/foo.sh dest=/root/foo.sh mode=0644 force=yes'
3、Stat模塊
# 獲取遠程文件狀態信息 - name:check if foo.sh exists stat: ‘path=/root/foo.sh' register: script_stat
4、Debug模塊
# 打印語句到Ansible執行輸出 - debug:msg=foo.sh exists when:script_stat.stat.exists
5、Command/Shell模塊,推薦shell
# 用來執行Linux目標主機命令行 - name:run the script command: "sh/root/foo.sh" - name:run the script shell: "echo‘test'>/root/test.txt"
6、Template模塊
# 實現Ansible服務端到目標主機的jinja2模板傳送 - name: write the nginx config file template: src=roles/testbox/templates/nginx.confj2 dest=/etc/nginx/nginx.conf
7、Packaging模塊
8、Service模塊
# 管理目標主機系統服務 - name:start nginx service service: name=nginx state=started
9、模塊應用
二、常用模塊案例操作
1、加載ansible
[deploy@ansible ~]$ source /home/deploy/.py3-a2.5-env/bin/activate (.py3-a2.5-env) [deploy@ansible ~]$ source .py3-a2.5-env/ansible/hacking/env-setup -q
2、配置測試機
[root@testbox ~]# useradd foo [root@testbox ~]# useradd deploy [root@testbox ~]# mkdir /etc/nginx [root@testbox ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
3、file模塊
# 在測試機上創建/root/foo.txt 並賦予權限;
(.py3-a2.5-env) [deploy@ansible ~]$ cd test_playbooks/
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ vim roles/testbox/tasks/main.yml
- name: create a file file: 'path=/root/foo.txt state=touch mode=0755 owner=foo group=foo'
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ ansible-playbook -i inventory/testenv ./deploy.yml
去測試機查看:
[root@testbox ~]# ll foo.txt
-rwxr-xr-x. 1 foo foo 0 4月 3 23:30 foo.txt
4、copy模塊
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ mkdir roles/testbox/files
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ vim roles/testbox/files/foo.sh
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ vim 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'
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ ansible-playbook -i inventory/testenv ./deploy.yml
去測試機查看:
[root@testbox ~]# ll foo.sh
-rw-r--r--. 1 root root 27 4月 3 23:45 foo.sh
5、stat模塊和debug模塊
stat模塊判斷遠程主機文件是否存在,debug模塊判斷文件如果存在,就輸出一句話foo.sh exists
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ vim roles/testbox/tasks/main.yml
- name: check if foo.sh exists stat: 'path=/root/foo.sh' register: script_stat - debug: msg="foo.sh exists" when: script_stat.stat.exists
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ ansible-playbook -i inventory/testenv ./deploy.yml
6、Command/Shell模塊
command模塊遠程執行腳本:
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ vim roles/testbox/tasks/main.yml
- name: Print server name and user to remote testbox shell: "echo 'Currently {{ user }} is logining {{ server_name }}' > {{ output }}" - name: run the script command: 'sh /root/foo.sh'
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ ansible-playbook -i inventory/testenv ./deploy.yml
7、template、packaging、service模塊
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ vim inventory/testenv
[testservers] test.example.com [testservers:vars] server_name=test.example.com user=root output=/root/test.txt server_name=test.example.com port=80 user=deploy worker_processes=4 max_open_file=65505 root=/www
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ mkdir roles/testbox/templates
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ vim roles/testbox/templates/nginx.conf.j2
# For more information on configuration, see:
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 }};
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;
}
}
}
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ vim roles/testbox/tasks/main.yml
- name: write the nginx configfile template: src=roles/testbox/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf - name: ensure nginx is at the latest version yum: pkg=nginx state=latest - name: start nginx service service: name=nginx state=started
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ ansible-playbook -i inventory/testenv ./deploy.yml
去測試機查看:
# 可見nginx已成功安裝、啟動,並已經應用了模板 [root@testbox ~]# ls /etc/nginx/ conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf nginx.conf.rpmnew scgi_params uwsgi_params win-utf [root@testbox ~]# [root@testbox ~]# head /etc/nginx/nginx.conf # For more information on configuration, see: user deploy; worker_processes 4; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 65505; ...... ...... [root@testbox ~]# netstat -ntlp |grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8836/nginx: master