shc         #加密shell,需要自己下载包

1.变量命名

只能包含数字,下划线,字母
只能用下划线或字母开头

2.变量级别

全局:    从命令行或配置文件中设定的
play:     在play和相关结构中设定的
主机:    由清单,事实收集或注册的任务

变量优先级设定:
狭窄范围有限与广域范围

3.变量设定和使用方式

3.1.在playbook中直接定义变量

---
- name: Create User
  hosts: westos1
  vars:
    USER: westosuser

ansible剧本中with_items引用变量 ansible 变量赋值_vim

 

ansible剧本中with_items引用变量 ansible 变量赋值_linux_02


3.2.在文件中定义变量和使用变量

vim westos.yml
---
user:
  - lee2
  - lee3

vim test.yml
---
- name: test
  hosts: westos
  vars_files:
    - ./westos.yml

  tasks:
    - name: user
      user:
        name: "{{ item }}"
        state: present

ansible剧本中with_items引用变量 ansible 变量赋值_文件名_03

 

ansible剧本中with_items引用变量 ansible 变量赋值_linux_04

 

ansible剧本中with_items引用变量 ansible 变量赋值_linux_05

 

ansible剧本中with_items引用变量 ansible 变量赋值_运维_06

4.设定主机变量和清单变量

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

[westos1]
172.25.21.20

[westos2]
172.25.21.30

[westos3]
172.25.21.40

[westos12]               
172.25.21.20
172.25.21.30

[westos12:vars]        #westos12清单中的所有用户的USER变量为yjy3
USER=yjy3


 

ansible剧本中with_items引用变量 ansible 变量赋值_文件名_07

 

ansible剧本中with_items引用变量 ansible 变量赋值_文件名_08


ansible剧本中with_items引用变量 ansible 变量赋值_vim_09


5.目录设定变量

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

mkdir group_vars
vim group_vars/westos
---
USER: test1

ansible剧本中with_items引用变量 ansible 变量赋值_linux_10

 

ansible剧本中with_items引用变量 ansible 变量赋值_文件名_11


mkdir host_vars
vim host_vars/172.25.254.221    #指定221的变量
---
USER: test2

ansible剧本中with_items引用变量 ansible 变量赋值_vim_12


6.用命令覆盖变量

ansible-playbook test3.yml -e "USER=hello"     #优先级最高

ansible剧本中with_items引用变量 ansible 变量赋值_vim_13


ansible剧本中with_items引用变量 ansible 变量赋值_vim_14


7.使用数组设定变量

#vim name1.yml
---
USER:
  yjy:
    age: 18
    obj: linux
 
  westos:
    age: 20
    obj: java

#vim user.yml
- name: Create User
  hosts: westos1
  vars_files:
    ./name1.yml

  tasks:
    - name: create user
      debug:
        msg:

            - "{{USER['yjy']['age']}}"
            - "{{USER['westos']}}"

            - "{{USER}}"

ansible剧本中with_items引用变量 ansible 变量赋值_linux_15

 

ansible剧本中with_items引用变量 ansible 变量赋值_vim_16

 

ansible剧本中with_items引用变量 ansible 变量赋值_运维_17


8.注册变量

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

---
- name: test
  hosts: westos1
  tasks:
    - name: shell
      shell: test -e /mnt/hello
      register: westos

      ignore_errors: yes

    - debug:
         msg: "{{westos.rc}}"

ansible剧本中with_items引用变量 ansible 变量赋值_运维_18


ansible剧本中with_items引用变量 ansible 变量赋值_文件名_19


9.事实变量

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

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

ansible  all -m setup        #查看所有事实变量

---
- name: do register
  hosts: westos1
  tasks:
    - name: show messages
      debug:
        msg: "{{ansible_facts['architecture']}}"

ansible剧本中with_items引用变量 ansible 变量赋值_linux_20


gather_facts: no     #在playbook中关闭事实变量收集


10.魔法变量

hostvars:        #ansible软件的内部信息
#eg:
ansible localhost -m debug -a "var=hostvars"

ansible剧本中with_items引用变量 ansible 变量赋值_linux_21


group_names:        ##当前受管主机所在组


#eg:


ansible localhost -m debug -a "var=group_names"

 

ansible剧本中with_items引用变量 ansible 变量赋值_文件名_22


groups:            ##列出清单中所有的组和主机
#eg:
ansible localhost -m debug -a "var=groups"

ansible剧本中with_items引用变量 ansible 变量赋值_vim_23


inventory_hostname:    ##包含清单中配置的当前授管主机的名称    
#eg:
ansible localhost -m debug -a "var=inventory_hostname"

ansible剧本中with_items引用变量 ansible 变量赋值_文件名_24


11.JINJA2的使用


11.1 JINJA2模板

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

11.2 j2模板书写规则

可以使用变量,copy不能使用变量
{# /etc/hosts line #}        #注释说明文件用途
127.0.0.1    localhost     #文件内容
{{ ansible_facts['all_ipv4_addresses'] }}    {{ansible_facts['fqdn']}}    #使用事实变量

#for循环#

vim users.yml

- name: host j2 hosts: westos tasks: - name: host template: src: ./host.j2 dest: /mnt/hosts

vim hosts.j2
{% for WESTOS in group['all']s %}
{{ hostvars[WESTOS]['ansible_facts']['ens3']['ipv4']['address'] }}{{hostvars[WESTOS]['ansible_facts']['fqdn']}}{{hostvars[WESTOS]['ansible_facts']['hostname']}}
{%endfor%}

ansible剧本中with_items引用变量 ansible 变量赋值_linux_25

ansible剧本中with_items引用变量 ansible 变量赋值_运维_26

#if 判定#

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

{% for NAME in users %}
{%if NAME in "linux"%}
{{loop.index0}}
{%endif%}
{%if NAME not in "linux"%}
{{loop.index0}}{{NAME}}
{%endif%}
{%endfor%}

ansible剧本中with_items引用变量 ansible 变量赋值_运维_27

 

ansible剧本中with_items引用变量 ansible 变量赋值_文件名_28

 

ansible剧本中with_items引用变量 ansible 变量赋值_linux_29


11.3 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.21.20
  vars:
    students:
      - name: student1
        obj: linux

      - name: student2
        age: 18
        obj: linux
 
  tasks:
    - template:
        src: ./test.j2
        dest: /mnt/list

12. Ansible的加密控制

创建加密文件

1.
ansible-vault create westos

2.
vim westos-vault
lee

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

ansible剧本中with_items引用变量 ansible 变量赋值_文件名_30


加密现有文件

ansible-vault encrypt test

ansible剧本中with_items引用变量 ansible 变量赋值_运维_31


查看加密文件内容

ansible-vault view westos
ansible-vault view --vault-password-file=westos-valut westos

ansible剧本中with_items引用变量 ansible 变量赋值_运维_32


编辑加密文件

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

ansible剧本中with_items引用变量 ansible 变量赋值_文件名_33


解密文件

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

ansible剧本中with_items引用变量 ansible 变量赋值_文件名_34

 

ansible剧本中with_items引用变量 ansible 变量赋值_vim_35


更改密码

ansible-vault rekey westos1
ansible-vault rekey westos1 --new-vault-password-file=key1

ansible剧本中with_items引用变量 ansible 变量赋值_vim_36

playbook

ansible-playbook apache_install.yml  --ask-vault-pass