• ansible的安装和简单的块使用


    目录

    一、概述

    二、安装

    1、选择源

    2、安装ansible

    3、模块查看

    三、实验

    1、拓扑​编辑

    2、设置组、ping模块

    3、hostname模块

    4、file模块

    ​编辑

    5、stat模块

    6、copy模块(本地拷贝到远程)

    7、fetch模块与copy模块类似,但作用相反。用于把远程机器的文件拷贝到本地。

    8、user模块

    9、group模块

    10、cron模块

    四、yum_repository模块

    五、yum模块

    六、server模块

    七、script模块

    八、command与shell模块

    九、playbook

    YMAL格式



    一、概述

    Ansible是一种自动化工具,用于配置管理、应用程序部署和协调云部署。它是一个开源工具,使用Python编写,通过SSH协议与远程主机通信。

    1. 相关介绍和同类平台:
       - 相关介绍:Ansible是一种基于代理的自动化工具,它使用简单、轻量级的语法。它的主要目标是使自动化过程简单易用,同时提供强大的功能来管理大规模的基础架构。
       - 同类平台:与Ansible类似的自动化工具包括Puppet、Chef和SaltStack等。这些工具都提供了类似的功能,但在语法、工作原理和部署模型上略有不同。

    2. 为什么要用Ansible、它能做什么以及优点:
       - 简单易用:Ansible使用基于YAML的语法,易于理解和编写,无需编程知识。它还具有良好的文档和活跃的社区支持。
       - 无代理:Ansible使用SSH协议进行通信,无需在远程主机上安装任何代理程序,这使得部署和配置更加简单和安全。
       - 基于剧本:Ansible使用基于剧本的方式来描述并执行自动化任务,使得任务的组织和管理更加灵活和可扩展。
       - 高度可配置:Ansible支持各种平台和操作系统,并提供了丰富的模块和插件,可以管理各种类型的资源,包括服务器、网络设备、云平台等。
       - 扩展性:Ansible具有强大的可扩展性,可以根据特定需求编写自定义模块和插件,或与现有工具和系统集成。

    3. 工作原理:
       - Ansible使用一个控制节点来管理多个远程主机。控制节点上的Ansible配置文件描述了需要执行的任务和相关主机的信息。
       - 当控制节点执行Ansible命令时,它会通过SSH连接到远程主机,并在远程主机上执行相应的任务。
       - Ansible使用基于模块的方式来管理和配置远程主机。它可以通过内置的模块执行各种操作,如文件管理、软件包安装、服务管理等。
       - Ansible还支持使用剧本(Playbooks)来定义和组织多个任务,以及使用变量和条件来实现更复杂的自动化流程。

    总的来说,Ansible是一个功能强大、易于使用和无代理的自动化工具,可以帮助简化配置管理和应用程序部署的过程,提高工作效率和可维护性。

    心组件:

    •    Inventory:Ansible 管理的主机信息,包括 IP 地址、 SSH 端口、账号、密码 等;

        Modules:任务均有模块完成,也可以自定义模块,例如经常用的脚本;

    •    Plugins使用插件增加Ansible 核心功能,自身提供了很多插件, 也可以自

    定义插件。例如 connection 插件, 用于连接目标主机。 callback 插件可以将 果输出到其他地方。vars 插件将定义的比变量注入到Ansible 中运行。

    •    Playbooks:“剧本”,模块化定义一系列任务,供外部统一调用。Ansible 核心功能。

     Ansible 可以在安装了 Python 2 (版本 2.6 或 2.7)或 Python 3 (版本 3.5 及更高版本) 的任何机器上运行(管理机器不支持 Windows)。

    二、安装

    在开始实验前我们先同步集群时间、进行ssh免密

    1. ##每台主机
    2. ssh-keygen
    3. ssh-copy-id 192.168.115.131
    4. ##在192.168.115.131
    5. for i in 136 140 ;do scp /root/.ssh/authorized_keys 192.168.115.$i:/root/.ssh ;done
    6. ##同步集群时间
    7. for i in 131 136 140 ;do ssh 192.168.115.$i yum -y install ntp;done
    8. for i in 131 136 140 ;do ssh 192.168.115.$i systemctl restart ntpd;done
    1、选择源
    1. cd /etc/yum.repos.d
    2. mkdir back
    3. mv * bcak
    4. yum clean all
    5. yum makecache
    6. yum update
    7. wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
    8. curl-o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
    9. yum makecache frist
    10. yum update
    2、安装ansible
    1. yum -y install epel-release.noarch
    2. yum -y install ansible
    3、模块查看
    1. ###模块列表
    2. ansible-doc -l
    3. ###模块用法查看
    4. ansible-doc 模块名

    三、实验

    1、拓扑

    2、设置组、ping模块

    绿色成功、红色失败

    方式1

    1. vim /etc/ansible/hosts
    2. ###插入
    3. [group]
    4. 192.168.115.131
    5. 192.168.115.136
    6. 192.168.115.140
    7. ###利用模块ping
    8. ansible -m ping group

    方式2

    1. #如果主机数量太多就这样表示
    2. [group]
    3. 192.168.115.[136:140]

    方式3

    1. ##指定端口
    2. [group]
    3. 192.168.115.136:22

    方式4

    1. ###别名
    2. [group]
    3. 192.168.115.136:22
    4. hy ansible_ssh_host=192.168.115.140 ansible_ssh_port=22

    方式5

    1. ##没有ssh免密的也可以
    2. [group]
    3. ansible_ssh_host=192.168.115.140 ansible_ssh_port=22 ansible_ssh_user=用户名 ansible_ssh_pass="密码"

    方式6

    1. ###利用别名分组
    2. [group]
    3. 192.168.115.136:22
    4. hy ansible_ssh_host=192.168.115.136 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123.com"
    5. nginx ansible_ssh_host=192.168.115.140 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123.com"
    6. [nginx]
    7. nginx
    8. [hy]
    9. hy

    3、hostname模块

    基本格式为: ansible 操作的机器名或组名 -m 模块名 -a “参数1=值1 参数2=值2” argment

    黄色表示成功

    1. ###修改192.168.115.136的主机名hy
    2. ###修改192.168.115.140的主机名nginx
    3. ansible hy -m hostname -a 'name=hy'
    4. ansible nginx -m hostname -a 'name=nginx'

    4、file模块

    1、创建目录

    1. ##创建一个目录
    2. ansible hy -m file -a "path=/opt/hy.txt state=directory"

    2、创建文件

    1. ansible hy -m file -a "path=/opt/hy.txt/test.tst state=touch"

    3、修改属主属组、权限

    1. ####修改属主为hy,属组hy 权限777
    2. ansible hy -m file -a "path=/opt/hy.txt recurse=yes owner=hy group=hy mode=777"

    4、删除目录(包括目录中的文件)

    1. ###删除/opt/hy.txt
    2. ansible hy -m file -a "path=/opt/hy.txt state=absent"

    5、创建软、硬连接

    1. #软
    2. ansible hy -m file -a "src=/etc/fstab path=/opt/fstab2 state=link"
    3. #硬
    4. ansible hy -m file -a "src=/etc/fstab path=/opt/fstab1 state=hard"

    5、stat模块
    1. ###用来获取文件的信息状态
    2. ansible hy -m file -a "path=/opt/hy.txt state=touch"
    3. ansible hy -m stat -a "path=/opt/hy.txt"

    6、copy模块(本地拷贝到远程)

    在ansible上准备一个文件拷贝到2台agent

    1. echo 192.168.115.131 > 1.txt
    2. ansible group -m copy -a "src=/root/1.txt dest=/opt"

    使用content远程写入内容,并覆盖原内容

    ansible group -m copy -a "content="hello\n" dest=/opt/1.txt"

    使用force参数是否强制覆盖

    1. ##写入
    2. echo 192.168.115.131 > 1.txt
    3. ansible group -m copy -a "content="hello" dest=/opt/1.txt force=no"#如果目标文件存在,不覆盖
    4. ansible group -m copy -a "content="hello" dest=/opt/1.txt force=yes"#如果目标文件存在,覆盖

    使用backup模块备份,把本机文件备份到远端

    1. ###把本机的文件备份到远端
    2. ansible group -m copy -a "src=/root/1.txt dest=/opt/2.txt backup=yes owner=root group=root mode=644"

    拷贝时/与没有/的区别

    ansible hy -m copy -a "src=/etc/yum.repos.d dest=/etc/yum.repos.d"

    ansible nginx -m copy -a "src=/etc/yum.repos.d/ dest=/etc/yum.repos.d"

    7、fetch模块与copy模块类似,但作用相反。用于把远程机器的文件拷贝到本地。

    注意: fetch模块不能从远程拷贝目录到本地

    1. ##两台agent上创建同名、路径文件
    2. echo 192.168.115.136 hy > test.txt
    3. echo 192.168.115.140 nginx > test.txt
    4. ###ansible上操作
    5. group -m fetch -a "src=/etc/yum.repos.d/test.txt dest=/etc/yum.repos.d"

    8、user模块

    user模块用于管理用户账号和用户属性。

    1. ##创建aaa用户,默认为普通用户,创建家目录
    2. ansible hy -m user -a "name=aaa state=present"

    1. ###创建系统用户
    2. ansible hy -m user -a'name=bbb state=present system=yes shell="/sbin/nologin"'

    1. ##创建ccc用户, 使用uid参数指定uid, 使用password参数传密码
    2. echo 123.com |openssl passwd -stdin
    3. ansible hy -m user -a 'name=ccc state=present uid=6666 password="密码"'

    1. ##创建一个普通用户叫ddd,并产生空密码 密钥对
    2. ansible hy -m user -a 'name=ddd state=present generate_ssh_key=yes'

    1. ##删除aaa用户,但家目录默认没有删除
    2. ansible hy -m user -a 'name=aaa state=absent'

    1. ##删除aaa用户、家目录
    2. ansible hy -m user -a 'name=aaa state=absent remove=yes'

    9、group模块

    group模块用于管理用户组和用户组属性。

    1. ###创建组
    2. ansible hy -m group -a 'name=web gid=2000 state=present'

    1. ##删除组(如果有用户的gid为此组,则删除不了)
    2. ansible hy -m group -a 'name=web state=absent'

    10、cron模块

    cron模块用于管理周期性时间任务

    1. ##创建一个cron任务,不指定user的话,默认就是root(因为我这里是用root操作的)。
    2. 如果minute,hour,day,month,week不指定的话,默认都为*
    3. ansible hy -m cron -a 'name="test1" user=root job="touch /etc/6666" minute=26'

    1. 删除cron任务
    2. ansible hy -m cron -a 'name="test1" state=sbsent'

    四、yum_repository模块

    yum_repository模块用于配置yum仓库。

    1. ### yum_repository模块yum_repository模块用于配置yum仓库。
    2. 注意:此模块只帮助配置yum仓库,但如果仓库里没有软件包,安装一样会失败。所以可以手动去挂载光驱到/mnt目录
    3. ansible hy -m yum_repository -a "name=local description=localyum baseurl=file:///mnt/ enabled=yes gpgcheck=no"
    4. ##删除/etc/yum.repos.d/local.repo配置文件
    5. ansible hy -m yum_repository -a "name=local state=absent"

    五、yum模块

    yum模块用于使用yum命令来实现软件包的安装与卸载。 前提:group的机器上的yum配置都已经OK ,如果是使用本地源的就需要挂载

    1. ##安装http
    2. ansible group -m yum -a 'name=httpd state=present'
    1. ######使用网络源
    2. ##在ansible上安安装网络源
    3. wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
    4. ##copy到其他主机

    使用yum安装httpd,httpd-devel软件,state=latest表示安装最新版本

    ansible group1 -m yum -a 'name=httpd,httpd-devel state=latest'

    使用yum卸载httpd,httpd-devel软件

    ansible group1 -m yum -a 'name=httpd,httpd-devel state=absent'

    六、server模块

    1. ### service模块(重点)service模块用于控制服务的启动,关闭,开机自启动等。
    2. 启动vsftpd服务,并设为开机自动启动
    3. master# ansible group -m service -a 'name=vsftpd state=started enabled=on'
    4. 关闭vsftpd服务,并设为开机不自动启动
    5. master# ansible group -m service -a 'name=vsftpd state=stoped enabled=false'

    我们安装一个mariadb服务,并设置为开机自启

    1. ansible group -m yum -a 'name=mariadb,mariadb-server state=present'
    2. ansible group -m service -a 'name=mariadb state=started enabled=yes'

    七、script模块

    script模块用于在远程机器上执行本地脚本。

    1. 在master上准备一个脚本
    2. master# vim /tmp/1.sh
    3. #!/bin/bash
    4. mkdir /tmp/haha
    5. touch /tmp/haha/{1..10}
    6. group1的远程机器里都执行master上的/tmp/1.sh脚本(此脚本不用给执行权限)
    7. master# ansible group1 -m script -a '/tmp/1.sh'

    八、command与shell模块

    两个模块都是用于执行linux命令的,这对于命令熟悉的工程师来说,用起来非常high。

    shell模块与command模块差不多(command模块不能执行一些类似$HOME,>,<,|等符号,但shell可以)

     ansible -m command group1 -a "useradd user2"
    ansible -m command group1 -a "id user2"
    ​
    ansible -m command group1 -a "cat /etc/passwd |wc -l"       --报错
    ansible -m shell group1 -a "cat /etc/passwd |wc -l"     --成功
    ​
    ansible -m command group1 -a "cd $HOME;pwd"   --报错
    ansible -m shell  group1 -a "cd $HOME;pwd"    --成功

    注意: shell模块并不是百分之百任何命令都可以,比如vim或ll别名就不可以。不建议大家去记忆哪些命令不可以,大家只要养成任何在生产环境里的命令都要先在测试环境里测试一下的习惯就好。

    九、playbook

    playbook(剧本): 是ansible用于配置,部署,和管理被控节点的剧本。用于ansible操作的编排。

    使用的格式为yaml格式(saltstack,elk,docker,docker-compose,kubernetes等也都会用到yaml格式)

    YMAL格式

    • 以.yaml或.yml结尾

    • 文件的第一行以 "---"开始,表明YMAL文件的开始(可选的)

    • 以#号开头为注释

    • 列表中的所有成员都开始于相同的缩进级别, 并且使用一个 "- " 作为开头(一个横杠和一个空格)

    • 一个字典是由一个简单的 键: 值 的形式组成(这个冒号后面必须是一个空格)

    • ==注意: 写这种文件不要使用tab键,都使用空格==

    1. ## playbook实例
    2. 先直接来看一个实例
    3. **1步: 创建一个存放playbook的目录(路径自定义)**
    4. ```powershell
    5. master# mkdir /etc/ansible/playbook
    6. ```
    7. **2步: 准备httpd配置文件,并修改成你想要的配置**
    8. ```powershell
    9. master# yum install httpd -y
    10. 按需要修改你想要的配置(为了测试可以随意改动标记一下)
    11. master# vim /etc/httpd/conf/httpd.conf
    12. ```
    13. **3步: 写一个playbook文件(后缀为.yml或.yaml)**
    14. ```powershell
    15. # vim /etc/ansible/playbook/example.yaml
    16. ---
    17. - hosts: group1
    18. remote_user: root
    19. tasks:
    20. - name: ensure apache is at the latest version
    21. yum: name=httpd,httpd-devel state=latest
    22. - name: write the apache config file
    23. copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
    24. notify:
    25. - restart apache
    26. - name: ensure apache is running (and enable it at boot)
    27. service: name=httpd state=started enabled=yes
    28. handlers:
    29. - name: restart apache
    30. service: name=httpd state=restarted
    31. ```
    32. 4步: 执行写好的palybook
    33. - 会显示出执行的过程,并且执行的每一步都有ok,changed,failed等标识
    34. - 执行如果有错误(failed)会回滚,解决问题后,直接再执行这条命令即可,并会把failed改为changed(幂等性)
    35. ```powershell
    36. # ansible-playbook /etc/ansible/playbook/example.yaml
    37. ```
    38. ## Playbook常见语法
    39. **hosts:** 用于指定要执行任务的主机,其可以是一个或多个由冒号分隔主机组.
    40. **remote_user:** 用于指定远程主机上的执行任务的用户.
    41. ```powershell
    42. - hosts: group1
    43. remote_user: root
    44. ```
    45. **tasks:** 任务列表, 按顺序执行任务.
    46. - 如果一个host执行task失败, 整个tasks都会回滚, 修正playbook 中的错误, 然后重新执行即可.
    47. ```powershell
    48. tasks:
    49. - name: ensure apache is at the latest version
    50. yum: name=httpd,httpd-devel state=latest
    51. - name: write the apache config file
    52. copy: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
    53. ```
    54. **handlers:** 类似task,但需要使用notify通知调用。
    55. - 不管有多少个通知者进行了notify,等到play中的所有task执行完成之后,handlers也只会被执行一次.
    56. - handlers最佳的应用场景是用来重启服务,或者触发系统重启操作.除此以外很少用到了.
    57. ```powershell
    58. notify:
    59. - restart apache
    60. - name: ensure apache is running (and enable it at boot)
    61. service: name=httpd state=started enabled=yes
    62. handlers:
    63. - name: restart apache
    64. service: name=httpd state=restarted
    65. ```
    66. **练习:** 修改httpd的端口为8080,再执行playbook测试
    67. **variables:** 变量
    68. - 定义变量可以被多次方便调用
    69. ```powershell
    70. master# vim /etc/ansible/playbook/example2.yaml
    71. ---
    72. - hosts: group1
    73. remote_user: root
    74. vars:
    75. - user: test1
    76. tasks:
    77. - name: create user
    78. user: name={{user}} state=present
    79. ~
    80. ```
    81. ```powershell
    82. master# ansible-playbook /etc/ansible/playbook/example2.yaml
    83. ```
    84. ### 案例: playbook编排vsftpd
    85. 写一个playbook实现
    86. 1. 配置yum
    87. 2. 安装vsftpd包
    88. 3. 修改配置文件(要求拒绝匿名用户登录)
    89. 4. 启动服务并实现vsftpd服务开机自动启动
    90. ```powershell
    91. ---
    92. - hosts: group1
    93. remote_user: root
    94. tasks:
    95. - name: rm yum repository
    96. file: path=/etc/yum.repos.d/ state=absent
    97. - name: 同步master上的yum源到group1
    98. copy: src=/etc/yum.repos.d dest=/etc/
    99. - name: ensure vsftpd is at the latest version
    100. yum: name=vsftpd state=latest
    101. - name: write the apache config file
    102. copy: src=/etc/vsftpd/vsftpd.conf dest=/etc/vsftpd/vsftpd.conf
    103. notify:
    104. - restart vsftpd
    105. - name: ensure vsftpd is running (and enable it at boot)
    106. service: name=vsftpd state=started enabled=yes
    107. handlers:
    108. - name: restart vsftpd
    109. service: name=vsftpd state=restarted
    110. ```
    111. ## **playbook编排多个hosts任务**
    112. ~~~powershell
    113. --- # ---代表开始(可选项,不写也可以)
    114. - hosts: 10.1.1.12
    115. remote_user: root
    116. tasks:
    117. - name: 创建/test1/目录
    118. file: path=/test1/ state=directory
    119. # 这里不能用---分隔,会报语法错误(后面课程玩k8s编排也写YAML文件,是可以用---来分隔段落的)
    120. - hosts: 10.1.1.13
    121. remote_user: root
    122. tasks:
    123. - name: 创建/test2/目录
    124. file: path=/test2/ state=directory
    125. ... # ...代表结束(可选项,不写也可以)
    126. ~~~
    127. ### 案例: 编排nfs搭建与客户端挂载
    128. 1, 在master上准备nfs配置文件
    129. ~~~powershell
    130. # vim /etc/exports
    131. /share *(ro)
    132. ~~~
    133. 2, 编写yaml编排文件
    134. ~~~powershell
    135. # vim /etc/ansible/playbook/nfs.yml
    136. ---
    137. - hosts: 10.1.1.12
    138. remote_user: root
    139. tasks:
    140. - name: 安装nfs服务相关软件包
    141. yum: name=nfs-utils,rpcbind,setup state=latest
    142. - name: 创建共享目录
    143. file: path=/share/ state=directory
    144. - name: 同步nfs配置文件
    145. copy: src=/etc/exports dest=/etc/exports
    146. notify: restart nfs
    147. - name: 启动rpcbind服务,并设置为开机自启动
    148. service: name=rpcbind state=started enabled=on
    149. - name: 启动nfs服务,并设置为开机自启动
    150. service: name=nfs state=started enabled=on
    151. handlers:
    152. - name: restart nfs
    153. service: name=nfs state=restarted
    154. - hosts: 10.1.1.13
    155. remote_user: root
    156. tasks:
    157. - name: 安装nfs客户端软件包
    158. yum: name=nfs-utils state=latest
    159. - name: 挂载nfs服务器的共享
    160. shell: mount 10.1.1.12:/share /mnt
    161. ~~~
    162. 3, 执行playbook
    163. ~~~powershell
    164. # ansible-playbook /etc/ansible/playbook/nfs.yaml
    165. ~~~

  • 相关阅读:
    balenaEtcher格式化的U盘恢复原来样子
    518. 零钱兑换 II【完全背包:求组合数】
    git commit后如何撤销或修改
    qlib的工作流管理:mlflow机器学习生命周期管理平台
    shiro组件漏洞分析(二)
    微信小程序会议OA系统
    【C++ 提高编程】- 泛型编程之模板(类型参数化)
    刘馨蔓:“她是行走的发光体”|OneFlow U
    [nlp] chathome—家居装修垂类大语言模型的开发和评估
    企业商标信息查询API的优势和应用实例分析
  • 原文地址:https://blog.csdn.net/2302_78534730/article/details/132721068