• ansible第一天


    ansible

    第一天

    以上主机使用rhel-8.2-x86_64-dvd.iso镜像,配置ip、yum源,关闭防火墙和selinux规则

    安装中文包,重启生效

     

    [root@control ~]# yum -y install langpacks-zh_CN.noarch && reboot

    配置名称解析
    1. [root@control ~]# echo -e "192.168.88.253\tcontrol">>/etc/hosts
    2. [root@control ~]# for i in {1..5}
    3. do
    4. echo -e "192.168.88.1$i\tnode$i">>/etc/hosts
    5. done
    配置ssh到所有节点免密登陆
    1. [root@control ~]# ssh-keygen
    2. root@control ~]# echo node{1..5}
    3. node1 node2 node3 node4 node5
    4. [root@control ~]# for i in node{1..5}
    5. > do
    6. > ssh-copy-id root@$i
    7. > done
    装包

    软件包链接:链接:百度网盘 请输入提取码 提取码:bb2o --来自百度网盘超级会员V5的分享

    1. [root@control ~]# ls
    2. anaconda-ks.cfg ansible_soft.tar.gz
    3. [root@control ~]# tar zxvf ansible_soft.tar.gz
    4. [root@control ~]# ls
    5. anaconda-ks.cfg ansible_soft ansible_soft.tar.gz
    6. [root@control ~]# ls ansible_soft
    7. ansible-2.8.5-2.el8.noarch.rpm           python3-paramiko-2.4.3-1.el8.noarch.rpm
    8. libsodium-1.0.18-2.el8.x86_64.rpm       python3-pynacl-1.3.0-5.el8.x86_64.rpm
    9. python3-bcrypt-3.1.6-2.el8.1.x86_64.rpm sshpass-1.06-9.el8.x86_64.rpm
    10. [root@control ~]# yum -y install /root/ansible_soft/*.rpm
    创建ansible工作目录
    1. 创建ansible工作目录,目录名自己定义,不是固定的
    2. [root@control ~]# mkdir ansible
    3. [root@control ~]# cd ansible
    4. 创建配置文件。默认的配置文件是/etc/ansible/ansible.cfg,一般不用,而是在工作目录下创建自己的配置文件
    5. [root@control ansible]# vim ansible.cfg 文件名必须是ansible.cfg
    6. [root@control ansible]# cat ansible.cfg
    7. [defaults]
    8. inventory = hosts 管理的主机,配置在当前目录的hosts文件中,hosts是自己定义的。=号俩边空格可有可无
    9. [root@control ansible]# touch hosts
    10. [root@control ansible]# vim hosts
    11. [root@control ansible]# cat hosts
    12. [test]
    13. node1
    14. [proxy]
    15. node2
    16. [webservers]
    17. node[3:4]
    18. [database]
    19. node5
    20. [cluster:children] cluster是组名,自定义的;children是固定写法,表示下面的组名是cluster的子组
    21. webservers
    22. database
    23. [root@control ansible]# ansible all --list
    24. hosts (5):
    25.   node1
    26.   node2
    27.   node3
    28.   node4
    29.   node5
    30. [root@control ansible]# ansible webservers --list
    31. hosts (2):
    32.   node3
    33.   node4
    34. [root@control ansible]# ansible proxy --list
    35. hosts (1):
    36.   node2
    简单演示
    1. 用ansible创建/tmp/abcd目录
    2. [root@control ansible]# ansible all -a "mkdir /tmp/abcd"
    3. [WARNING]: Consider using the file module with state=directory rather than running 'mkdir'.
    4. If you need to use command because file is insufficient you can add 'warn: false' to this
    5. command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
    6. node2 | CHANGED | rc=0 >>
    7. node1 | CHANGED | rc=0 >>
    8. node5 | CHANGED | rc=0 >>
    9. node3 | CHANGED | rc=0 >>
    10. node4 | CHANGED | rc=0 >>
    ansible管理
    ansible进行远程管理的俩个办法

    adhoc临时命令。就是在命令行上执行管理命令

    playbook剧本。把管理任务用特定格式写到文件中

    无论哪种方式,都是通过模块加参数进行管理

    adhoc临时命令
    1. 语法:
    2. ansible 主机或者组列表 -m 模块 -a 参数
    3. 测试ansible与被控主机的连通性
    4. [root@control ansible]# ansible all -m ping
    5. node1 | SUCCESS => {
    6.    "ansible_facts": {
    7.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    8.   },
    9.    "changed": false,
    10.    "ping": "pong"
    11. }
    12. node3 | SUCCESS => {
    13.    "ansible_facts": {
    14.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    15.   },
    16.    "changed": false,
    17.    "ping": "pong"
    18. }
    19. node5 | SUCCESS => {
    20.    "ansible_facts": {
    21.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    22.   },
    23.    "changed": false,
    24.    "ping": "pong"
    25. }
    26. node2 | SUCCESS => {
    27.    "ansible_facts": {
    28.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    29.   },
    30.    "changed": false,
    31.    "ping": "pong"
    32. }
    33. node4 | SUCCESS => {
    34.    "ansible_facts": {
    35.        "discovered_interpreter_python": "/usr/libexec/platform-python"
    36.   },
    37.    "changed": false,
    38.    "ping": "pong"
    39. }
    command模块
    1. ansible默认模块,用于在远程主机上执行任意命令
    2. command不支持shell特性。如管道、重定向
    3. 在所有被管主机上创建目录aaa
    4. [root@control ansible]# ansible all -a "mkdir aaa"
    5. [WARNING]: Consider using the file module with state=directory rather than running 'mkdir'.
    6. If you need to use command because file is insufficient you can add 'warn: false' to this
    7. command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
    8. node5 | CHANGED | rc=0 >>
    9. node3 | CHANGED | rc=0 >>
    10. node1 | CHANGED | rc=0 >>
    11. node2 | CHANGED | rc=0 >>
    12. node4 | CHANGED | rc=0 >>
    13. 查看node节点的ip地址,不支持管道、重定向命令
    14. [root@control ansible]# ansible all -a "ip a|head -2"
    15. node3 | FAILED | rc=1 >>
    16. Object "a|head" is unknown, try "ip help".non-zero return code
    17. node2 | FAILED | rc=1 >>
    18. Object "a|head" is unknown, try "ip help".non-zero return code
    19. node1 | FAILED | rc=1 >>
    20. Object "a|head" is unknown, try "ip help".non-zero return code
    21. node4 | FAILED | rc=1 >>
    22. Object "a|head" is unknown, try "ip help".non-zero return code
    23. node5 | FAILED | rc=1 >>
    24. Object "a|head" is unknown, try "ip help".non-zero return code
    shell模块
    1. command模块类似,但是支持shell特性,如管道、重定向
    2. [root@control ansible]# ansible node1 -m shell -a "ip a| head"
    3. node1 | CHANGED | rc=0 >>
    4. 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    5.   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    6.   inet 127.0.0.1/8 scope host lo
    7.       valid_lft forever preferred_lft forever
    8.   inet6 ::1/128 scope host
    9.       valid_lft forever preferred_lft forever
    10. 2: eth0: mtu 1500 qdisc fq_codel state UP group default qlen 1000
    11.   link/ether 00:0c:29:44:4e:3b brd ff:ff:ff:ff:ff:ff
    12.   inet 192.168.88.11/24 brd 192.168.88.255 scope global noprefixroute eth0
    13.       valid_lft forever preferred_lft forever
    script模块
    1. 用于在远程主机上执行脚本
    2. 在控制端创建脚本即可
    3. [root@control ansible]# vim http.sh
    4. #!/bin/bash
    5. yum -y install httpd
    6. systemctl start httpd
    7. test组的主机上执行脚本
    8. [root@control ansible]# ansible test -m script -a "http.sh"
    9. 查看test组的主机httpd服务是否开启
    10. [root@control ansible]# ansible test -a "systemctl status httpd"
    11. node1 | CHANGED | rc=0 >>
    12. ● httpd.service - The Apache HTTP Server
    13.   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
    14.   Active: active (running) since Tue 2023-11-07 19:04:56 EST; 44s ago
    15.     Docs: man:httpd.service(8)
    16. Main PID: 3226 (httpd)
    17.   Status: "Running, listening on: port 80"
    18.   Tasks: 213 (limit: 5298)
    19.   Memory: 27.8M
    20.   CGroup: /system.slice/httpd.service
    21.           ├─3226 /usr/sbin/httpd -DFOREGROUND
    22.           ├─3227 /usr/sbin/httpd -DFOREGROUND
    23.           ├─3230 /usr/sbin/httpd -DFOREGROUND
    24.           ├─3231 /usr/sbin/httpd -DFOREGROUND
    25.           └─3233 /usr/sbin/httpd -DFOREGROUND
    26. 11月 07 19:04:56 node1 systemd[1]: Starting The Apache HTTP Server...
    27. 11月 07 19:04:56 node1 httpd[3226]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::dde1:3eea:5077:d08f. Set the 'ServerName' directive globally to suppress this message
    28. 11月 07 19:04:56 node1 systemd[1]: Started The Apache HTTP Server.
    29. 11月 07 19:04:56 node1 httpd[3226]: Server configured, listening on: port 80
  • 相关阅读:
    Vue07/Vue插槽介绍、默认插槽、插槽后备内容(默认值)
    12306 火车票价格解析 (PHP 解析)
    mongoDB副本集
    VLAN 实验
    zookeeper第一章:集群搭建
    java计算机毕业设计小区车辆管理系统源码+系统+数据库+lw文档(1)
    18年,51cto老师录视频- Vue.js前端开发基础与项目实战的接口,不能用了
    基于微服务、Java、Springcloud、Vue、MySQL开发的智慧工地管理系统源码
    MapReduce计算流程
    flask---》自定义过滤器 模板继承 include 宏
  • 原文地址:https://blog.csdn.net/weixin_65562581/article/details/134279064