Linux Ansible playbook解析
2023-02-18 16:38:31 时间
文章目录
实施playbook
vimrc
[devops@workstation ansible]$ vim ~/.vimrc
set number ts=2 sw=2 et
批量缩进
1.按ctrl v进入可视化块模式
2.按↑↓方向键选中你要缩进的行
3.按I(大)进入输入模式
4.按tab键缩进
5.按esc(两)
帮助文档
[devops@workstation ansible]$ ansible-doc -l | grep yum
[devops@workstation ansible]$ ansible-doc yum
/EX
playbook
playbook格式
--- //固定开头格式
2 - name: 安装软件包 //注释说明
3 hosts: qq //对哪个主机或主机组进行操作
4 tasks: //具体任务
ad-hoc
[devops@workstation ansible]$ ansible qq -m user -a "name=bob comment=testbob uid=1050 group=root"
servera | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "testbob",
"create_home": true,
"group": 0,
"home": "/home/bob",
"name": "bob",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1050
}
[devops@workstation ansible]$ ansible qq -a "id bob"
servera | CHANGED | rc=0 >>
uid=1050(bob) gid=0(root) groups=0(root)
playbook
[devops@workstation ansible]$ vim user.yml
1 ---
2 - name: useradd user
3 hosts: qq
4 tasks:
5 - name: Add the user 'johnd'
6 user:
7 name: johnd
8 comment: John Doe
9 uid: 1040
10 group: root
[devops@workstation ansible]$ ansible-playbook user.yml
验证
[devops@workstation ansible]$ ansible qq -a "id johnd"
servera | CHANGED | rc=0 >>
uid=1040(johnd) gid=0(root) groups=0(root)
检查语法
[devops@workstation ansible]$ ansible-playbook user.yml -C //空运行
[devops@workstation ansible]$ ansible-playbook user.yml -vvv //执行playbook时查看详细信息
[devops@workstation ansible]$ ansible-playbook user.yml --syntax-check //检查语法
清单文件的密码和临时命令密码做优先级
[devops@workstation ansible]$ vim inventory
1 [all:vars]
2 ansible_password=redhat1
[devops@workstation ansible]$ ansible qq -a "id"
servera | UNREACHABLE! => {
"changed": false,
"msg": "Invalid/incorrect password: Permission denied, please try again.",
"unreachable": true
}
原因:密码错误
[devops@workstation ansible]$ ansible qq -a "id" -e ansible_password=redhat
servera | CHANGED | rc=0 >>
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
没有问题,说明变量以临时命令优先
第三题:安装软件
[devops@workstation ansible]$ vim /home/devops/ansible/packages.yml
1 ---
2 - name: install
3 hosts: dev,test,prod
4 tasks:
5 - name: ensure a list of packages installed
6 yum:
7 name: "{{ packages }}"
8 vars:
9 packages:
10 - php
11 - mariadb
12 - name: install
13 hosts: dev
14 tasks:
15 - name: install the 'Development tools' package group
16 yum:
17 name: "@RPM Development Tools"
18 state: present
19 - name: upgrade all packages
20 yum:
21 name: '*'
22 state: latest
[devops@workstation ansible]$ ansible-playbook /home/greg/ansible/packages.yml
第一题梳理:主机清单
ssh greg@control
sudo yum install -y ansible
mkdir -p /home/greg/ansible/roles
cd ansible
cp /etc/ansible/ansible.cfg .
ansible --version
vim ansible.cfg
inventory = /home/greg/ansible/inventory
69 #roles_path = /etc/ansible/roles
70 roles_path = /home/greg/ansible/roles
vim inventory
第二题:脚本
[devops@workstation ansible]$ vim /home/devops/ansible/adhoc.sh
1 #!/bin/bash
2
3 ansible all -m yum_repository -a "name=EX294_BASE description='EX294 base software' baseurl=http://co ntent/rhel8.4/x86_64/dvd/BaseOS gpgcheck=yes gpgkey=http://content/rhel8.4/x86_64/dvd/RPM-GPG-KEY-red hat-release enabled=yes"
4 ansible all -m yum_repository -a "name=EX294_STREAM description='EX294 stream software' baseurl=http: //content/rhel8.4/x86_64/dvd/AppStream gpgcheck=yes gpgkey=http://content/rhel8.4/x86_64/dvd/RPM-GPG- KEY-redhat-release enabled=yes"
[devops@workstation ansible]$ chmod +x /home/devops/ansible/adhoc.sh
[devops@workstation ansible]$ /home/devops/ansible/adhoc.sh
相关文章
- git分支使用规范
- Helm 搭建K8S环境gitlab-runner
- 生物信息数据分析教程视频——15-clusterProfiler包+ClueGO做富集分析
- IMvigor210CoreBiologies包官方下载链接失效问题
- Kagol:2022年最值得推荐的前端开源文章
- 把时间沉淀到自己的热爱里 | Kagol 的 2022 年终总结
- 使用Git,10件你可能需要“反悔”的事
- 如何将MongoDB数据库的数据迁移到MySQL数据库中
- Linux下服务器环境的搭建和配置之一——Apache篇
- Node使用Mongoose操作MongoDB数据库——增删改查的实现
- 如何使用自对弈强化学习训练一个五子棋机器人Alpha Gobang Zero
- 【华为云技术分享】MongoDB经典故障系列五:sharding集群执行sh.stopBalancer()命令被卡住怎么办?
- GitHub车牌检测识别项目调研
- Linux基础-学会使用命令帮助
- Linux 基础-新手必备命令
- Linux 基础-文件及目录管理
- Linux 基础-文本处理命令
- Linux 基础-文件权限与属性
- Linux 基础-查看进程命令 ps 和 top
- Linux基础-查看和设置环境变量