Ansible Playbooks常用模块

一、常用模块介绍

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模块

image


8、Service模块

# 管理目标主机系统服务

- name:start nginx service
  service: name=nginx state=started


9、模块应用

image


二、常用模块案例操作

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
posted @ 2020-04-04 00:27  米兰的小铁將  阅读(3205)  评论(0编辑  收藏  举报