Ansible中的变量及加密_ansible加密解密vars(1)

  • name: Create User
    hosts: all
    vars_files:
    ./user_list.yml
    tasks:
    • name: create user
      user:
      name: “{{USER}}”
      state: absent


#3.使用变量#

tasks:
- name: create user
user:
name: “{{ USER }}”



#4.设定主机变量和清单变量#
#在定义主机变量和清单变量时使用

vim inventory
[list1]
172.25.1.10
[list2]
172.25.1.20
[list3]
172.25.1.10
172.25.1.20
[list1:vars]
USER=westosuser

vim test.yml

  • name: test var
    hosts: list1
    tasks:
    • name: create user
      user:
      name: “{{USER}}”
      state: present


#5.目录设定变量#
group_vars ##清单变量,目录中的文件名称与主机清单名称一致
host_vars ##主机变量,目录中的文件名称与主机名称一致


![](https://img-blog.csdnimg.cn/20201221114155834.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)


![](https://img-blog.csdnimg.cn/20201221113921304.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)


![](https://img-blog.csdnimg.cn/20201221114129858.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)



#6.用命令覆盖变量#
ansible-playbook user.yml -e “USER=hello”



#7.使用数组设定变量#
#vim user_var.yml

USER:
lee:
age: 18
obj: linux

westos:
age: 20
obj: java

#vim user.yml

  • name: Create User
    hosts: all
    gather_facts: no
    vars_files:
    ./user_var.yml

    tasks:

    • name: create user
      shell:
      echo “{{USER[‘lee’][‘age’]}}”
      echo “{{USER.westos.obj}}”


#8.注册变量#
#register 把模块输出注册到指定字符串中


  • name: test register
    hosts: list1
    tasks:
    • name: hostname command
      shell:
      hostname
      register: info
    • name: show messages
      debug:
      msg: {{info[‘stdout’]}}"

![](https://img-blog.csdnimg.cn/20201221115924190.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)



#9.事实变量#
事实变量是ansible在受控主机中自动检测出的变量
事实变量中还有与主机相关的信息

当需要使用主机相关信息时不需要采集赋值,直接调用即可
因为变量信息为系统信息所以不能随意设定仅为采集信息,故被成为事实变量


  • name: test
    hosts: list1
    tasks:
    • name: show
      debug:
      msg: “{{ansible_facts[‘fqdn’]}}”

![](https://img-blog.csdnimg.cn/20201221120256117.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)


 练习脚本:


采集主机的ip 和主机名并以


hostname:


ip:


形式输出到/etc/motd




  • name: test
    hosts: list1
    tasks:
    • name: info
      copy:
      content: “hostname: {{ansible_facts[‘fqdn’]}}\nip: {{ansible_facts[‘enp1s0’][‘ipv4’][‘address’]}}\n”
      dest: /etc/motd

**测试:**


![](https://img-blog.csdnimg.cn/20201221121119673.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)


![](https://img-blog.csdnimg.cn/20201221121132505.png)



#10.魔法便变量#
hostvars: ##ansible软件的内部信息

group_names: ##当前受管主机所在组
groups: ##列出清单中所有的组和主机
inventory_hostname: ##包含清单中配置的当前授管主机的名称


####  四、JINJA2模板


#介绍  
 Jinja2是Python下一个被广泛应用的模版引擎  
 他的设计思想来源于Django的模板引擎,  
 并扩展了其语法和一系列强大的功能。  
 其中最显著的一个是增加了沙箱执行功能和可选的自动转义功能


#### 五、j2模板书写规则



{# /etc/hosts line #}
127.0.0.1 localhost
{{ ansible_facts[‘all_ipv4_addresses’] }} {{ansible_facts[‘fqdn’]}}

#for循环#
vim users.yml
users:

  • westos
  • linux
  • ansible

vim test.j2
{% for NAME in users %}
{{ NAME }}
{%endfor%}

#if 判定#
{% for NAME in users if not NAME == “ansible” %}
User number {{loop.index}} - {{ NAME }}
{%endfor%}

loop.index ##循环迭代记数从1开始
loop.index0 ##循环迭代计数从0开始

{% for user in students %}
name: {{user[‘name’]}}
{%if user[‘age’] is defined%}
age: {{user[‘age’]}}
{%endif%}
{% if user[‘age’] is not defined %}
age: null
{% endif%}
obj: {{user[‘obj’]}}
{%endfor%}



#j2模板在playbook中的应用#
#playbook1

  • name: test register
    hosts: xxxx
    tasks:
    • name: create hosts
      template:
      src: ./xxxx.j2
      dest: /mnt/hosts

#playbook2

  • name: test.j2
    hosts: 172.25.0.254
    vars:
    students:
    - name: student1
    obj: linux

    - name: student2
      age: 18
      obj: linux
    

    tasks:

    • template:
      src: ./test.j2
      dest: /mnt/list

练习脚本  
 create web vhost  
 www.westos.com  80    ------ > /var/www/html


linux.westos.com 80 ------> /var/www/virtual/westos.com/linux


 


#### 六、Ansible的加密控制



#创建建立文件
1.
ansible-vault create westos

vim westos-vault
123456

ansible-vault create --vault-password-file=westos-vault westos

#加密现有文件
ansible-vault encrypt test

#查看加密文件
ansible-vault view westos
ansible-vault view --vault-password-file=westos-valut westos

#编辑加密文件

ansible-vault edit westos1
ansible-vault edit --vault-password-file=westos-valut westos

##解密文件
ansible-vault decrypt westos ##文件永久解密
ansible-vault decrypt westos --output=linux ##文件解密保存为linux

##更改密码
ansible-vault rekey westos1
ansible-vault rekey westos1 --new-vault-password-file=key1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值