Common modules and cases of CICDAnsible

Ansible common modules and cases

Common modules

  • File module: create a directory or file for the target host, and give permissions
- name: create a file
  file: 'path=/root/aaa.txt state=touch mode=0755 owner=foo group=foo'
  • copy module: file transfer from ansible to target
- name: copy a file
  copy: 'remote_src=no src=roles/testbiox/ dest=/root/ mode=0644 force=yes'
  • stat module: get status information of remote files
- name: check exists
  stat: 'path=/root/'
  register: script_stat
  • debug module: print execution output
- debug: exists
  when: script_stat.stat.exists
  • command/shell: used to execute shell host commands
- name: run a script
  command: "sh /root/"
- name: run the scripts
  shell: "echo 'test' > /root/foo.txt"
  • Template: implement jinja2 template transfer from ansible service end to target host
- name: transport template jinja2
  template: src=roles/testbox/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
  • package: call the yum/apt command
- name: yum install package
  yum: pkg=nginx state=latest

- name: yum install package
 apt: pkg=nginx state=latest
  • Service module: manage init service
- name: start nginx service
  service: name=nginx state=started


Integrate all the above modules

  • Initialization of target machine

    [root@centos7-node5 ~]# rpm -Uvh
    [root@centos7-node5 ~]# useradd foo
    [root@centos7-node5 ~]# useradd deploy
    [root@centos7-node5 ~]# mkdir /etc/nginx
  • ansible host work

    [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/
    echo "test scripts"
    (.py3-a2.5-env) [deploy@centos7-node3 test_playbooks]$ vim inventory/testenv  #Add
  • 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/;  
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 file
(.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/ dest=/root/ mode=0644 force=yes'
- name: check if exists
  stat: 'path=/root/'
  register: script_stat
- debug: msg=" exists"
  when: script_stat.stat.exists
- name: run the script
  command: 'sh /root/'
- 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
  • Perform task
(.py3-a2.5-env) [deploy@centos7-node3 test_playbooks]$ ansible-playbook -i inventory/testenv deploy.yml 

Tags: Linux Nginx ansible yum shell

Posted on Thu, 04 Jun 2020 09:22:46 -0700 by impulse()