ansible定义变量



定义变量

ansible定义变量_变量名


在主机清单中定义变量

node1 myvar="hello" myvar2="test"
node2 yourname="omaidb"


剧本中使用变量

---
- name: copy模块示例1
hosts: node1
tasks:
- name: 将变量中的内容添加到文件中
copy:
content: "主机名是:{{ansible_hostname}}\nbios版本是:{{ansible_bios_version}}\n"
dest: /tmp/facts.txt

- name: 打印myvar变量和myvar2变量
hosts: node1
tasks:
- name: 打印node1主机清单中的变量
debug:
msg: "{{myvar}},{{myvar2}}"

- name: copy模块示例2
hosts: node2
tasks:
- name: 将变量中的内容添加到node2的文本中
copy:
content: "{{yourname}}\n"
dest: /tmp/facts.txt

- name: playbook中定义变量示例
hosts: node1
tasks:
- name: playbook中定义变量
shell: hostname
# register表示将shell输出的信息存到myvar变量中
register: myvar


- name: 打印上面定义的myvar变量(也就是hostname的信息)
debug:
msg: "{{myvar}}"

- name: 显示myvar的指定key的数据(子变量)
debug:
msg: "运行的节点是{{myvar.stdout}}"


Host Factsbianl(ansible收集的系统信息保存在特定的变量中)

ansible定义变量_ios_02

示例剧本

---
- name: 演示facts变量
hosts: all
tasks:
- name: 用户facts信息
copy:
content: "主机名是:{{ansible_hostname}},\nbios版本是:{{ansible_bios_version}}\n"
dest: /tmp/facts.txt

执行结果

ansible定义变量_ios_03


register接收变量


打印接收的变量信息

---
- name: register接收变量演示
hosts: all
tasks:
- name: shell执行hostname,并将hostname结果保存到变量名myvar
shell: hostname
register: myvar
- name: 打印接收的myvar变量
debug:
msg: "{{ myvar }}"


  • 执行结果
[rhel8 root ~/ansible/play]# ansible-playbook reg.yml 

PLAY [register接收变量演示] ****************************************************

TASK [shell执行hostname,并将hostname结果保存到变量名myvar] *********************
changed: [node2]
changed: [node1]

TASK [打印接收的myvar变量] *****************************************************
ok: [node1] => {
"msg": {
"changed": true,
"cmd": "hostname",
"delta": "0:00:00.003724",
"end": "2021-10-18 02:36:29.122224",
"failed": false,
"msg": "",
"rc": 0,
"start": "2021-10-18 02:36:29.118500",
"stderr": "",
"stderr_lines": [],
"stdout": "node1",
"stdout_lines": [
"node1"
]
}
}
ok: [node2] => {
"msg": {
"changed": true,
"cmd": "hostname",
"delta": "0:00:00.004386",
"end": "2021-10-18 02:36:29.119046",
"failed": false,
"msg": "",
"rc": 0,
"start": "2021-10-18 02:36:29.114660",
"stderr": "",
"stderr_lines": [],
"stdout": "node2",
"stdout_lines": [
"node2"
]
}
}

PLAY RECAP *********************************************************************
node1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0


只打印子变量信息

---
- name: register接收变量演示
hosts: all
tasks:
- name: shell执行hostname,并将hostname结果保存到变量名myvar
shell: hostname
register: myvar
- name: 打印接收的myvar变量
debug:
msg: "{{ myvar.stdout }}"


  • 执行结果
[rhel8 root ~/ansible/play]# ansible-playbook reg.yml 

PLAY [register接收变量演示] ****************************************************

TASK [shell执行hostname,并将hostname结果保存到变量名myvar] *********************
changed: [node2]
changed: [node1]

TASK [打印接收的myvar变量] *****************************************************
ok: [node1] => {
"msg": "node1"
}
ok: [node2] => {
"msg": "node2"
}

PLAY RECAP *********************************************************************
node1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0


剧本中定义变量


剧本中定义变量来创建和删除用户

---
- name: 剧本文件中定义变量
hosts: all
vars:
iname: omaidb
ipass: '123456'

tasks:
- name: 使用剧本中定义的变量来创建用户
user:
name: "{{iname}}"
password: "{{ ipass | password_hash('sha512')}}"

tasks:
- name: 使用剧本中定义的变量来删除用户
user:
name: "{{iname}}"
state: absent
remove: yes


剧本中定义提示变量创建用户

---
- name: 剧本中的提示变量
hosts: all
vars_prompt: # 定义提示变量
- name: iname # 定义iname空变量
prompt: "请输入用户名" # 提示输入用户名
private: no # 是否隐私信息,no将回显输入内容
- name: ipass # 定义ipass空变量
prompt: "请输入密码" # 提示输入密码
private: yes # 是否隐私信息,yes将不显示输入内容
tasks:
- name: 从提示变量中创建用户
user:
name: "{{ iname }}"
password: "{{ ipass | password_hash('sha512')}}"


单独定义变量文件

单独定义个变量文件,在playbook中用vars_files调用该变量文件

  • 变量文件
---
iname: cloud
ipass: '123456'

  • palybook中导入变量文件
---
- name: 在剧本中导入变量文件
hosts: all
vars_files: variables.yml
tasks:
- name: 创建用户
user:
name: "{{ iname }}"
password: "{{ ipass | password_hash('sha512') }}"

  • 执行结果
    ansible定义变量_删除用户_04
  • 引用变量文件删除刚才创建的用户
---
- name: 引用变量文件删除用户
hosts: all
vars_files: variables.yml
tasks:
- name: 删除用户
user:
name: "{{ iname }}"
state: absent
remove: yes

ansible定义变量_删除用户_05


执行ansible-playbook命令时使用-e参数定义变量

  • 编写剧本
---
- name: 演示ansible-playbook命令执行时-e参数定义变量
hosts: all
tasks:
- name: 创建用户
user:
name: "{{ iname }}"
password: "{{ ipass | password_hash('sha512') }}"

  • 执行ansible-play变量
# 执行剧本
[rhel8 root ~/ansible/play]# ansible-playbook -C var_local.yml -e iname="omaidb" -e ipass="123456"

# 查看node1执行结果
[node1 root ~]# id omaidb
uid=1000(omaidb) gid=1000(omaidb) 组=1000(omaidb)

# 查看node2执行结果
[node2 root ~]# id omaidb
uid=1000(omaidb) gid=1000(omaidb) 组=1000(omaidb)

  • 演示自定义变量删除用户
---
- name: 演示ansible-playbook命令执行时-e参数定义变量
hosts: all
tasks:
- name: 删除用户
user:
name: "{{ iname }}"
state: absent
remove: yes

  • 执行结果
    ansible定义变量_ansible_06