ansible中变量及加密

变量设定和使用方式

1.在playbook中定义变量

vim test.yml
编辑内容:
  -  name: test play
      hosts: all 
      vars:
        USER: westosuser
ansible-playbook test.yml 

2.在文件中定义变量

vim user_list.yml
编辑内容:
USER: westoslinux

vim test.yml 
写入:
- name: test var
  hosts: all
  vars_files:
    user_list.yml
  tasks:
  - name: create file
    user:
     name: "{{ USER }}"

ansible-playbook test.yml 

3.使用数组设定变量

vim user_list.yml
  1 userlist:
  2   username: user1
  3   uid: 1111
  4   shell: /bin/bash
vim westos.yml
写入:
  1 - name: test play
  2   hosts: all
  3   vars_files: ./user_list.yml
  4   tasks:
  5     - name: create file
  6       debug:
  7         msg: "{{userlist['username']}} {{userlist['uid']}} {{userlist['shell    ']}}"
    ansible-playbook westos.yml

4.用命令覆盖变量

vim westos.yml
写入:
 1 - name: test play
  2   hosts: all
  3   vars:
  4     name: hello
  5   tasks:
  6     - name: create file
  7       debug:
  8         msg: "{{name}}"
ansible-playbook westos.yml "name=haha"                          

vim westos.yml
写入:
 - name: test play
    hosts: all
    tasks:
      -  name: create file
          file:
            path: "/mnt/{{item['name']}}"
            mode: "{{item.mode}}"
            state: touch
         with_items:
           -  name: file1
               mode:777
           -  name: file2
               mode: 666
           -  name: file3
               mode: 755
ansible-playbook westos.yml

5.注册变量

vim  westos.yml
写入:
  1 - name: test play
  2   hosts: all
  3   vars:
  4     name: hello
  5   tasks:
  6     - name: checkfile
  7       shell: id linuxrui
  8       ignore_errors: yes
  9       register: output
 10 
 11     - name: show messages
 12       debug:
 13         msg: "{{output['stdout_lines']}}"
ansible-paybook westos.yml     
vim westos.yml
写入:
  1 - name: test play
  2   hosts: all
  3   tasks:
  4     - name: checkfile
  5       shell: test -e /mnt/westoslinux
  6       ignore_errors: yes
  7       register: output
  8 
  9     - name: show messages
 10       debug:
 11         msg: "{{output['rc']}}"    
output['rc']  = output.rc
ansible-playbook westos.yml                      

6. 事实变量

vim host.yml
写入:
1 - name: test play
  2   hosts: all
  3   tasks:
  4     - name: show messages
  5       debug:
  6         msg: "{{ansible_facts['fqdn']}} - {{ansible_facts['ens160']['ipv4'][    'address']}}"
ansible-playbook hist.yml

7. 魔法变量

   hostvars: ##ansible软件内部信息
#eg:
ansible localhost -m debug -m "var=hostvars"
group_names: ##当前受管主机所在组
#eg:
ansible localhost -m debug -m "var=group_names"
groups: ##列出清单中所有的组和主机
#eg:
ansible localhost -m debug -m "var=groups"
inventory_hostname: ##包含清单中配置的当前授管主机的名称
#eg:
ansible localhost -m debug -m "var=inventory_hostname"

8.j2文件模板书写规则:

for循环

vim users.yml
users:
 - westos
- linux
 - ansible
vim test.j2
{% for NAME in users %}
{{ NAME }}
{%endfor%}
vim westos.yml
  1 - name: test play
  2   hosts: all
  3   vars_files: ./users.yml
  4   tasks:
  5     - name: create hosts
  6       template:
  7         src: ./test.j2
  8         dest: /mnt/hosts
ansible-playbook westos.yml
ansible all -m shell -a "cat /mnt/hosts"

if 判定

{% for NAME in users if not NAME == "ansible" %}
User number {{loop.index}} - {{ NAME }}
{%endfor%}

{% for NAME in users %}
{% for NAME == linux %}
luckhosts
{%endif%}
{%if not NAME == linux %}
{{NAME}}
{%endif%}
{%endfor%} 


if not NAME == "ansible" %}
User number {{loop.index}} - {{ NAME }}
{%endfor%}
loop.index ##循环迭代记数从1开始
loop.index0 ##循环迭代计数从0开始                     


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值