中文名(剧本),它是一个自动化处理脚本,使用yaml语言来表示。
当前inventory文件
[student@workstation ansible]$ cat inventory
[dev]
servera
[test]
serverb
[prod]
serverc
serverd
[balancers]
serverb
[webservers:children]
prod
编写yaml剧本
[student@workstation ansible]$ vim 1-config_yum.yaml
#hosts表示要执行剧本的主机
- hosts: all
#remote_user表示远程执行的身份
remote_user: root
#tasks是任务列表的意思,每一个任务后面的name是当前任务的描述
tasks:
- name: 1.remove old yum
shell: rm -rf /etc/yum.repos.d/
- name: 2.copy new yum config to host
copy: src=/etc/yum.repos.d/ dest=/etc/yum.repos.d/
- name: 3.clean yum cache
shell: yum clean all
检测语法
[student@workstation ansible]$ ansible-playbook --syntax-check 1-config_yum.yaml
playbook: 1-config_yum.yaml
模拟执行
[student@workstation ansible]$ ansible-playbook -C 1-config_yum.yaml
PLAY [all] **************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
ok: [serverc]
ok: [serverd]
ok: [serverb]
ok: [servera]
TASK [1.remove old yum] **************************************************************************************************
skipping: [servera]
skipping: [serverc]
skipping: [serverb]
skipping: [serverd]
TASK [2.copy new yum config to host] **************************************************************************************************
ok: [servera]
ok: [serverd]
ok: [serverb]
ok: [serverc]
TASK [3.clean yum cache] **************************************************************************************************
skipping: [serverc]
skipping: [serverb]
skipping: [servera]
skipping: [serverd]
PLAY RECAP **************************************************************************************************
servera : ok=2 changed=0 unreachable=0 failed=0
serverb : ok=2 changed=0 unreachable=0 failed=0
serverc : ok=2 changed=0 unreachable=0 failed=0
serverd : ok=2 changed=0 unreachable=0 failed=0
真实执行
[student@workstation ansible]$ ansible-playbook 1-config_yum.yaml
PLAY [all] **************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
ok: [serverd]
ok: [serverb]
ok: [serverc]
ok: [servera]
TASK [1.remove old yum] **************************************************************************************************
[WARNING]: Consider using file module with state=absent rather than running rm
changed: [serverc]
changed: [serverb]
changed: [servera]
changed: [serverd]
TASK [2.copy new yum config to host] **************************************************************************************************
changed: [servera]
changed: [serverd]
changed: [serverc]
changed: [serverb]
TASK [3.clean yum cache] **************************************************************************************************
[WARNING]: Consider using yum module rather than running yum
changed: [serverb]
changed: [servera]
changed: [serverc]
changed: [serverd]
PLAY RECAP **************************************************************************************************
servera : ok=4 changed=3 unreachable=0 failed=0
serverb : ok=4 changed=3 unreachable=0 failed=0
serverc : ok=4 changed=3 unreachable=0 failed=0
serverd : ok=4 changed=3 unreachable=0 failed=0
剧本文件名可以是.yml或者.yaml结尾
里面的内容只能以空格缩进
可以配置vim方便编写剧本
[student@workstation ansible]$ head -1 /etc/vimrc
set nu ai ts=2 sw=2 cursorcolumn cursorline
--- #代表文件开始
....
…#文件结尾,可省略
- play1
- play2
- play3
tasks下面的每一个列表就是一个任务
key: value
示例
id: 1
name: xiaoniu
passwd: 123456
检测语法:ansible-playbook --syntax-check config_yum.yaml
模拟执行:ansible-playbook -C config_yum.yaml
真实执行:ansible-playbook config_yum.yaml
可以写在剧本中,与hosts,tasks同级
- hosts: all
remote_user: devops
become: true
become_method: sudo
become_user: root
tasks:
- debug:
msg: "Test"
也可以提前写在配置文件中
[defaults]
inventory = /home/student/ansible/inventory
remote_user = devops
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
--become,-b | 使用提权 |
---|---|
--ask-become-pass,-K | 询问提权用户的密码 |
--become-method | 提权方式 |
--become-user | 提升权限的用户 |
[student@workstation ansible]$ ansible -b -K --become-method=sudo --become-user=root all -m ping
SUDO password:
servera | SUCCESS => {
"changed": false,
"ping": "pong"
}
serverc | SUCCESS => {
"changed": false,
"ping": "pong"
}
serverb | SUCCESS => {
"changed": false,
"ping": "pong"
}
serverd | SUCCESS => {
"changed": false,
"ping": "pong"
}
两种方式: “ |”和“ >”,表明多行字符串是一个整体。
“ >”会把换行符替换成空格
“ |”表明多行字符串是一个整体。
[student@workstation ansible]$ cat str.yml
- hosts: all
tasks:
- debug:
msg: >
This is one line
This is two line
- debug:
msg: |
Example one line
Example two line
查看结果
TASK [debug] **************************************************************************************************
ok: [servera] => {
"msg": "This is one line This is two line\n"
}
ok: [serverc] => {
"msg": "This is one line This is two line\n"
}
ok: [serverb] => {
"msg": "This is one line This is two line\n"
}
ok: [serverd] => {
"msg": "This is one line This is two line\n"
}
TASK [debug] **************************************************************************************************
ok: [serverb] => {
"msg": "Example one line\nExample two line\n"
}
ok: [servera] => {
"msg": "Example one line\nExample two line\n"
}
ok: [serverc] => {
"msg": "Example one line\nExample two line\n"
}
ok: [serverd] => {
"msg": "Example one line\nExample two line\n"
比如上面那个例子中,就是shorthand格式
- name: 2.copy new yum config to host
copy: src=/etc/yum.repos.d/ dest=/etc/yum.repos.d/
正常yaml格式
- name: 2.copy new yum config to host
copy:
src: /etc/yum.repos.d/
dest: /etc/yum.repos.d/
启动一个http服务
- hosts: all
vars:
- web_root: /var/www/html
- srv_name: www.example.com
- http_port: 9090
tasks:
- name: copy configfile to host
#src是本机模板文件,dest是目标主机的文件
template: src=/playbook/myweb.conf dest=/etc/httpd/conf.d/myweb.conf
- name: make web index
shell: echo "Ansible Genareic" > {{ web_root }}/index.html
- name: stop selinux firewalld
shell: setenforce 0 && iptables –F
#忽略错误继续执行
ignore_errors: yes
- name: start httpd
service: name=httpd state=restarted
查看模板文件
[root@ansible playbook]# cat /playbook/myweb.conf
<VirtualHost {{ ansible_default_ipv4.address }}:{{ http_port }}>
DocumentRoot {{ web_root }}
ServerName {{ srv_name }}
<Directory {{ web_root }}>
Require all granted
</Directory>
</Virtualhost>
name | 仓库的文件名称 |
---|---|
baseurl | yum源地址 |
description | 描述信息 |
file | 仓库文件前缀,默认是以name参数作为前缀,同一个.repo配置文件 中可以存在多个源 |
enabled | (yes|no) 是否开户yum源 |
gpgcheck | (yes|no) 是否启用包验证 |
gpgkey | 公钥的位置,当gpgcheck=yes时生效 |
state | present 添加 absent 删除,默认present |
使用playbook方式来添加yum源
- hosts: myhosts
remote_user: root
tasks:
- name: config yum
yum_repository: name=dvd baseurl=file:///media description=dvd enabled=yes gpgcheck=no
删除远程主机的dvd yum源
ansible myhosts -m yum_repository -a 'name=dvd state=absent'
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有