79、ansible-----playbook2

1、作业

复制代码
[root@test1 opt]# vim test2.yaml 

- name: this is mulu
  hosts: 192.168.168.22
  gather_facts: false
  vars:            ##定义变量test
    test:
      - /opt/test1    ##对变量进行赋值
      - /opt/test2
      - /opt/test3
      - /opt/test4
  tasks:
    - name: create mulu
      file:
        path: "{{item}}"
        state: directory
      with_items: "{{test}}"


- name: test2
  hosts: 192.168.168.22
  gather_facts: false
  tasks:
    - name: create mulu
      file:
        path: "{{item}}"
        state: directory
      with_items: [/opt/test1,/opt/test2,/opt/test3,/opt/test4]




[root@test1 opt]# ansible-playbook test2.yaml 

2、tags模块 可以给任务定义标签,可以根据标签来运行指定的任务

复制代码
[root@test1 opt]# vim test3.yaml

#标签的类型:
#always:设定了标签名为always,除非指定跳过这个标签,否则该任务将始终运行,即使指
定了标签还会运行
#never:始终不允许的任务,指定标签名,never可以运行。
#debug:用于调试
#setup:收集主机的信息
#标签名也可以自定义:tags
- hosts: 192.168.168.22
  gather_facts: false
  tasks:
    - name: debug-test1
      debug:
        msg: "cow"
      tags:
        - debug
    - name: always-test1
      debug:
        msg: "always-run"   ##不指定也会运行
      tags:
        - always
    - name: setup-test1
      debug:
        msg: "setup"
      tags:
        - setup
    - name: never-test1  ##除非指定,否则不运行
      debug:
        msg: "never-run"
      tags:
        - never

[root@test1 opt]# ansible-playbook test3.yaml --tags=never

[root@test1 opt]# ansible-playbook test3.yaml --skip-tags=always
复制代码
[root@test1 opt]# ansible-playbook test3.yaml --tags="debug","setup"
复制代码
[root@test1 opt]# ansible-playbook test3.yaml --tags="debug","setup" --skip-tags=always

3、自定义标签

复制代码
[root@test1 opt]# vim test4.yaml

- hosts: 192.168.168.22
  gather_facts: false
  remote_user: root
  tasks:
    - name: fuzhiwenjian
      copy: src=/etc/hosts dest=/opt/hosts
      tags:
        - zlm
    - name: touch file
      file: path=/opt/test1 state=touch
      tags:
        hpc


[root@test1 opt]# ansible-playbook test4.yaml --tags=hpc  ##指定标签执行
复制代码
[root@test1 opt]# yum -y install httpd

[root@test1 opt]# cd /etc/httpd/conf
[root@test1 conf]# ls
httpd.conf  magic
[root@test1 conf]# cp httpd.conf /opt/httpd.conf.j2
[root@test1 conf]# 
[root@test1 conf]# cd /opt/
[root@test1 opt]# ls
123  345  httpd.conf.j2  test2.yaml  test3.yaml  test4.yaml


[root@test1 opt]# vim test5.yaml

#模板,对应用的配置文件初始化:templates模块,jinja组件,把编译过的模板文件传送给
目标文件。
- hosts: 192.168.168.22
  gather_facts: false
  remote_user: root
  vars:
    - pg: httpd
    - sv: httpd
  tasks:
    - name: install httpd
      yum: name={{pg}}
    - name: editon conf
      template: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
      notify:
        - restart httpd
  handlers:
    - name: restart httpd
      service: name={{sv}} state=restarted

[root@test1 opt]# vim httpd.conf.j2 

 42 Listen {{http_port}}

 95 ServerName {{server_name}}

119 DocumentRoot "{{root_dir}}"

[root@test1 ansible]# vim hosts 


[web]
## alpha.example.org
## beta.example.org
192.168.168.22 http_port=192.168.168.22:80 server_name=www.xy.com:80 root_dir=/etc/httpd/htdocs
192.168.168.23 ansible_port=22 ansible_user=root ansible_password=123

