Linux运维day51_二阶段_ansible-playbook的Ansible varialbes

1.什么是变量?

以一个固定的字符串,表示一个不固定的值    version:  1.12

2.定义变量?

1.在playbook中定义变量?

vars  关键字

Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第1张图片

vars_file 属于一种共享的方式


Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第2张图片
Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第3张图片


2.在inventory主机清单中定义变量?

1.清单文件中直接定义  hosts文件定义--

Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第4张图片


2.创建hosts_vars group_vars 目录

[root@manager project1]# mkdir hosts_vars #单个主机

[root@manager project1]# mkdir group_vars #主机组

#1.单个主机定义和使用方式 (host_vars能分别对不同的主机定义变量)

[root@manager project1]# cat host_vars/172.16.1.7

host_vars_name: 172.16.1.7

[root@manager project1]# cat host_vars/172.16.1.8

host_vars_name: 172.16.1.8

[root@manager project1]# cat f4.yml

- hosts: webservers

  tasks:

    - name: Create New File

      file:

        path: /opt/{{ host_vars_name }}

        state: touch


Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第5张图片

Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第6张图片


Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第7张图片


3.通过外置传参定义变量? -e

[root@manager project1]# ansible-playbook -i hosts f6.yml -e "web_vars=123"

3.变量冲突,优先级?

优先级测试:

外置传入参数优先级最高 ---> playbook ( vars_files(共享)--->vars(私有) ) 

---> host_vars  --> group_vars/group_name ---> group_vars/all

4.变量注册?


Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第8张图片


5.facts变量?


Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第9张图片
Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第10张图片
Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第11张图片


Ansible 流程控制


1.判断语句

1.centos和ubuntu系统都需要安装httpd, 判断系统.

2.安装软件仓库,只有web组的安装webtatic其他的主机全部跳过.

3.TASK任务, TASK1任务执行成功,才会执行TASK2 

#根据不同的系统,安装不同的服务

- hosts: webservers

  tasks:

    - name: CentOS Installed Httpd Server

      yum:

        name: httpd

        state: present

      when: ( ansible_distribution == "CentOS" )

    - name: Ubuntu Installed Httpd Server

      yum:

        name: httpd2

        state: present

      when: ( ansible_distribution == "Ubuntu" )


[root@manager project1]# cat f16.yml

- hosts: all

  tasks:

  - name: Add Nginx Yum Repository

    yum_repository:

      name: nginx

      description: Nginx Repository

      baseurl: http://nginx.org/packages/centos/7/$basearch/

    when: ( ansible_hostname is match ("web*"))


Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第12张图片
Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第13张图片

[root@manager project1]# cat f17.yml

- hosts: webservers

  tasks:

    - name: Check Httpd Server

      command: systemctl is-active httpd

      register: Check_Httpd

      ignore_errors: yes

#判断Check_Httpd.rc是否等于0,如果为0则执行任务,否则不执行

    - name: Restart Httpd Server

      systemd:

        name: httpd

        state: restarted

      when: ( Check_Httpd.rc == 0 )


Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第14张图片
Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第15张图片


2.循环语句


#一次启动多个服务

[root@manager project1]# cat f18.yml

- hosts: webservers

  tasks:

    - name: Systemd Nginx Status

      systemd:

        name: "{{ item }}"    #调用的变量也不变,也是固定

        state: started

#固定的语法格式

      with_items:

        - nginx

        - php-fpm


Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第16张图片


Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第17张图片


#一次拷贝多个文件

[root@manager project1]# cat f19.yml

- hosts: webservers

  tasks:

    - name: Configure nginx.conf

      copy:

        src: '{{ item.src }}'

        dest: '{{ item.dest }}'

        mode: '{{ item.mode }}'

      with_items:

        - { src: ./file/nginx.conf.j2, dest: /etc/nginx/nginx.conf, mode: '0644' }

        - { src: ./file/kold.oldxu.com.conf.j2, dest: /etc/nginx/conf.d/kold.oldxu.com.conf, mode: '0600' }


Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第18张图片
Linux运维day51_二阶段_ansible-playbook的Ansible varialbes_第19张图片


#创建多个用户,一次创建多个? 3个用户 TASK

[root@manager project1]# cat f20.yml

- hosts: webservers

  tasks:

    - name: Create User

      user:

        name: "{{ item }}"

      with_items:

        - test1

        - test2

        - test3

        - test4

#1.创建tt1 --> bin tt2 -->root tt3 --->adm 附加组

[root@manager project1]# cat  f20.yml

- hosts: webservers

  tasks:

    - name: Create User

      user:

        name: "{{ item.name }}"

        groups: "{{ item.groups }}"

      with_items:

        - { name: tt1, groups: bin }

        - { name: tt2, groups: root }

        - { name: tt3, groups: adm }


1.标准循环                  --->居多

item

with_items:

  - test

2.字典循环:                  --->居多

    itme.name

    with_items:

        - { name: test }

3.变量循环

- hosts: webservers

  tasks:

    - name: ensure a list of packages installed

      yum: name={{ packages }} state=present

      vars:

        packages:

          - httpd

          - httpd-tools

3.handlers

[root@manager project1]# cat f22.yml

- hosts: webservers

  tasks:

    - name: Installed Nginx and PHP Packages

      yum:

        name: nginx

        state: present

    - name: Configure nginx.conf

      template:

        src: ./file/nginx.conf.j2

        dest: /etc/nginx/nginx.conf

      #监控-->changed状态-->通知-->handlers--->name-->Restart Nginx Server

      notify: Restart Nginx Server

      #notify:

      #  - Restart Nginx Server

      #  - Restart php Server

    - name: Systemd Nginx Server

      systemd:

        name: nginx

        state: started

        enabled: yes

#当nginx或php配置文件发生变更才会触发此操作

  handlers:

    - name: Restart Nginx Server

      systemd:

        name: nginx

        state: restarted

#3.handlers注意事项

1.无论多少个task通知了相同的handlers,handlers仅会在所有tasks结束后运行一次。

2.只有task发生改变了才会通知handlers,没有改变则不会触发handlers.

3.不能使用handlers替代tasks、因为handlers是一个特殊的tasks。

你可能感兴趣的:(Linux运维day51_二阶段_ansible-playbook的Ansible varialbes)