ansible 简易教程

安装命令

pip3 install ansible

# 用密码登陆远程主机需要额外装sshpass
sudo apt install sshpass

配置

ansible --version

config_file = None

这时要在 /etc/ansible/ 目录下手动创建 ansible.cfg 和 hosts 文件。

在ansible.cfg中配置,跳过初次ssh时的客户端可信校验。

[defaults]
host_key_checking = False

添加hosts

hosts文件示例如下:

[group_name]
123.123.123.123:22 ansible_ssh_user=root ansible_ssh_pass='password'
123.123.123.124:22 ansible_ssh_user=root ansible_ssh_pass='password'

分别是IP,端口,账号和密码,一个[group]可以有多台服务器。

测试

ansible groupname -m ping

-m表示调用的模块名称。

实例

fetch: 从远程copy文件到本地,注意目前支持文件,不支持文件夹递归copy

拷贝单个文件:

ansible hosts -m fetch -a "src=/remote/dir/file dest=/local/dir" #-a表示模块的参数

copy:从本地到远程

shell:运行shell

find: 查找文件

unarchive: 解压

archive:压缩

ansible-playbook

ansible playbook 在 ansible命令行的基础上提供了跟家强大的流程编写功能。

写一个yaml文件 p.yml:

---  #固定格式, 这个脚本演示了把一个远程文件夹里的多个文件拷贝到本地的/tmp/目录

- name: Fetch_dir_from_remote
  hosts: IP or Groupname

  tasks:
    - name: find
      find:
        paths: /var/www/html
        patterns: "*"
        recurse: yes
      register: file_2_fetch  #注册变量

    - name: fetch_find
      fetch:
        src: "{{ item.path }}"
        dest: /tmp/
        flat: yes
      with_items: "{{ file_2_fetch.files }}"  #遍历find的结果

执行yml

ansible-playbook p.yml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱知菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值