[root@test2 httpd]# mkdir htdocs

[root@test2 httpd]# yum -y remove httpd

[root@test1 opt]# ansible-playbook test5.yaml
[root@test2 httpd]# netstat -antp | grep httpd
[root@test2 httpd]# netstat -antp | grep 80
[root@test2 httpd]# curl 192.168.168.22
[root@test2 htdocs]# cd /var/www/html/
[root@test2 html]# ls
index.html
[root@test2 html]# ls
index.html
[root@test2 html]# cat index.html 
this is httpd
[root@test2 html]# find / -type f -name index.html
/usr/share/httpd/noindex/index.html
[root@test2 html]# cd /usr/share/httpd/noindex/
[root@test2 noindex]# ls
css  images  index.html
[root@test2 noindex]# cat index.html 
[root@test2 noindex]# echo 123 > index.html 
[root@test2 noindex]# curl 192.168.168.22
123

nginx 传参的方式,端口8080 servername:www.xy.com

4、nsible为了层次化,结构化的组织playbook,使用roles(角色)通过层次化自动装载变量,任务和处理器等等。

roles把变量,任务和模块的文件单独放在各个不同的目录中,通过rolse一键的编排。

复制代码
mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/php/{files,templates,tasks,handlers,vars,defaults,meta} -p

[root@test1 ansible]# yum -y install tree
[root@test1 ansible]# cd roles/
[root@test1 roles]# tree



roles:
├── httpd            ##角色名称,自定义
│   ├── defaults     ##存放配置文件的目录,可以不写
│   ├── files        ##存放copy模块或者script
│   ├── handlers     ##存放处理器文件的目录
│   ├── meta         ##保存角色源信息的文件
│   ├── tasks        ##保存任务的文件
│   ├── templates    ##保存模板的文件
│   └── vars         ##保存变量的文件
就是把原来写一个yaml的配置,分开--------》不同的目录----------》保存在一个名字的yaml里面。
执行的时候调用不同目录的同一个yaml的文件
main.yaml

[root@test1 roles]# touch /etc/ansible/roles/php/{files,templates,tasks,handlers,vars,defaults,meta}/main.yaml
[root@test1 roles]# touch /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta}/main.yaml
[root@test1 roles]# touch /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta}/main.yaml
[root@test1 roles]# tree

总结:

ansible:14个模块必须掌握,熟练。

playbook:剧本能够定义一般的远程部署相关的配置即可

​ 了解条件判断,循环

​ tags的作用 标签的 系统标签:always never 自定义

​ templates:了解即可

​ roles:了解即可。

作业、

配置主机清单,实现免密钥对登录。声明ip地址列表

1、在目标主机批量创建目录:/opt/test1 /opt/test2 /opt/test3

2、从主机批量复制文件,123 456 789,分别输出到指定的123-->test1 456---->test2 789---->test3

指定主机为192.168.233.20.

3、创建一个nginx.conf文件,改配置文件可以实现upstream反向代理 复制到nginx1

4、分别在nginx2和nginx3上配置页面: test1 test2

5、在主机访问目标主机nginx1,实现负载均衡。

以上步骤全部用ansible远程完成!

复制代码
[root@test1 opt]# vim test6.yaml

