ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

ansible入门快速上手使用教程

2022-01-05 14:12:59  阅读:167  来源: 互联网

标签:教程 入门 etc ansible hosts ssh file command


1 安装ansible

centos安装:

# yum -y install epel-release    //更新本地安装库
# yum list all *ansible*   //查看相应的版本
# yum info ansible         //查看ansible的信息
# yum install ansible      //开始安装

2 ansible的配置文件

  • /etc/ansible/ansible.cfg 主配置文件
  • /etc/ansible/hosts Inventory
  • /usr/bin/ansible-doc 帮助文件
  • /usr/bin/ansible-playbook 指定运行任务文件

3 定义一个Linux集群组

(1)修改/etc/ansible/hosts

# cd /etc/ansible/
# cp hosts{,.bak}
# > hosts
# cat hosts
[webserver]
127.0.0.1
192.168.10.149
[dbserver]
192.168.10.113

(2)指定相应的配置文件

大部分时候,对/etc/ansible/hosts 文件的修改,需要root权限,而往往工作中我们都是使用的普通用户进行登录,所以一般在自己的文件夹中单独创建一个hosts文件,然后在执行命令的时候指定解析自己的hosts文件即可,文件修改语法不变,执行命令如下:

  • -i {配置文件路径}

    # ansible webserver -i ~/.ansible/hosts -m command -a 'ls ~'
    

4 密钥连接

在进行批量管理的时候,通常需要密码认证,这个时候有两种方式:

  • 每次执行命令都输入一次密码(比较麻烦)

    • -k,是小写的,然后输入密码即可
    # ansible webserver -i ~/.ansible/hosts -m command -a 'ls ~' -k
    

  • 使用ssh方式将密钥传输到指定的主机

    //首先生成ssh密钥
    # ssh-genkey -t rsa    //连续按enter即可
    //将同ssh密钥拷贝到远程主机
    # ssh-copy-id -i .ssh/id_rsa.pub zhangsan@192.168.1.10    //-i表示指定的ssh密钥
    # ssh-copy-id zhangsan@192.168.1.10    //或者直接在ssh密钥的文件夹
    

5 基础教程用法

  • ping

    enable this config, record_host_keys=Flase. And add the host’s ip address in the file(/etc/ansible/hosts).

    $ ssh-keygen$ ssh-copy-id [ip_address]
    $ ansible -m ping
    
  • ansible-playbook: execute a script.

    $ ansible-playbook hello.yml
    $ cat hello.yml
    

  • ansible-vault: encrypt or decrypt a file, to ensure the secrity of the file.

    $ ansible-vault encrypt hello.yml
    $ ansible-vault decrypt hello.yml
    
  • ansible-console: A interact command line of ansible.

Ansible Module:

  • use module:

    • command: execute only system command
    • shell: execute the command via shell process
    • raw: execute the low level command
    $ ansible [host group] -m [module name] -a "[command line]"
    $ ansible webserver -m command/shell -a "ls /etc"
    

    Change default module:

    A tutorials of ansible module

    change config file(/etc/ansible/ansible.cfg), enable the line and change it.

    module_name = command  // change it to shellmodule_name = shell
    
  • script: Run a script on the remote hosts

    $ ansible [host group] -m script -a "test.sh"
    
  • copy: Copy files from current host to remote hosts.

    $ ansible [host group] -m copy -a "src=/etc/systemfig dest=data/os.txt" owner=[user name] mode=600$ ansible webserver -m copy -a "src=/home/centos/pig/1.txt dest=/home/root123/2.txt owner=root123 mode=600"
    

  • fetch: Fetch a file from remote hosts. This must be a file, not a directory. Like copy.

    $ ansible all -m fetch -a "src=/etc/os-release dest=/home/centos/pig/tem"
    

  • file: change file’s attributes, create a empty file.

    $ ansible webserver -m file -a "path=/home/root123/new.txt state=touch"
    

  • unarchive: unzip a file.

    $ ansible webserver -m unarchive -a "src=./etc.tar.gz dest=/home/root123/ owner=root123 mode=600"   // push the zip file to the remote hosts and unzip it.
    

6 ansible转义

ansible 中想执行 lvm vgs | grep ceph | awk '{print $1}' | xargs -I {} lvm vgremove -f {} 命令

排查结果发现 lvm vgs | grep ceph | awk '{print $1}' 中 awk '{print $1}' 失效

最终发现是 $ 符出了问题,添加转义字符 \ 后,ok

ansible all -i  inventories/production/infrastructure -m shell -a "lvm vgs | grep ceph | awk '{print \$1}'| xargs -I {} lvm vgremove -f {}" --become

Reference

写在最后

欢迎大家关注鄙人的公众号【麦田里的守望者zhg】,让我们一起成长,谢谢。
微信公众号

标签:教程,入门,etc,ansible,hosts,ssh,file,command
来源: https://blog.csdn.net/qq_33521184/article/details/122321996

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有