pzczyy 2020-06-04
- name: create a file file: ‘path=/root/aaa.txt state=touch mode=0755 owner=foo group=foo‘
- name: copy a file copy: ‘remote_src=no src=roles/testbiox/foo.sh dest=/root/foo.sh mode=0644 force=yes‘
- name: check fool.sh exists stat: ‘path=/root/fool.sh‘ register: script_stat
- debug: msg=fool.sh exists when: script_stat.stat.exists
- name: run a script command: "sh /root/foo.sh" - name: run the scripts shell: "echo ‘test‘ > /root/foo.txt"
- name: transport template jinja2 template: src=roles/testbox/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
- name: yum install package yum: pkg=nginx state=latest - name: yum install package apt: pkg=nginx state=latest
- name: start nginx service service: name=nginx state=started
综合上述的所有模块
目标机的初始化工作
[ ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm [ ~]# useradd foo [ ~]# useradd deploy [ ~]# mkdir /etc/nginx
ansible主机的工作
[ ~]# su - deploy [ ~]$ source .py3-a2.5-env/bin/activate (.py3-a2.5-env) [ ~]$ source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q (.py3-a2.5-env) [ ~]$ cd test_playbooks/ (.py3-a2.5-env) [ test_playbooks]$ mkdir roles/testbox/files (.py3-a2.5-env) [ test_playbooks]$ vim roles/testbox/files/foo.sh echo "test scripts" (.py3-a2.5-env) [ test_playbooks]$ vim inventory/testenv #追加 server_name=localhost port=80 user=deploy work_process=2 max_open_file=65505 root=/www
(.py3-a2.5-env) [ test_playbooks]$ mkdir roles/testbox/tempaltes (.py3-a2.5-env) [ 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; } } }
(.py3-a2.5-env) [ ~]$ 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) [ test_playbooks]$ ansible-playbook -i inventory/testenv deploy.yml