zoukankan      html  css  js  c++  java
  • ansible 变量定义和引用 sudo 执行命令

    cat /etc/ansible/hosts

    [nodes]
    10.2.1.232 key=232
    10.2.1.43 key=43

    cat debug.yaml

    ---
    - name: test how to use command module
    hosts: nodes
    remote_user: root
    gather_facts: false
    vars:
    - content: '20181104'
    tasks:
    - name: create a file
    shell: "echo {{key}} >/root/{{inventory_hostname}}.txt"

    结果:

    cat  10.2.1.232.txt

    232

    2.gather_facts


    - name: gather_facts of each host
      template: src=/root/nginx.conf dest=/root/nginx.conf

     cat /root/nginx.conf 

    worker_processes {{  ansible_processor_cores }};

     \获取ansible的要调用的相关函数

    ---------------------------------------------------------------------------------

    远程登陆机器执行脚本,结果输出到以IP命名的文件

    [dev@VM_0_11_centos ~]$ cat getResult.yaml
    ---
    - name: test how to use command module
      hosts: qiwei
      become: yes
      become_user: root
      gather_facts: false
      vars:
      - content: '20181104'
      tasks:
      - name: create a file
        shell: "sh /tmp/secure1.sh >/tmp/{{ inventory_hostname }}.log"

     收集远端文件到本地目录

    ---
    - name: test how to use command module
      hosts: qiwei
      become: yes
      become_user: root
      gather_facts: false
      vars:
      - content: '20181104'
      tasks:
      - name: copy remote file to local
        fetch: "src=/tmp/{{ inventory_hostname }}.log dest=/tmp/1113/"

     -------------------------------------------------------------------------------------

    sudo远程执行包括变量的命令

    [dev@VM_0_11_centos tmp]$ cat toip.yaml 
    ---
    -
     name: get ip imformation into ip.txt
     hosts: ab-pool
     gather_facts: false
     become: true
     become_user: root
     tasks:
     - name: echo ip imformation into $ip.txt
       shell: echo {{ inventory_hostname  }} > /tmp/{{ inventory_hostname }}.txt
  • 相关阅读:
    【C++】深度探索C++对象模型读书笔记--关于对象(Object Lessons)
    【操作系统】调度算法
    【vim】vim常用命令
    【linux】linux的数据流重定向
    以太网帧,IP,TCP,UDP首部结构
    IPv4编址及子网划分
    【计算机网络】计算机网络模型
    【计算机网络】NAT:网络地址转换
    【设计模式】C++中的单例模式
    (转)linux查找技巧: find grep xargs
  • 原文地址:https://www.cnblogs.com/hixiaowei/p/9904550.html
Copyright © 2011-2022 走看看