自动化运维—playbook(变量结合debug模块&获取远程主机信息)
文章目录
- 自动化运维---playbook(变量结合debug模块&获取远程主机信息)
- 1.获取远程主机信息
- 2.debug模块
- 3.debug结合变量使用
- 4.debug结合变量获得远程主机信息:
- 5.注册变量
- playbook1
- playbook2
- playbook3
- playbook4
- playbook5
- playbook6
- 6.ansible内置变量
1.获取远程主机信息
我们会发现每次执行playbook时都会有TASK [Gathering Facts]的动作,这个动作是在收集主机信息,是系统自动帮我们添加的set up模块
手动获取信息:
ansible testB -m setup
获取指定信息:
ansible testB -m setup -a "filter=*mb*"
ansible_local信息:
这个信息默认在/etc/ansible/facts.d路径下
首先去远程主机中创建测试信息:
mkdir /etc/ansible/facts.d
vim info.fact
[message]
msg1=This is the first custom test message
msg2=This is the second custom test message
注意:
1.该文件必须以.fact结尾
2.文件内容使用INI语法
获取ansible_local信息:
ansible testB -m setup -a "filter=ansible_local"
如果我们将测试文件移动到别的目录中(即本地信息不在默认路径下):
mv info.fact /testdir/
需要在查找信息时加上fact_path=/testdir信息:
ansible testB -m setup -a "fact_path=/testdir filter=ansible_local"
2.debug模块
它会在操作行中输出我们指定的信息:
vim debug1.yml
---
- hosts: testB
remote_user: root
tasks:
- name: task1
file:
path=/testdir/file1
state=touch
- name: debug demo
debug:
msg: this is debug indo,the test file has been touched.
3.debug结合变量使用
在操作行输出变量的值:
vim debug2.yml
---
- hosts: testB
remote_user: root
vars:
testvar: value of test variable
tasks:
- name: debug demo
debug:
var: testvar
在操作行输出指定信息和变量的值:
vim debug2.yml
---
- hosts: testB
remote_user: root
vars:
testvar: this is a testvar
tasks:
- name: debug demo
debug:
msg: "value of testvar is : {{testvar}}"
注意:最后一行使用引号是因为其中包含特殊符号:
4.debug结合变量获得远程主机信息:
vim debug2.yml
---
- hosts: testB
remote_user: root
tasks:
- name: debug demo
debug:
msg: "remote host memory information : {{ansible_memory_mb}}"
指定获取ansible_memory_mb信息中的real模块:
查看到ansible_memory_mb信息中的real模块:
vim debug2.yml
---
- hosts: testB
remote_user: root
tasks:
- name: debug demo
debug:
msg: "remote host memory information : {{ansible_memory_mb.real}}"
5.注册变量
注册变量:将模块的返回值引用到变量中的方法
playbook1
将模块返回值输出在操作行中:
vim bl1.yml
---
- hosts: testB
remote_user: root
tasks:
- name: test shell
shell: "echo test > /var/testshellfile"
register: testvar
- name: shell module return values
debug:
var: testvar
写法2:
vim bl1.yml
---
- hosts: testB
remote_user: root
tasks:
- name: test shell
shell: "echo test > /var/testshellfile"
register: testvar
- name: shell module return values
debug:
msg: "{{testvar}}"
取返回值中指定信息:
vim bl1.yml
---
- hosts: testB
remote_user: root
tasks:
- name: test shell
shell: "echo test > /var/testshellfile"
register: testvar
- name: shell module return values
debug:
msg: "{{testvar.cmd}}"
写法2:
vim bl1.yml
---
- hosts: testB
remote_user: root
tasks:
- name: test shell
shell: "echo test > /var/testshellfile"
register: testvar
- name: shell module return values
debug:
msg: "{{testvar['cmd']}}"
playbook2
获取用户输入信息并返回(vars_prompt):
vim bl2.yml
---
- hosts: testB
remote_user: root
vars_prompt:
- name: "your_name"
prompt: "what is your name"
- name: "your_age"
prompt: "how old are you"
tasks:
- name: output vars
debug:
msg: Your name is {{your_name}},you are {{your_age}} years old.
但我们发现此时用户输入的值不回显,这比较适合输入密码一类的信息,那么如何在输入信息时显示呢?
只需要引入private: no参数即可
vim bl2.yml
---
- hosts: testB
remote_user: root
vars_prompt:
- name: "your_name"
prompt: "what is your name"
private: no
- name: "your_age"
prompt: "how old are you"
private: no
tasks:
- name: output vars
debug:
msg: Your name is {{your_name}},you are {{your_age}} years old.
此时用户的输入信息就可以看到了
playbook3
当用户不输入信息时设置默认值(default):
---
- hosts: testB
remote_user: root
vars_prompt:
- name: "solution"
prompt: "Choose the solution you want \n
A: solutionA\n
B: solutionB\n
C: solutionC\n"
private: no
default: A
tasks:
- name: outpur vars
debug:
msg: the final solution is {{solution}}.
当用户不输入信息直接回车时,默认选择A;当选择其他项时,则为用户选择结果
playbook4
如何利用前面学的内容写一个剧本,输入用户名和密码,然后在远程主机创建对应的用户?
vim useradd.yml
---
- hosts: testB
remote_user: root
vars_prompt:
- name: "user_name"
promote: "Enter user name"
private: no
- name: "user_password"
promote: "Enter user password"
tasks:
- name: create user
user:
name: "{{user_name}}"
password: "{{user_password}}"
初步写出的内容是这样的,思路没有任何问题,但是有一点:之前在user模块中,创建用户时密码不能是明文密码,是要经过加密的,但在这里我直接明文密码创建了用户。如何解决这一问题?
此时需要借助python的模块:
wget https://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz
tar zxf setuptools-0.6c11.tar.gz
并且我们需要到python的官网下载pip的包:
tar zxf pip-20.0.2.tar.gz
cd pip-20.0.2
ls
python setup.py install
pip install passlib
完成以上操作后,我们先写一个剧本测试以下加密功能:
vim bl4.yml
---
- hosts: testB
remote_user: root
vars_prompt:
- name: "hash_string"
prompt: "Enter something"
private: no
encrypt: "sha512_crypt"
tasks:
- name: output the hash string
debug:
msg: "{{hash_string}}"
可以看到返回了加密后的内容
此时我们修改一下创建用户的剧本:
vim useradd.yml
---
- hosts: testB
remote_user: root
vars_prompt:
- name: "user_name"
promote: "Enter user name"
private: no
- name: "user_password"
promote: "Enter user password"
encrypt: "sha512_crypt"
confirm: yes
tasks:
- name: create user
user:
name: "{{user_name}}"
password: "{{user_password}}"
encrypt: "sha512_crypt"的作用是加密,而confirm: yes的作用是让用户重复输入两次
playbook5
在playbook中直接引用变量,而在命令行中定义变量:
vim bl5.yml
---
- hosts: testB
remote_user: root
tasks:
- name: "passing variables on the command line"
debug:
msg: "{{pass_var}}"
定义变量要引入–extra-vars参数(可简写为-e):
ansible-playbook bl5.yml --extra-vars "pass_var=redhat"
多个变量:
vim bl5.yml
---
- hosts: testB
remote_user: root
tasks:
- name: "passing variables on the command line"
debug:
msg: "{{pass_var,num_var}}"
当定义了多个变量而在命令行中引用其中一个变量时,只返回引用的那个变量:
ansible-playbook bl5.yml -e 'pass_var="redhat" num_var="666"'
如果在playbook中定义了变量,又在命令行中定义变量,会返回命令行定义的结果:
vim bl5.yml
---
- hosts: testB
remote_user: root
vars:
pass_var: test_default
tasks:
- name: "passing variables on the command line"
debug:
msg: "{{pass_var}}"
由此可以看出命令行的优先级高于与playbook
还可以在命令行中定义和调用变量:
ansible testB -e "testvar=test" -m shell -a "echo {{testvar}}"
在文件中定义变量:
vim testvar
testvar: testfile
numlist:
- a
- b
- c
- d
vim bl5.yml
---
- hosts: testB
remote_user: root
tasks:
- name: "passing variables on the command line"
debug:
msg: "{{testvar}} {{numlist}}"
执行时要指明文件路径:
ansible-playbook bl5.yml -e "@/root/playbook/testvar"
playbook6
之前定义变量时都是与tasks处于同级,在tasks外面定义然后在tasks内部调用。如果要在tasks内部定义,就要使用set_fact关键字
vim bl6.yml
---
- hosts: testB
remote_user: root
tasks:
- set_fact:
testvar: "test"
- debug:
msg: "{{testvar}}"
注册变量的值可以赋给另一个变量:
vim bl6.yml
---
- hosts: testB
remote_user: root
vars:
testvar1: test1_string
tasks:
- shell: "echo test2_string"
register: shellreturn
- set_fact:
testsf1: "{{testvar1}}"
testsf2: "{{shellreturn.stdout}}"
- debug:
var: shellreturn
可以看到"stdout": "test2_string"内容,我们需要将test2_string赋给另一个变量:
vim bl6.yml
---
- hosts: testB
remote_user: root
vars:
testvar1: test1_string
tasks:
- shell: "echo test2_string"
register: shellreturn
- set_fact:
testsf1: "{{testvar1}}"
testsf2: "{{shellreturn.stdout}}"
- debug:
msg: "{{testsf1}} {{testsf2}}"
此时看到debug返回给我们的结果是:“test1_string test2_string”。其中test1_string是定义在vars中的变量testvar1的值,而test2_string是注册变量的值取了stdout的结果
用set_fact定义的变量可以在另一个部分中使用,但vars定义的不可以:
vim bl6.yml
---
- hosts: testB
remote_user: root
vars:
testvar1: tv1
tasks:
- set_fact:
testvar2: tv2
- debug:
msg: "{{testvar1}} ---- {{testvar2}}"
- hosts: testB
remote_user: root
tasks:
- name: other play get testvar2
debug:
msg: "{{testvar2}}"
- name: other play get testvar1
debug:
msg: "{{testvar1}}"
由返回结果可以看出:在第一块内容中引用vars和set_fact定义的变量都可以被引用(返回了"msg": “tv1 ---- tv2”);而在第二块内容中只有set_fact定义的变量(“msg”: “tv2”)可以被引用,到引用vars定义的变量时报错
注册变量也可以在其他部分使用:
vim bl6.yml
---
- hosts: testB
remote_user: root
vars:
testvar1: tv1
tasks:
- shell: "echo tv2"
register: testvar2
- debug:
msg: "{{testvar1}} ---- {{testvar2.stdout}}"
- hosts: testB
remote_user: root
tasks:
- name: other play get testvar2
debug:
msg: "{{testvar2.stdout}}"
- name: other play get testvar1
debug:
msg: "{{testvar1}}"
查看到注册变量的结果成功返回
6.ansible内置变量
获取ansible版本信息:
ansible testB -m debug -a "msg={{ansible_version}}"
获取被操作当前主机的主机名:
注意:这里获取的主机名不是linux系统的主机名,而是写在ansible清单中的主机名
首先我们编辑ansible清单:
vim /etc/ansible/hosts
我把之前的信息注释掉,并且添加了新的信息作为测试如图:
获取test-group组中的主机名:
ansible test-group -m debug -a "msg={{inventory_hostname}}"
获取test-group组中的主机名(短名,即第一个.之前的内容):
ansible test-group -m debug -a "msg={{inventory_hostname_short}}"
查看ansible清单中的分组情况(本地):
首先编辑ansible清单如图:
ansible test1 -m debug -a "msg={{groups}}"
查看testA组的主机:
ansible test1 -m debug -a "msg={{groups.testA}}"
查看test的主机:
ansible test1 -m debug -a "msg={{groups.test}}"
查看未分组的主机:
ansible test1 -m debug -a "msg={{groups.ungrouped}}"
查看指定主机所在组的名称:
ansible test1 -m debug -a "msg={{group_names}}"
查看本地ansible清单保存的路径:
ansible test1 -m debug -a "msg={{inventory_dir}}"