Ansible-playbook变量学习

1.命令行变量赋值

#编写playbook  变量名为rpm,格式{{rpm}}
vim app.yml
---
- hosts: web
  tasks:
    - name: install app
      yum: name="{{rpm}}"
    - name: start app
      service:
        name: "{{rpm}}"
        state: started
        enabled: yes

给rpm变量定义为vsftpd

[root@tdm1 playbook]# ansible-playbook app.yml  -e 'rpm=vsftpd'

PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [47.93.98.117]

TASK [install app] *****************************************************************************************************changed: [47.93.98.117]

TASK [start app] *******************************************************************************************************changed: [47.93.98.117]

PLAY RECAP *************************************************************************************************************47.93.98.117               : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@tdm1 playbook]# ansible web -m shell -a 'ps -ef | grep vsftpd'
47.93.98.117 | CHANGED | rc=0 >>
root     15665     1  0 16:59 ?        00:00:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
root     15880 15879  0 16:59 pts/1    00:00:00 /bin/sh -c ps -ef | grep vsftpd
root     15883 15880  0 16:59 pts/1    00:00:00 grep vsftpd

在这里插入图片描述

2.在playbook中赋值变量

#编写playbook,在剧本中定义变量
vim app.yml
---
- hosts: web
  vars:
    - rpm1: httpd
    - rpm2: vsftpd
  tasks:
    - name: install app
      yum: name="{{rpm1}}"
    - name: install app
      yum: name="{{rpm2}}"                                
[root@tdm1 playbook]# ansible-playbook app.yml 

PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [47.93.98.117]

TASK [install app] *****************************************************************************************************changed: [47.93.98.117]

TASK [install app] *****************************************************************************************************changed: [47.93.98.117]

PLAY RECAP *************************************************************************************************************47.93.98.117               : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
[root@tdm1 playbook]# ansible web -m shell  -a 'rpm -q httpd vsftpd'
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'.  If you need to use command because
yum, dnf or zypper 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.
47.93.98.117 | CHANGED | rc=0 >>
httpd-2.4.6-99.el7.centos.1.x86_64
vsftpd-3.0.2-29.el7_9.x86_64

在这里插入图片描述

3.在ansible的清单文件(/etc/ansible/hosts)中定义普通变量

vim /etc/ansible/hosts
[web]
47.93.98.117 rpm1=httpd rpm2=vsftpd

vim app.yml
---
- hosts: web
  tasks:
    - name: install app
      yum: name="{{rpm1}}"
    - name: install app
      yum: name="{{rpm2}}"
[root@tdm1 playbook]# ansible-playbook app.yml 

PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [47.93.98.117]

TASK [install app] *****************************************************************************************************changed: [47.93.98.117]

TASK [install app] *****************************************************************************************************changed: [47.93.98.117]

PLAY RECAP *************************************************************************************************************47.93.98.117               : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

在这里插入图片描述

4.在ansible的清单文件(/etc/ansible/hosts)中定义分组变量

vim /etc/ansible/hosts
[web]
47.93.98.117

[web:vars]
rpm1=httpd
rpm2=vsftpd

vim app.yml
---
- hosts: web
  tasks:
    - name: install app
      yum: name="{{rpm1}}"
    - name: install app
      yum: name="{{rpm2}}"
[root@tdm1 playbook]# ansible-playbook  app.yml 

PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [47.93.98.117]

TASK [install app] *****************************************************************************************************changed: [47.93.98.117]

TASK [install app] *****************************************************************************************************changed: [47.93.98.117]

PLAY RECAP *************************************************************************************************************47.93.98.117               : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

在这里插入图片描述

5.定义变量到一个文件中

vim var.yml    #变量放在该文件中
rpm1: httpd
rpm2: vsftpd

vim app.yml
---
- hosts: web
  vars_files:
    - var.yml     #变量文件,路径写清楚,我存放在了当前路径
  tasks:
    - name: install app
      yum: name="{{rpm1}}"
    - name: install app
      yum: name="{{rpm2}}"
[root@tdm1 playbook]# ansible-playbook app.yml 

PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [47.93.98.117]

TASK [install app] *****************************************************************************************************changed: [47.93.98.117]

TASK [install app] *****************************************************************************************************changed: [47.93.98.117]

PLAY RECAP *************************************************************************************************************47.93.98.117               : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@tdm1 playbook]# ansible web -m shell -a 'rpm -q httpd vsftpd'
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'.  If you need to use command because
yum, dnf or zypper 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.
47.93.98.117 | CHANGED | rc=0 >>
httpd-2.4.6-99.el7.centos.1.x86_64
vsftpd-3.0.2-29.el7_9.x86_64

在这里插入图片描述

6.在执行playbook的目录创建 group_vars host_vars

mkdir group_vars
cd group_vars
vim  web   #创捷和hosts文件下和组一样的文件
rpm1: htpd
rpm2: vsftpd

#创建与组同名的文件
[root@tdm1 playbook]# cat /etc/ansible/hosts 
[web]
47.93.98.117 

在这里插入图片描述

vim app.yml
[root@tdm1 playbook]# cat  app.yml 
---
- hosts: web
  tasks: 
    - name: install app
      yum: name="{{rpm1}}"
    - name: install app
      yum: name="{{rpm2}}"
[root@tdm1 playbook]# ansible-playbook app.yml 

PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [47.93.98.117]

TASK [install app] *****************************************************************************************************changed: [47.93.98.117]

TASK [install app] *****************************************************************************************************changed: [47.93.98.117]

PLAY RECAP *************************************************************************************************************47.93.98.117               : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@tdm1 playbook]# ansible web -m shell -a 'rpm -q httpd vsftpd'
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'.  If you need to use command because
yum, dnf or zypper 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.
47.93.98.117 | CHANGED | rc=0 >>
httpd-2.4.6-99.el7.centos.1.x86_64
vsftpd-3.0.2-29.el7_9.x86_64

cclear

7.注册变量

注册变量就是将操作结果,包括标准输出和标准错误输出,保存到变量中,然后再根据这个变量的内容来决定下一步的操作,在这个过程中用来保存操作结果的变量叫注册变量

vim test.yml
---
- hosts: web
  tasks:
    - name: test register variables
      shell: uptime
      register: results

    - name: print the register result
      debug: msg="{{ results }}"                       
[root@tdm1 playbook]# ansible-playbook test.yml  

PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [47.93.98.117]

TASK [test register variables] *****************************************************************************************changed: [47.93.98.117]

TASK [print the register result] ***************************************************************************************ok: [47.93.98.117] => {
    "msg": {
        "changed": true, 
        "cmd": "uptime", 
        "delta": "0:00:00.039581", 
        "end": "2023-08-31 19:54:38.670898", 
        "failed": false, 
        "rc": 0, 
        "start": "2023-08-31 19:54:38.631317", 
        "stderr": "", 
        "stderr_lines": [], 
        "stdout": " 19:54:38 up 8 days, 11:29,  2 users,  load average: 0.00, 0.02, 0.05", 
        "stdout_lines": [
            " 19:54:38 up 8 days, 11:29,  2 users,  load average: 0.00, 0.02, 0.05"
        ]
    }
}

PLAY RECAP *************************************************************************************************************47.93.98.117               : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

在这里插入图片描述
changed:任务是否对远程主机造成的变更。
delta:任务运行所用的时间。
stdout:正常的输出信息。
stderr:错误信息。
rc:返回值

- hosts: web
  tasks:
    - name: test register variables
      shell: uptime
      register: results

    - name: print the register result
      debug: msg="{{ results.stdout }}"

[root@tdm1 playbook]# ansible-playbook  test.yml 

PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [47.93.98.117]

TASK [test register variables] *****************************************************************************************changed: [47.93.98.117]

TASK [print the register result] ***************************************************************************************ok: [47.93.98.117] => {
    "msg": " 19:55:57 up 8 days, 11:30,  2 users,  load average: 0.00, 0.02, 0.05"
}

PLAY RECAP *************************************************************************************************************47.93.98.117               : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值