Linux 第五本书 第四单元--------Ansible中的变量及加密

本文详细介绍了Ansible在配置管理、变量使用、模板渲染、文件操作、加密及自动化运维等方面的应用。通过实例展示了如何定义和使用变量、创建用户、配置虚拟主机、加密文件,并实现自动化任务。此外,还涵盖了Ansible的事实变量、注册变量以及如何与其他工具如firewalld和httpd配合使用。
摘要由CSDN通过智能技术生成

变量

 

vim file.yml


[
- name: test
  hosts: westos
  vars:     ----定义变量
    NAME: westos                 定义变量NAME为westos
  tasks:
   - name: debug
     debug:
       msg: "{{  NAME }}"
   - name: create file             
     file:
       path: "/mnt/{{NAME}}"
       state: touch

   - name: create file
     file:
       path: "/mnt/{{item}}"      使用loop循环必须要用item
       state: touch
     loop:
      - file1
      - file2
      - file3

   - name: copy
     copy:
       dest: /mnt/testfile
       content: "{{ansible_facts['fqdn']}}"    把用户名称打入testfile
]

 

===========================================================

 

vim userlist.yml
[
- name: create user
  hosts: westos
  vars:
    USER1:       ----定义变量USER1
      NAME: user1  ---赋予变量详细附属信息
      UID: 678
  tasks:
   - name: create user1
     user:
       name: "{{USER1.NAME}}"  ----变量的使用
       uid: "{{USER1.UID}}"
]

使用文件形式
vim user_list.yml   ----  在文件中定义变量
[
---
USER1:
  NAME: user1
  UID: 666

]

 


vim userlist.yml
[
- name: create user
  hosts: westos
  vars_files:
    - ./user_list.yml     ----当前目录的user_list.yml,使用此文件
  tasks:
   - name: create user1
     user:
       name: "{{USER1.NAME}}"
       uid: "{{USER1.UID}}"

]

====================================================

设定清单变量

vim inventory   
[
[westos:vars]              ------编辑清单里的变量
WESTOS=who are you
]


vim list.yml
[
- name: test
  hosts: westos
  tasks:
   - debug:
       msg: "{{WESTOS}}"           ----引用清单里的变量
]

 

================================================

注册变量
vim www.yml
- name: www
  hosts: westos
  tasks:
      - shell: test -e /mnt/file1
        register: out
      - debug:
          msg: "{{out.rc}}"
      

 

                       

事实变量
- name: www
  hosts: westos
  tasks:
      - debug:
          msg: "{{ansible_facts['fqdn']}}"

 

 

========================================================================
j2

能抓取所有主控机和受控机的ip
先在受控机建立westos

 

主控机:
vim westos.yml
[
- name: test
  hosts: all
  tasks:
     - name: host j2
       template:
         src: ./hosts.j2
         dest: /mnt/westos

]

vim  hosts.j2
[
{%for HOST in groups['all']%}
{{HOST}} {{hostvars[HOST]['ansible_facts']['fqdn']}}
{%endfor%}
         
]

 


练习,分别在网页输入www.westos.com, linux.westos.com, news.westos.com,最终显示其名

 vim http.yml
WEBS:
    - DOC: /var/www/html
      INDEX: /var/www/html/index.html
      INDEX_TEST: www.westos.org
    - NAME: linux.westos.org
      DOC: /var/www/virtual/westos.com/linux
      INDEX: /var/www/virtual/westos.com/linux/index.html
      INDEX_TEST: linux.westos.org
    - NAME: news.westos.org
      DOC: /var/www/virtual/westos.com/news
      INDEX: /var/www/virtual/westos.com/news/index.html
      INDEX_TEST: news.westos.org


vim http.j2

{% for WEB in WEBS%}
{% if WEB['NAME'] is not defined %}
<VirtualHost _default_:80>
{% endif %}
{% if WEB['NAME'] is defined%}
 <VirtualHost *:80>
 ServerName  {{ WEB['NAME']}}
{% endif %}
 DocumentRoot  "{{ WEB['DOC'] }}"
 </VirtualHost>
{% endfor %}

 

 

vim web.yml

- name: web
  hosts: westos
  vars_files: ./http.yml
  tasks:
      - name: install apache
        dnf:
          name: httpd
          state: present

      - name: check_file
        file:
          path: /etc/httpd/conf.d/vhosts.conf
          state: absent
      - name: firewalld
        firewalld:
          service: http
          permanent: yes
          state: enabled
          immediate: yes
      - name: start apache
        service:
          name: httpd
          state: restarted
          enabled: yes
      - name: create
        file:
           path: "{{item.DOC}}"
           state: directory
        loop:
            "{{WEBS}}"
      - name: configure
        template:
              src: ./http.j2
              dest: /mnt/http
      - name: index
        copy:
          content: "{{item.INDEX_TEST}}"
          dest: "{{item.INDEX}}"
        loop:
             "{{WEBS}}"
                               
=================================================

加密
ansible-vault encrypt westos.yml   -------给westos加密


 
cat westos.yml          ----查看只会发现加密码

 

 ansible-vault view  westos.yml     查看需要输入密码

 ansible-vault edit  westos.yml     ----编辑westos,输入密码
 
vim pass   ----为了方便,提前编写一个文件写入密码

westos

 ansible-vault view  westos.yml --vault-password-file=pass
利用文件里的密码直接,不用登陆直接查看westos

 

ansible-vault rekey  westos.yml --vault-password-file=pass
修改密码,注意:修改完密码后,原先编辑的密码文件需要更改为修改后的密码

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值