• 在 Ubuntu 22.04安装配置 Ansible


    一、按官网指引安装

    我使用的ubuntu22.04版本,使用apt安装。官网指引如下:

    1. $ sudo apt-get install software-properties-common
    2. $ sudo apt-add-repository ppa:ansible/ansible
    3. $ sudo apt-get update
    4. $ sudo apt-get install ansible

    由于内部网络需要代理上网,需要在apt配置代理。

    1. sudo export http_proxy=http://192.168.1.1:8080
    2. sudo export https_proxy=http://192.168.1.1:8080

     安装完,reboot重启,并查看版本ansible --version

     

    二、设置 SSH 密钥并在被管理节点之间共享

     ssh 无密码认证。在控制节点为用户生成 SSH 密钥,并在被管理主机之间共享。执行 ssh-keygen 命令,执行 ssh-copy-id 命令,在受控主机之间共享 ssh 密钥,示例如下

    1. # 生成本机的密码
    2. $ ssh-keygen
    3. # 把本地主机的公钥复制到远程主机。
    4. $ ssh-copy-id root@108.61.163.242
    5. # 可能会如下情况:
    6. $ ssh root@108.61.163.242
    7. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    8. @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
    9. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    10. IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
    11. Someone could be eavesdropping on you right now (man-in-the-middle attack)!
    12. It is also possible that a host key has just been changed.
    13. The fingerprint for the ECDSA key sent by the remote host is
    14. SHA256:HDjXJvu0VYXWF+SKMZjSGn4FQmg/+w6eV9ljJvIXpx0.
    15. Please contact your system administrator.
    16. Add correct host key in /Users/wangdong/.ssh/known_hosts to get rid of this message.
    17. Offending ECDSA key in /Users/wangdong/.ssh/known_hosts:46
    18. ECDSA host key for 108.61.163.242 has changed and you have requested strict checking.
    19. Host key verification failed.
    20. # 一般这个问题,是你重置过你的服务器后。你再次想访问会出现这个问题。
    21. 解决办法如下:
    22. ssh-keygen -R 108.61.163.242

    三、ansible命令

    1、ansible

    语法:ansible  host  options

    举例:检查所有主机是否存活,执行命令如下

    1. root@:/etc/ansible# ansible server -m ping
    2. 192.168.1.1 | SUCCESS => {
    3. "ansible_facts": {
    4. "discovered_interpreter_python": "/usr/bin/python3"
    5. },
    6. "changed": false,
    7. "ping": "pong"
    8. }
    2、Ansible-doc 

    Ansible-doc用来查询ansible模块文档的说明,类似于man命令

    3、Ansible-playbook

    语法格式:ansible-play  playbook.yml

    4、Ansible-console

    Ansible-console是Ansible为用户提供的一款交互式工具,类似于Windows的cmd或者是Linux中shell

    1. [root@centos01 ~]# ansible-console
    2. Welcome to the ansible console.
    3. Type help or ? to list commands.
    4. root@all (2)[f:5]$ cd web
    5. root@web (2)[f:5]$ list
    6. 192.168.100.20
    7. 192.168.100.30

     四、ansible模块

    1、command模块

    command模块在远程主机执行基础命令,不支持管道、重定向等shell的特性

    在所有主机上运行“ls ./”命令,运行前切换到/home目录下。

    操作如下:

    [root@centos01 ~] ansible web -m command -a "chdir=/ ls ./"
     2、shell模块

    shell模块在远程主机执行命令,相当于调用远程主机的Shell进程

    1. [root@centos01 ~] ansible web -m shell -a "echo hello world "
    2. 192.168.100.20 | SUCCESS | rc=0 >>
    3. hello world
     3、copy模块

    copy模块用于复制指定主机文件到远程主机的指定位置

    1. [root@centos01 ~] ansible web -m copy -a "src=/etc/hosts
    2. dest=/root/a1.hosts mode=777 owner=root group=root"
     4、hostname模块

    hostname模块用于管理远程主机上的主机名

    1. [root@centos01 ~] ansible 192.168.100.20 -m hostname -a "name=test"
     5、service模块

    service模块为用来管理远程主机上的服务的模块

    1. [root@centos01 ~] ansible web -m service -a "name=httpd
    2. enabled=yes state=restarted"
     6、user模块

    user模块主要用于管理远程主机上的用户账号

    1. [root@centos01 ~] ansible web -m user -a "name=user01
    2. system=yes uid=502 group=root groups=root shell=/etc/nologin
    3. home=/home/user01 password=pwd@123"

    相关参考中文官网:Ansible中文权威指南- 国内专业的Ansible中文官方学习手册

    https://www.cnblogs.com/-wenli/p/13292855.html

    shell和ansible自动化运维实例_ansible playbook执行shell脚本_一个高效工作的家伙的博客-CSDN博客

  • 相关阅读:
    IronPDF for Java 2023.9.2 Crack
    HTML案例-3.表格练习
    低代码助力企业数字化转型
    数学建模之多项式回归
    使用apriltag_ros检测相机姿态
    微服务day04-基于Feign的远程调用
    vue 性能优化方案
    阿里面试官终于把多年总结的Java八股文PDF版分享出来了,帮我金九银十拿下4个offer
    啊哈算法--堆排序 (python)
    MySQL篇---第二篇
  • 原文地址:https://blog.csdn.net/qiuweifan/article/details/133800812