文章目录
ansible-playbook介绍:
playbook.yaml格式
--- #开头三个小-开头
- hosts: webB #可以执行host(默认也可以读取/etc/ansible/hosts的配置)
tasks: #task指定执行任务
- name: test01 #多个动作,需要定义多个name
shell: echo "这是第一个动作"
- name: test2 #多个动作,需要定义多个name
shell:uname -m
playbook.yaml执行方式:
ansible-playbook playbook.yaml
playbook设置debug模式输出:
yaml默认无debug输出,yaml设置如下:
--- #开头三个小-开头
- hosts: all #可以执行host(默认也可以读取/etc/ansible/hosts的配置)
tasks: #task指定行任务
- name: test01 #多个动作,需要定义多个name
shell: echo "这是第一个动作"
- name: test2 #多个动作,需要定义多个name
shell: echo "这是二个动作"
无debug模式设置输出结果:
yaml设置debug模式,配置如下:
单个执行动作可以这样设置:
tasks:
- name: FIRST JOB
shell: echo "welcome to test01 `hostname -I`"
register: var_result #将之前命令的输出结果保存在变量var_result里
- debug: var=var_result ##将变量的值作为debug输出出来。
多个执行动作可以这样设置:
tasks:
- name: 第一个动作
shell: echo "第一个动作 `hostname -I`"
register: var_result
- debug: var=var_result
- name: 第二个动作
shell: echo "这是第二个动作 `hostname -I`"
register: var_result #将之前命令的输出结果保存在变量var_result里
- debug: var=var_result #将变量的值作为debug输出出来。
完整代码:
[root@KVM20201208-0 gao]# cat o.yaml
--- #开头三个小-开头
- hosts: all #可以执行host(默认也可以读取/etc/ansible/hosts的配置)
tasks:
- name: 第一个动作
shell: echo "第一个动作 `hostname -I`"
register: var01_result #将之前命令的输出结果保存在变量var01_result里
- debug: var=var1_result #将变量的值作为debug输出出来。
- name: 第二个动作
shell: echo "这是第二个动作 `hostname -I`"
register: var02_result #将之前命令的输出结果保存在变量var02_result里
- debug: var=var02_result #将变量的值作为debug输出出来。
带有debug模式输出结果:
[root@kVM20606098-0 gao]# ansible-playbook o.yaml
PLAY [all] **************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
[WARNING]: Platform linux on host web63 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter
could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [web63]
ok: [web33]
TASK [第一个动作] ************************************************************************************************************************************************
changed: [web63]
changed: [web33]
TASK [debug] ************************************************************************************************************************************************
ok: [web63] => {
"var_result": {
"changed": true,
"cmd": "echo \"第一个动作 `hostname -I`\"",
"delta": "0:00:00.023953",
"end": "2021-01-24 20:01:38.820180",
"failed": false,
"rc": 0,
"start": "2021-01-24 20:01:38.796227",
"stderr": "",
"stderr_lines": [],
"stdout": "第一个动作 81.67.56.83 ",
"stdout_lines": [
"第一个动作 81.67.56.83 "
]
}
}
ok: [web33] => {
"var_result": {
"changed": true,
"cmd": "echo \"第一个动作 `hostname -I`\"",
"delta": "0:00:00.026612",
"end": "2021-01-24 20:01:38.848623",
"failed": false,
"rc": 0,
"start": "2021-01-24 20:01:38.822011",
"stderr": "",
"stderr_lines": [],
"stdout": "第一个动作 10.1.18.33 ",
"stdout_lines": [
"第一个动作 10.1.18.33 "
]
}
}
TASK [第二个动作] ************************************************************************************************************************************************
changed: [web63]
changed: [web33]
TASK [debug] ************************************************************************************************************************************************
ok: [web63] => {
"var_result": {
"changed": true,
"cmd": "uname -m",
"delta": "0:00:00.023328",
"end": "2021-01-24 20:01:39.528240",
"failed": false,
"rc": 0,
"start": "2021-01-24 20:01:39.504912",
"stderr": "",
"stderr_lines": [],
"stdout": "x86_64",
"stdout_lines": [
"x86_64"
]
}
}
ok: [web33] => {
"var_result": {
"changed": true,
"cmd": "uname -m",
"delta": "0:00:00.025336",
"end": "2021-01-24 20:01:39.593545",
"failed": false,
"rc": 0,
"start": "2021-01-24 20:01:39.568209",
"stderr": "",
"stderr_lines": [],
"stdout": "x86_64",
"stdout_lines": [
"x86_64"
]
}
}
PLAY RECAP **************************************************************************************************************************************************
web33 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web63 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
playbook的自定义变量<重点>
【说明】:
A)- hosts下面,定义变量vars:
B)下面接着开始正式定义,目标变量
yaml配置如下:
[root@kVM2057086 user]# cat test.yaml
--- #开头三个小-开头
- hosts: all #可以执行host(默认也可以读取/etc/ansible/hosts的配置)
vars: #定义变量
where: beijing #设置第一个变量where
work: linux_user #设置第二个变量work
tasks:
- name: 第一个动作
shell: echo "我在 {{ where }},我的工作时 {{ work }}"
register: var_result #将之前命令的输出结果保存在变量var_result里
- debug: var=var_result #将变量的值作为debug输出出来。
-
输出结果:
[root@kVM2057086 user]# ansible-playbook test.yaml
PLAY [all] **************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
[WARNING]: Platform linux on host web63 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter
could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [web63]
TASK [第一个动作] ************************************************************************************************************************************************
changed: [web63]
TASK [debug] ************************************************************************************************************************************************
ok: [web63] => {
"var_result": {
"changed": true,
"cmd": "echo \"我在 beijing,我的工作时 linux_user\"",
"delta": "0:00:00.023028",
"end": "2021-01-24 20:33:51.798498",
"failed": false,
"rc": 0,
"start": "2021-01-24 20:33:51.775470",
"stderr": "",
"stderr_lines": [],
"stdout": "我在 beijing,我的工作时 linux_user",
"stdout_lines": [
"我在 beijing,我的工作时 linux_user"
]
}
}
PLAY RECAP **************************************************************************************************************************************************
web63 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
yaml通过设置变量怎么使用ansible-book创建目录呢???
代码:
[root@kVM20606098-0 gao]# ansible-playbook o.yaml
--- #开头三个小-开头
- hosts: all #可以执行host(默认也可以读取/etc/ansible/hosts的配置)
vars: #定义变量
new_dir: beijing #设置第一个变量where
tasks:
- name:
shell: mkdir -p /tmp/"{{ new_dir }}"
register: var_result #将之前命令的输出结果保存在变量var_result里
- debug: var=var_result #将变量的值作为debug输出出来。
结果:
PLAY [all] **************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
[WARNING]: Platform linux on host web63 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter
could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [web63]
TASK [shell] ************************************************************************************************************************************************
[WARNING]: Consider using the file module with state=directory rather than running 'mkdir'. If you need to use command because file is insufficient you can
add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [web63]
TASK [debug] ************************************************************************************************************************************************
ok: [web63] => {
"var_result": {
"changed": true,
"cmd": "mkdir -p /tmp/\"beijing\"",
"delta": "0:00:00.023702",
"end": "2021-01-24 20:41:23.090041",
"failed": false,
"rc": 0,
"start": "2021-01-24 20:41:23.066339",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": [],
"warnings": [
"Consider using the file module with state=directory rather than running 'mkdir'. If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message."
]
}
}
PLAY RECAP **************************************************************************************************************************************************
web63 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-playbook下发,含有变量的配置文件:(重点)
背景说明:
1)ansible master有nginx.conf,该文件有 “变量”
2)配置文件中的 “变量”,来源于,ansible-playbook yaml文件
yaml中设置变量:
--- #开头三个小-开头
- hosts: all
gather_facts: True #开启系统变量
vars:
nginx_info: nginx-1.9.1-3.el7.x86_64 #设置变量
tasks:
- name: 分发nginx主配置文件
template: src=./nginx.conf dest=/tmp/
#使用template下发可变配置文件
#意思:将ansible当前目录下nginx.conf,分发到远程机器/tmp目录下
【注意】:
gather_facts: True #开启系统变量
nginx_info: nginx-1.9.1-3.el7.x86_64 #设置变量
template: src=./nginx.conf dest=/tmp/ #使用template下发可变配置文件
ansible master当前目录下的nginx.conf是这样的(带变量的)
[root@kVM20706278-9 user]# cat nginx.conf
1 这是nginx配置文件
2 这行引用ansible master yaml中的变量: nginx_info {{ nginx_info }}
在查看下远程机器/tmp/nginx.conf(变量值获取到了)
扩展:将执行结果输出到指定log中:
ansible-playbook test.yaml 2>&1 | tee ansible_run.log
ansible-playbook test.yaml 2>&1 | tee -a ansible_run.log
【支持】:信息追加
更多资料:
https://www.cnblogs.com/robertx/p/10827768.html