[Jenkins + Ansible + Gitlab automated deployment Musketeers] study notes - Chapter 3 3-6 ~ 3.7 Ansible playbooks common module introduction and demonstration

A, Ansible playbooks common module Introduction

1.1, File module

Here Insert Picture Description
name: Name The name of the task
file: indicates the type of task
path: representation generation to the file destination host in the path of the
state = touch: touch expressed the need to create a file
mode: represents the file permissions
owner: indicates that the document belongs Username
group: indicates that the file belongs to the user group name

1.2, Copy module

Here Insert Picture Description
name: Name The name of the task
copy: representation of this is to copy the files
remote_src = no: means we need to copy the machine's files to the target host among
src: represents the path of the source file
dest: indicates that the file path to the target
mode : Indicates the file permissions
force = yes: expressed enforcement

1.3, Stat module

Here Insert Picture Description
Check whether a file exists
Here Insert Picture Description
name: Name The name of the task
stat: indicates that the call is a stat module
register: Specifies whether to pass in front of the state to check the file exists variable here

1.4, Debug Module

Print "foo.sh exists" when script_stat.stat.exists statement is true.
Here Insert Picture Description
debug: msg: the definition of conditional statements
when: express conditional statements

1.5, Command / Shell module

A means of: performing file foo.sh
second sentence means: to print test test.txt file ( ">" for the redirection symbol)
the difference between the two statements is that the shell can be used redirection operator;
Here Insert Picture Description

1.6, Template module

Here the main attention in the transmission process below, the variables in the file can be called to the parameters defined in the template.
Here Insert Picture Description
src: indicates the address of the source file
dest: indicates the address of the target host transfer

1.7, Packaging module

The following statement indicates a nginx install the latest version of
Here Insert Picture Description
pkg: indicates the name of the installation package of the
state: represents the version number

1.8, Service Module

The following statement will indicate on Nginx service
Here Insert Picture Description
name: Specifies the name of the service
state: expressed the need to reach a state of service

1.9, a comprehensive case the above modules

Here Insert Picture Description

Two, Ansible playbooks case presentations

2.1, ansible ready to host

First, switch to deploy user ansible host, and then use the following two statements, start ansible in a virtual environment Python3.6 in
the figure below, I've started.
Here Insert Picture Description

2.2, ready on testbox

# 进入到testbox主机界面
# 创建两个系统用户
useradd foo
useradd deploy
# 创建一个nginx的文件目录
mkdir /etc/nginx
# 安装一个Nginx的yum源,防止playbook运行安装的时候出错
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# 安装完后退出testbox
exit

Here Insert Picture Description

2.3, create a case file -File module

2.3.1, written main.yml file

# 进入到test_playbooks文件夹
cd test_playbooks/
# 编辑main.yml文件
vim roles/testbox/tasks/main.yml

Here Insert Picture Description
Add a task
Here Insert Picture Description

2.3.2 Test

# 测试上面编写的代码
ansible-playbook -i inventory/testenv ./deploy.yml
- name: create a file
  file: 'path=/root/foo.txt state=touch mode=0755 owner=foo group=foo'

Here Insert Picture Description
Tips:

# 可以使用下面的代码去查看是否创建了该文件,如果创建会返回该文件的相关信息
ssh [email protected] ls -al /root/foo.txt

Here Insert Picture Description

2.4, the file copy module case -Copy

2.4.1 Environmental ready

First, create a folder files in the / roles / testbox folder

# 创建files文件夹
mkdir roles/testbox/files
# 创建并编写foo.sh文件
vim roles/testbox/files/foo.sh

Here Insert Picture Description

2.4.2, written main.yml file

# 编写main.yml文件
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'

Here Insert Picture Description

2.4.3, test scripts

Here Insert Picture Description

2.5, case -Stat, Debug Module

2.5.1, written main.yml file

- name: check if foo.sh is exists
  stat: 'path=/root/foo.sh'
  register: script_stat
- debug: msg="foo.sh exists"
  when: script_stat.stat.exists

Here Insert Picture Description

2.5.2 Test

Here Insert Picture Description

2.6, case -Command module

2.6.1, written main.yml file

- name: run the script
  command: 'sh /root/foo.sh'

Here Insert Picture Description

2.6.2 Test

Here Insert Picture Description

2.7, case -Template module

2.7.1, written testenv file

First, we need to prepare the environment, add some configuration items in testenv manifest file
Here Insert Picture Description
to create templates directory under roles / testbox / directory

# 创建文件夹
mkdir roles/testbox/templates
# 创建Nginx的模板文件
vim roles/testbox/templates/nginx.conf.j2

2.7.2, written nginx.conf.j2 file

# 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;  
        }  
  
    }  
  
}

2.7.3, written main.yml file

The following files added three tasks, namely:

  • 1. Nginx configuration files to the target server template template way
  • 2. To ensure service is the latest version of Nginx
  • 3. Start Nginx Service
- name: write the nginx config file
  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

Here Insert Picture Description

2.7.4 Test

Here Insert Picture Description
After a successful run using the following statement may be generated by the Template View module, from the screenshot below can be seen on the variable has been replaced before nginx.conf.j2 variable configuration on testenv.

ssh [email protected] cat /etc/nginx/nginx.conf

Here Insert Picture Description
See if testbox host Nginx service is turned on

ssh [email protected] ps -ef | grep nginx

Here Insert Picture Description
Since you can see the consistency of the above cases common module has been implemented successfully! ! !

Published 76 original articles · won praise 16 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_38657051/article/details/100746864