#配置主机清单,实现免密钥对登录。声明ip地址列表
#1、在目标主机批量创建目录:/opt/test1 /opt/test2 /opt/test3
#2、从主机批量复制文件,123 456 789,分别输出到指定的
123-->test1 456---->test2 789---->test3
#指定主机为192.168.168.22.
#3、创建一个nginx.conf文件,改配置文件可以实现upstream反向代理 复制到nginx1
#4、分别在nginx2和nginx3上配置页面: test1 test2
#5、在主机访问目标主机nginx1,实现负载均衡。
#以上步骤全部用ansible远程完成!
- name: this is if
  hosts: all
  remote_user: root
  tasks:
    - name: create mulu
      file:
        path: "{{item}}"
        state: directory
      with_items: [/opt/test1,/opt/test2,/opt/test3]
      when: ansible_default_ipv4.address == "192.168.168.22"
    - name: copy wenjian
      copy:
        src: "{{ item.src }}"
        dest: "/opt/{{ item.dest }}"
      with_items:
        - { src: '/opt/test/123', dest: 'test1' }
        - { src: '/opt/test/456', dest: 'test2' }
        - { src: '/opt/test/789', dest: 'test3' }
      when: ansible_default_ipv4.address == "192.168.168.22"
    - name: nginx1
      yum: name=nginx state=latest
    - name: nginx.conf
      copy: 'src=/opt/nginx.conf dest=/etc/nginx/nginx.conf'
      when: ansible_default_ipv4.address == "192.168.168.22"
      notify: start nginx
    - name: test2 connection
      ping:
    - name: close selinux
      command: '/sbin/setenforce 0'
      ignore_errors: true
    - name: close firewalld
      service: name=firewalld state=stopped
    - name: install nginx
      yum: name=nginx state=latest
    - name: interview
      shell: echo "this is test1" > /usr/share/nginx/html/index.html
      when: ansible_default_ipv4.address == "192.168.168.23"
      notify: start nginx
    - name: test3 connection
      ping:
    - name: close2 selinux
      command: '/sbin/setenforce 0'
      ignore_errors: true
    - name: close2 firewalld
      service: name=firewalld state=stopped
    - name: install nginx2
      yum: name=nginx state=latest
    - name: interview2
      shell: echo "this is test2" > /usr/share/nginx/html/index.html
      when: ansible_default_ipv4.address == "192.168.168.24"
      notify: start nginx
  handlers:
    - name: start nginx
      service: name=nginx state=started


[root@test1 ansible]# yum -y install nginx
[root@test1 ansible]# cp /etc/nginx/nginx.conf /opt/nginx.conf
[root@test1 ansible]# cd /opt/
[root@test1 opt]# ls
123  345  httpd.conf.j2  nginx.conf  test  test2.yaml  test3.yaml  test4.yaml  test5.yaml  test6.yaml
[root@test1 opt]# vim nginx.conf 


 upstream xy102 {
        server 192.168.168.23;
        server 192.168.168.24;
        }
    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;
        location / {
            root   html;
            index  index.html index.htm;
        proxy_pass http://xy102;
        }

[root@test1 opt]# curl 192.168.168.22
this is test1
[root@test1 opt]# curl 192.168.168.22
this is test1
[root@test1 opt]# curl 192.168.168.22
this is test2
[root@test1 opt]# curl 192.168.168.22
this is test2
[root@test1 opt]# curl 192.168.168.22
this is test1
[root@test1 opt]# curl 192.168.168.22
this is test2
[root@test1 opt]# curl 192.168.168.22
this is test1
[root@test1 opt]# curl 192.168.168.22
this is test1
相关推荐
有毒的教程3 天前
Ansible批量操作自动化完整教程:批量部署服务、配置同步、软件更新实战
运维·自动化·ansible
Hy行者勇哥8 天前
用Cursor智能编写Ansible/Terraform脚本,打通CI/CD链路
ci/cd·ansible·terraform
芷栀夏10 天前
飞牛NAS怎么部署Immich?家庭照片自动备份与远程访问教程
服务器·nginx·ansible
悠然南风21 天前
Ansible常见模块总结及LDAP Role 编写与调试
ansible
祺风挽楠1 个月前
ansible编辑
网络·ansible
芳心粽伙饭1 个月前
Ansible课后作业
ansible
烁3471 个月前
Ansible初识
ansible
烁3471 个月前
Ansible安装部署调试
ansible
烁3471 个月前
Ansible命令
ansible
小义_1 个月前
【Ansible】(三)基础配置与连接设置
云原生·ansible