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
相关推荐
风清再凯8 小时前
自动化工具ansible,以及playbook剧本
运维·自动化·ansible
IT乌鸦坐飞机8 小时前
ansible部署数据库服务随机启动并创建用户和设置用户有完全权限
数据库·ansible·centos7
遇见火星13 天前
如何使用Ansible一键部署MinIO集群?
ansible
粥周粥13 天前
ANSIBLE
ansible
码农101号13 天前
Linux中ansible模块补充和playbook讲解
linux·运维·ansible
码农101号14 天前
Linux的Ansible软件基础使用讲解和ssh远程连接
ansible
烟雨书信15 天前
ANSIBLE运维自动化管理端部署
运维·自动化·ansible
碎碎-li15 天前
ANSIBLE(运维自动化)
运维·自动化·ansible
@donshu@18 天前
Linux运维-ansible-python开发-获取inventroy信息
linux·运维·ansible
Kendra91921 天前
Ansible
ansible