ansible5 变量

1.只能包含下划线,数字,字母,只能下划线和字母开头
play》主机》全局
举个例子:
在这里插入图片描述vars:
NAME:就是变量
NAME可以是A,也可以是B
user模块换行缩进,"{{NAME}}"是ansible中的格式

[root@serverb mnt]# id chihao 
uid=1001(chihao) gid=1001(chihao) groups=1001(chihao)

受控机已经建立了名为chihao的用户
(2)也可以在文件中指定变量:

[root@bastion ansible]# vim user.yml

在里面写入:
在这里插入图片描述把刚才的playbook改成:
在这里插入图片描述
这样也可以执行。在文件中指定变量 ,格式如此:
在这里插入图片描述(3)在清单里指定变量

在清单写:
在这里插入图片描述prod组作为变量
然后修改刚才的playbook
在这里插入图片描述

[root@serverb mnt]# id niubi 
uid=1003(niubi) gid=1003(niubi) groups=1003(niubi)

受控主机也建立了新用户

(4)命令指定变量

[root@bastion ansible]# ansible-playbook test.yml -e "NAME=hahaha"

PLAY [test] ********************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************
ok: [172.25.250.12]
ok: [172.25.250.11]

TASK [create user] *************************************************************************************************************************
ok: [172.25.250.11] => {
    "NAME": "hahaha"
}
ok: [172.25.250.12] => {
    "NAME": "hahaha"
}

(5)使用数组定义变量

[root@bastion ansible]# cat shuzu.yml 
---
USERLIST: 
  chihao: 
    age: 18
    obj: linux

  peilin: 
    age: 15
    obj: java

编写一个数组
修改刚才的test

[root@bastion ansible]# cat test.yml 
---
- name: test
  hosts: prod
  vars_files: ./shuzu.yml
  tasks:
    - name: create user
      debug:
        var: USERLIST['chihao']['age']

    - debug:
        msg: "{{USERLIST['chihao']['obj']}}"

格式很重要
在这里插入图片描述在这里插入图片描述在此指定了变量的位置
2.变量注册

[root@westos Desktop]# TIME='date'
[root@westos Desktop]# echo $TIME
date
[root@westos Desktop]# 

原理

[root@bastion ansible]# vim test.yml 
[root@bastion ansible]# cat test.yml 
---
- name: prod
  hosts: prod
  tasks:
  - name:
    shell:
      date

受控主机会执行date命令,但是如何把date的内容捕捉起来
显示rc=0就是执行成功

---
- name: prod
  hosts: prod
  tasks:
  - name:
    shell:
      date
    register: CHIHAO
  - debug:
      var: CHIHAO['rc']
```powershell

- name: prod
  hosts: prod
  tasks:
  - name:
    shell:
      date
    register: CHIHAO
  - debug:
      var: CHIHAO['stdout']

显示具体时间
chihao是一个变量,变量里面又可以设置字典
register把模块输出注册到指定的字符串中
3.事实变量
事实变量是ansible在受控主机中自动检测出的变量
有与主机相关的信息

  480  ansible prod -m setup

ad-hoc可以通过这样的方式查看
要查看架构

[root@bastion ansible]# cat test.yml 
---
- name: prod
  hosts: prod
  tasks:
  - debug:
      var: ansible_facts['architecture']

查看IP

---
- name: prod
  hosts: prod
  tasks:
  - debug:
      var: ansible_facts['ens3']['ipv4']['address']

查看主机名

---
- name: prod
  hosts: prod
  tasks:
  - debug:
      var: ansible_facts['fqdn']

debug相当于echo,输入的变量都是setup里存在的
事实变量是真实存在的,不需要去定义变量
在这里插入图片描述这相当于setup
4.魔法变量
ansible软件的内部信息

[root@bastion ansible]# ansible localhost -m debug -a 'var=hostvars'

查看所有的ansible内部信息

[root@bastion ansible]# ansible prod -m debug -a 'var=group_names'
172.25.250.11 | SUCCESS => {
    "group_names": [
        "prod",
        "webservers"
    ]
}
172.25.250.12 | SUCCESS => {
    "group_names": [
        "prod",
        "webservers"
    ]
}

查看组信息

[root@bastion ansible]# ansible prod -m debug -a 'var=groups'

所有的组信息

```powershell
[root@bastion ansible]# ansible prod -m debug -a 'var=inventory_hostname'
172.25.250.11 | SUCCESS => {
    "inventory_hostname": "172.25.250.11"
}
172.25.250.12 | SUCCESS => {
    "inventory_hostname": "172.25.250.12"

清单配置中当前受管主机的名称
5.j2模块的使用
自动生成配置文件

[root@bastion ansible]# cat test.j2 
{# /etc/hosts line #}
127.0.0.1 localhost
{{ ansible_facts['ens3']['ipv4']['address']}} {{ansible_facts['fqdn']}}

j2文件的内容

---
- name: test j2
  hosts: prod
  tasks:
    - name: test j2
      template:
        src: ./test.j2
        dest: /mnt/hosts

在mnt/hosts会自动生成

[root@serverc mnt]# cat /mnt/hosts 
127.0.0.1 localhost
172.25.250.12 serverc.exam.com
[root@serverb mnt]# cat /mnt/hosts 
127.0.0.1 localhost
172.25.250.11 serverb.exam.com

为什么不直接写?使用这个方法,在哪个主机生成的名称都是不一样的,不是固定的
(1)for 语句

[root@bastion ansible]# cat test.j2 
{% for NAME in users %}
{{NAME}}
{% endfor %}
[root@bastion ansible]# cat chihao.yml 
---
- name: test j2
  hosts: prod
  vars: 
    users:
      - chihao
      - peilin
      - gou
  tasks:
    - name: test j2
      template:
        src: ./test.j2
        dest: /mnt/hosts

在这里插入图片描述变量在此
(2)判定语句if
如果不是A,就显示B,是A就显示C

[root@bastion ansible]# cat test.j2 
{% for NAME in users if not NAME== "gou" %}
{{loop.index0}}-{{NAME}}
{% endfor %}

0表示从0开始计数

[root@bastion ansible]# cat chihao.yml 
---
- name: test j2
  hosts: prod
  vars: 
    users:
      - chihao
      - peilin
      - gou
  tasks:
    - name: test j2
      template:
        src: ./test.j2
        dest: /mnt/hosts

最后结果

[root@serverc mnt]# cat /mnt/hosts 
0-chihao
1-peilin

如果名字为gou就停止循环计数,不是gou就从0开始计数

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值