美文网首页
Ansible 上手开始

Ansible 上手开始

作者: 与狼共舞666 | 来源:发表于2021-12-27 17:17 被阅读0次

初识play中的connnection: local

先看下建立主机互信和分发主机公钥信息的栗子:

- name: configure ssh connection
  hosts: new
  gather_facts: false
  connection: local
  tasks:
    - name: configure ssh connection
      shell: 
        ssh-keyscan {{inventory_hostname}} >> ~/.ssh/known_hosts
        sshpass -p'xxxxxxx' ssh-copy-id root@{{inventory_hostname}}

解释一下,上述play中定义了 play级别connection连接配置信息,意思就是在Ansible本地执行 N次(由筛选出的inventory主机数量决定),如果有多个tasks,那么每个task都将执行N次。

利用script模块在受控端执行Ansible上的shell脚本

- name: use script module
  hosts: new
  gather_facts: false
  tasks: 
    - name: use script module
      script: /tmp/hello.sh HELLOWORLD

执行结果:

[root@pg01 ~]# ansible-playbook -v hello_script.yaml 
Using /etc/ansible/ansible.cfg as config file

PLAY [use some module] *********************************************************************************************************************************************************************************************

TASK [use script module] *******************************************************************************************************************************************************************************************
changed: [10.0.31.92] => {"changed": true, "rc": 0, "stderr": "Shared connection to 10.0.31.92 closed.\r\n", "stderr_lines": ["Shared connection to 10.0.31.92 closed."], "stdout": "hello world: HELLOWORLD\r\n", "stdout_lines": ["hello world: HELLOWORLD"]}
changed: [10.0.31.91] => {"changed": true, "rc": 0, "stderr": "Shared connection to 10.0.31.91 closed.\r\n", "stderr_lines": ["Shared connection to 10.0.31.91 closed."], "stdout": "hello world: HELLOWORLD\r\n", "stdout_lines": ["hello world: HELLOWORLD"]}

PLAY RECAP *********************************************************************************************************************************************************************************************************
10.0.31.91                 : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
10.0.31.92                 : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

相关文章

  • Ansible 上手开始

    初识play中的connnection: local 先看下建立主机互信和分发主机公钥信息的栗子: 解释一下,上述...

  • 快速上手 Ansible

    Ansible 系列:(一):快速上手 Ansible(二):Ansible 命令(三):Ansible 主机清单...

  • Ansible 命令

    Ansible 系列:(一):快速上手 Ansible(二):Ansible 命令(三):Ansible 主机清单...

  • Ansible 主机清单配置文件

    Ansible 系列:(一):快速上手 Ansible(二):Ansible 命令(三):Ansible 主机清单...

  • Ansible Playbook 剧本语法

    Ansible 系列:(一):快速上手 Ansible(二):Ansible 命令(三):Ansible 主机清单...

  • Ansible 快速上手

    三部曲 1. 安装 先安装 python3, 然后通过 pip 来安装 或者用系统自带的包管理工具来安装 cent...

  • 『Ansible 上手指南』

    Ansible 上手指南 前言 最近在重构一款命令行工具,使用 golang 重新开发,但需要继续维持原有的命令,...

  • Ansible awx 快速上手

    准备 playbook 在目录 /var/lib/awx/projects 下创建目录 test ,在 test ...

  • 『Ansible 上手指南:2』

    读一本书最好的时机是什么时候?是你刚买的时候,趁着新鲜劲,先了解这本书,继而马上阅读完这本书。如果错过了最好的时机...

  • ansible ad-hoc 快速上手

    /etc/ansible/hosts 配置[webserver] 如果没有配置ssh 秘钥 添加 -k / ...

网友评论

      本文标题:Ansible 上手开始

      本文链接:https://www.haomeiwen.com/subject/qvbwsltx.html