zoukankan      html  css  js  c++  java
  • Ansible_主机变量和主机组变量的演示

    一、主机变量和主机组变量的演示

    1、主机变量和主机组变量的基本语法和规则

    请访问https://www.cnblogs.com/itwangqiang/p/13592362.html

    2、首先在/etc/ansible/下创建清单文件

    [root@localhost ~]# cat /etc/ansible/inventory 
    [test]
    client.example.com
    

    3、实例一:针对特定主机定义变量

    1️⃣:首先在/etc/ansible目录下创建host_vars目录(注意:目录名必须是host_vars;且与清单文件所在目录相同)

    [root@localhost ansible]# mkdir host_vars
    [root@localhost ansible]# ls
    ansible.cfg  hosts  host_vars  inventory  playbook.yaml  roles

    2️⃣:在host_vars创建与主机同名的文件(如果主机是IP地址,则文件名必须是IP地址)

    [root@localhost ansible]# cd host_vars/
    [root@localhost host_vars]# vim client.example.com
    [root@localhost host_vars]# cat client.example.com 
    user: lisi
    [root@localhost host_vars]# cat ../inventory 
    [test]
    client.example.com
       //文件名必须与主机的名字一样

    3️⃣:编写playbook文件

    [root@localhost ansible]# cat playbook.yaml 
    ---
    - hosts: client.example.com
      tasks:
        - name: create user
          user:
            name: "{{ user }}"
            create_home: no
            state: present

    4️⃣:测试是否可执行

    [root@localhost ansible]# tree /etc/ansible/
    /etc/ansible/
    ├── ansible.cfg
    ├── hosts
    ├── host_vars
    │   └── client.example.com
    ├── inventory
    ├── playbook.yaml
    └── roles
    
    [root@localhost ansible]# ansible-playbook -C playbook.yaml 
    
    PLAY [client.example.com] *************************************************************************************************************************************************
    
    TASK [Gathering Facts] ****************************************************************************************************************************************************
    ok: [client.example.com]
    
    TASK [create user] ********************************************************************************************************************************************************
    changed: [client.example.com]
    
    PLAY RECAP ****************************************************************************************************************************************************************
    client.example.com         : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
       //测试成功
    

    4、实例二:针对主机组定义变量

    (接着上面的操作) 

    1️⃣:创建主机组清单文件

    [root@localhost ansible]# cat inventory 
    [example]
    client.example.com

    2️⃣:在/etc/ansible文件在创建group_vars目录(注意:目录名必须是group_vars;且与清单文件所在目录相同)

    [root@localhost ansible]# mkdir group_vars
    [root@localhost ansible]# tree /etc/ansible/
    /etc/ansible/
    ├── ansible.cfg
    ├── group_vars
    ├── hosts
    ├── inventory
    ├── playbook.yaml
    └── roles

    3️⃣:在group_vars目录下创建与主机组名称相同的文件

    [root@localhost ansible]# cd group_vars/
    [root@localhost group_vars]# vim example
    [root@localhost group_vars]# cat example 
    user: lisi
    

    4️⃣:测试是否可执行

    [root@localhost ansible]# tree /etc/ansible/
    /etc/ansible/
    ├── ansible.cfg
    ├── group_vars
    │   └── example
    ├── hosts
    ├── inventory
    ├── playbook.yaml
    └── roles
    
    [root@localhost ansible]# ansible-playbook -C playbook.yaml 
    
    PLAY [client.example.com] *************************************************************************************************************************************************
    
    TASK [Gathering Facts] ****************************************************************************************************************************************************
    ok: [client.example.com]
    
    TASK [create user] ********************************************************************************************************************************************************
    changed: [client.example.com]
    
    PLAY RECAP ****************************************************************************************************************************************************************
    client.example.com         : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
      //测试成功
    

      

  • 相关阅读:
    js与jquery实时监听输入框值的oninput与onpropertychange方法
    jQuery实现的浮动层div浏览器居中显示效果
    jquery代码规范让代码越来越好看
    asp.net截屏功能实现截取web页面
    拦截asp.net输出流并进行处理的方法
    asp.net调用系统设置字体文本框的方法
    asp.net网站防恶意刷新的Cookies与Session解决方法
    Attribute在.net编程中的应用(一)
    java学习——构造类之3!+5!=126
    java学习——构造类
  • 原文地址:https://www.cnblogs.com/itwangqiang/p/13595447.html
Copyright © 2011-2022 走看看