CICD(三)Ansible常用模块以及案例

时间:2022-07-25
本文章向大家介绍CICD(三)Ansible常用模块以及案例,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Ansible常用模块以及案例

常用模块

  • file模块: 对目标主机创建目录或者文件,并赋予权限
- name: create a file
  file: 'path=/root/aaa.txt state=touch mode=0755 owner=foo group=foo'
  • copy模块:实现ansible到目标机之间的文件传输
- name: copy a file
  copy: 'remote_src=no src=roles/testbiox/foo.sh dest=/root/foo.sh mode=0644 force=yes'
  • stat模块: 获取远程文件的状态信息
- name: check fool.sh exists
  stat: 'path=/root/fool.sh'
  register: script_stat
  • debug模块: 打印执行输出
- debug: msg=fool.sh exists
  when: script_stat.stat.exists
  • command/shell: 用来执行shell主机命令
- name: run a script
  command: "sh /root/foo.sh"
- name: run the scripts
  shell: "echo 'test' > /root/foo.txt"
  • template: 实现ansible服务端到目标主机的jinja2模板传送
- name: transport template jinja2
  template: src=roles/testbox/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
  • package: 调用yum/apt命令
- name: yum install package
  yum: pkg=nginx state=latest

- name: yum install package
 apt: pkg=nginx state=latest
  • service模块: 管理init服务
- name: start nginx service
  service: name=nginx state=started

案例

综合上述的所有模块

  • 目标机的初始化工作 [root@centos7-node5 ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm [root@centos7-node5 ~]# useradd foo [root@centos7-node5 ~]# useradd deploy [root@centos7-node5 ~]# mkdir /etc/nginx
  • ansible主机的工作 [root@centos7-node3 ~]# su - deploy [deploy@centos7-node3 ~]$ source .py3-a2.5-env/bin/activate (.py3-a2.5-env) [deploy@centos7-node3 ~]$ source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q (.py3-a2.5-env) [deploy@centos7-node3 ~]$ cd test_playbooks/ (.py3-a2.5-env) [deploy@centos7-node3 test_playbooks]$ mkdir roles/testbox/files (.py3-a2.5-env) [deploy@centos7-node3 test_playbooks]$ vim roles/testbox/files/foo.sh echo "test scripts" (.py3-a2.5-env) [deploy@centos7-node3 test_playbooks]$ vim inventory/testenv #追加 server_name=localhost port=80 user=deploy work_process=2 max_open_file=65505 root=/www
  • playbook
(.py3-a2.5-env) [deploy@centos7-node3 test_playbooks]$ mkdir roles/testbox/tempaltes
(.py3-a2.5-env) [deploy@centos7-node3 test_playbooks]$ vim roles/testbox/tempaltes/nginx.conf.j2 
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 65;  
    server {  
        listen {{ port }} default_server;  
        server_name {{ server_name }};  
        location / {  
            root {{ root }};  
            index index.html index.htm;  
        }  
        error_page 404 /404.html;  
        location = /404.html {  
            root /usr/share/nginx/html;  
        }  
        error_page 500 502 503 504 /50x.html;  
        location = /50x.html {  
            root /usr/share/nginx/html;  
        }    
    }    
}
  • yaml文件
(.py3-a2.5-env) [deploy@centos7-node3 ~]$ vim test_playbooks/roles/testbox/tasks/main.yaml
- name: Print server name and user to remote testbox
  shell: "echo 'Currently {{ user }} is logging {{ servername }}' >> {{output}}"
- name: create a file
  file: 'path=/root/foo.txt state=touch mode=0755 owner=foo group=foo'
- name: copy a file to remote
  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: write the nginx config file
  template: src=roles/testbox/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
- name: yum install nginx latest
  yum: pkg=nginx state=latest
- name: service enable nginx
  service: name=nginx state=started
  • 执行任务
(.py3-a2.5-env) [deploy@centos7-node3 test_playbooks]$ ansible-playbook -i inventory/testenv deploy.yml