• Linux之ansible(playbook)超详解


    目录

    1.编写Playbook:具备两个play, 每个Play具备两个任务

    2.在play中定义变量: play_var = students10 -> 使用debug模块输出

    3.在文件中定义变量: file_var = file_var -> 使用debug模块输出

    在清单文件中定义主机变量和主机组变量:4.inventory_host_var -> 使用debug模块输出

    5.inventory_group_var -> 使用debug模块输出

    6.在host_vars和group_vars中定义文件:在文件中定义变量使用debug模块输出要求定义单个变量,数组变量,和字典变量

    7.vault加密:创建加密文件(注意提供密码的方式:键盘输入,文件读取)

    8.加密已有文件

    9.解密文件

    10.使用--vault-id选项来对已有文件进行加密

    事实:facts11.使用debug模块输出:收集事实的hostname, default_address, fqdn, kernel 

     hostname

     default_address

    fqdn 

     kernel 

    12.关闭事实:两种方式

     一​

     二

     13.针对单个play的关闭

    14.针对所有play的关闭

    15.自定义事实:使用两种方式:INI和json方式(注意格式)

    16.提供数据:需要自己组织成INI格式或json格式的数据

    student1: name: zhangsanage: 8agender: maleaddress: openlab.comstudent2:name: lisiage: 8agender: femaleaddress: openlab.com


    1.编写Playbook:具备两个play, 每个Play具备两个任务

    1. [root@rhcsa ~]# ansible-playbook playbook.yml
    2. PLAY [rhce] ****************************************************************************
    3. TASK [Gathering Facts] *****************************************************************
    4. ok: [rhce]
    5. TASK [httpd] ***************************************************************************
    6. changed: [rhce]
    7. TASK [firewalld] ***********************************************************************
    8. changed: [rhce]
    9. PLAY RECAP *****************************************************************************
    10. rhce : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

     

    1. [root@rhcsa ~]# ansible-playbook playbook.yml
    2. PLAY [rhce] ****************************************************************************
    3. TASK [Gathering Facts] *****************************************************************
    4. ok: [rhce]
    5. TASK [touch file] **********************************************************************
    6. changed: [rhce]
    7. TASK [user] ****************************************************************************
    8. changed: [rhce]
    9. PLAY RECAP *****************************************************************************
    10. rhce : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    2.在play中定义变量: play_var = students10 -> 使用debug模块输出

    1. [root@rhcsa ~]# ansible-playbook playbook.yml
    2. PLAY [rhce] ****************************************************************************
    3. TASK [Gathering Facts] *****************************************************************
    4. ok: [rhce]
    5. TASK [touch file] **********************************************************************
    6. changed: [rhce]
    7. TASK [user] ****************************************************************************
    8. changed: [rhce]
    9. TASK [debug] ***************************************************************************
    10. ok: [rhce] => {
    11. "msg": "Hello world!"
    12. }
    13. PLAY RECAP *****************************************************************************
    14. rhce : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    3.在文件中定义变量: file_var = file_var -> 使用debug模块输出

    1. [root@rhcsa ~]# echo "file_var: students12" > vars_files
    2. [root@rhcsa ~]# more vars_files
    3. students12

     

    1. [root@rhcsa ~]# ansible-playbook playbook.yml
    2. PLAY [rhce] ****************************************************************************
    3. TASK [Gathering Facts] *****************************************************************
    4. ok: [rhce]
    5. TASK [user] ****************************************************************************
    6. changed: [rhce]
    7. TASK [debug] ***************************************************************************
    8. ok: [rhce] => {
    9. "msg": "Hello world!"
    10. }
    11. PLAY RECAP *****************************************************************************
    12. rhce : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    在清单文件中定义主机变量和主机组变量:
    4.inventory_host_var -> 使用debug模块输出

    1. [root@rhcsa ~]# ansible-playbook playbook.yml -C
    2. PLAY [inventory_host_var] **************************************************************
    3. TASK [Gathering Facts] *****************************************************************
    4. ok: [rhce]
    5. TASK [debug] ***************************************************************************
    6. ok: [rhce] => {
    7. "msg": "Hello world!"
    8. }
    9. PLAY RECAP *****************************************************************************
    10. rhce : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    5.inventory_group_var -> 使用debug模块输出

    1. [root@rhcsa ~]# ansible-playbook playbook.yml -C
    2. PLAY [inventory_group_var1 inventory_group_var2] ***************************************
    3. TASK [Gathering Facts] *****************************************************************
    4. ok: [rhce]
    5. ok: [rhel]
    6. TASK [debug] ***************************************************************************
    7. ok: [rhce] => {
    8. "msg": "Hello world!"
    9. }
    10. ok: [rhel] => {
    11. "msg": "Hello world!"
    12. }
    13. PLAY RECAP *****************************************************************************
    14. rhce : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
    15. rhel : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    6.在host_vars和group_vars中定义文件:在文件中定义变量使用debug模块输出
    要求定义单个变量,数组变量,和字典变量


    7.vault加密:创建加密文件(注意提供密码的方式:键盘输入,文件读取)

    1. #键盘输入
    2. [root@rhcsa ~]# ansible-vault create file1.yml
    3. New Vault password:
    4. Confirm New Vault password:
    1. 文件读取
    2. [root@rhcsa ~]# echo "123456" > file2
    3. [root@rhcsa ~]# ansible-vault create --vault-id file2 file2.yml

    8.加密已有文件

    1. #从键盘输入密码
    2. [root@rhcsa ~]# ansible-vault encrypt test.txt
    3. New Vault password:
    4. Confirm New Vault password:
    5. Encryption successful
    1. #从已有文件中提取密码
    2. [root@rhcsa ~]# ansible-vault encrypt --vault-id file2 test2.txt
    3. Encryption successful

    9.解密文件

    1. #从键盘输入密码
    2. [root@rhcsa ~]# ansible-vault decrypt test.txt
    3. Vault password:
    4. Decryption successful
    1. #从已有文件中提取密码
    2. [root@rhcsa ~]# ansible-vault decrypt --vault-id file2 test2.txt
    3. Decryption successful

    10.使用--vault-id选项来对已有文件进行加密

    1. #从键盘输入密码
    2. [root@rhcsa ~]# ansible-vault --vault-id encrypt test.txt
    3. New Vault password:
    4. Confirm New Vault password:
    5. Encryption successful
    1. #从已有文件中提取密码
    2. [root@rhcsa ~]# ansible-vault encrypt --vault-id file2 test2.txt
    3. Encryption successful

    事实:facts
    11.使用debug模块输出:收集事实的hostname, default_address, fqdn, kernel 

     hostname

    1. [root@rhcsa ~]# ansible-playbook playbook.yml -C
    2. PLAY [Facts] ***************************************************************************
    3. TASK [Gathering Facts] *****************************************************************
    4. ok: [rhce]
    5. TASK [debug] ***************************************************************************
    6. ok: [rhce] => {
    7. "msg": "The hostname is rhce"
    8. }
    9. PLAY RECAP *****************************************************************************
    10. rhce : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

     default_address

    1. [root@rhcsa ~]# ansible-playbook playbook.yml -C
    2. PLAY [Facts] ***************************************************************************
    3. TASK [Gathering Facts] *****************************************************************
    4. ok: [rhce]
    5. TASK [debug] ***************************************************************************
    6. ok: [rhce] => {
    7. "msg": "The default_address is 192.168.40.131"
    8. }
    9. PLAY RECAP *****************************************************************************
    10. rhce : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    fqdn 

    1. [root@rhcsa ~]# ansible-playbook playbook.yml -C
    2. PLAY [Facts] ***************************************************************************
    3. TASK [Gathering Facts] *****************************************************************
    4. ok: [rhce]
    5. TASK [debug] ***************************************************************************
    6. ok: [rhce] => {
    7. "msg": "The fqdn is rhce"
    8. }
    9. PLAY RECAP *****************************************************************************
    10. rhce : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

     kernel 

    1. [root@rhcsa ~]# ansible-playbook playbook.yml -C
    2. PLAY [Facts] ***************************************************************************
    3. TASK [Gathering Facts] *****************************************************************
    4. ok: [rhce]
    5. TASK [debug] ***************************************************************************
    6. ok: [rhce] => {
    7. "msg": "The kernel is 4.18.0-348.el8.x86_64"
    8. }
    9. PLAY RECAP *****************************************************************************
    10. rhce : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

    12.关闭事实:两种方式

     一

    1. [root@rhcsa ~]# ansible-playbook playbook.yml -C
    2. PLAY [This play gathers no facts automatically] ****************************************
    3. PLAY RECAP *****************************************************************************

     二

    可以在 ansible.cfg 中添加如下配置:
    [defaults]
    gathering = explicit (明确的)
    ansible 的配置文件中可以修改'gathering'的值为 smart、 implicit 或者 explicit
    (1)smart 表示默认收集 facts,但 facts 已有的情况下不会收集,即使用缓存 facts
    (2)implicit 表示默认收集 facts 
    (3)explicit 则表示默认不收集

     13.针对单个play的关闭

    1. # vim test.yml
    2. 创建以下内容:
    3. ---
    4. - name: test
    5. hosts: all
    6. gather_facts: false
    7. tasks:
    8. ......

    14.针对所有play的关闭

    1. [root@rhcsa ~]# ansible-playbook playbook.yml -C
    2. PLAY [This play gathers no facts automatically] ****************************************
    3. PLAY RECAP *****************************************************************************

    15.自定义事实:使用两种方式:INI和json方式(注意格式)

    1. ##INI格式
    2. [packages]
    3. web_pkg = httpd
    4. db_pkg = mariadb-server
    5. [users]
    6. user1 = rhce
    7. user2 = rhel
    8. ##JSON格式
    9. {
    10. "packages":{
    11. "web_packages":"httpd",
    12. "db_packages":"mariadb"
    13. },
    14. "users":{
    15. "user1":"rhce",
    16. "user2":"rhel"
    17. }
    18. }

    16.提供数据:需要自己组织成INI格式或json格式的数据

    student1: 
    name: zhangsan
    age: 8
    agender: male
    address: openlab.com
    student2:
    name: lisi
    age: 8
    agender: female
    address: openlab.com

    默认情况下 setup 模块从受管主机的/etc/ansible/facts.d 目录下的文件和脚本中加
    载自定义事实。各个文件名必须以.fact 结尾才能使用。动态自定义事实脚本必须
    输出 JSON 格式的事实,而且必须是可执行文件。
    INI 和 JSON 格式编写的静态自定义事实文件。 INI 格式的自定义事实文件包含由一
    部分定义的顶层值,后跟用于待定义事实的键值对。 

    1. ##INI
    2. [packages]
    3. name: zhangsan
    4. age: 8
    5. agender: male
    6. [users]
    7. user1 = rhce
    8. user2 = rhel
    9. ##json
    10. {
    11. "packages":{
    12. "name: lisi",
    13. "age: 8",
    14. "agender: female",
    15. "address: openlab.com"
    16. },
    17. "users":{
    18. "users1":"rhce",
    19. "users2":"rhel"
    20. }
    21. }

  • 相关阅读:
    jar包导入到本地仓库无法引用
    FastGpt流程
    零基础HTML入门教程(15)——合并单元格
    PCB叠层设计
    【ES】笔记-Map介绍与API
    hive分桶分区表和内部外部表
    Gradle 笔记 2
    部署zookeeper+kafka
    真香!阿里最新公开的200页Spring全家桶进阶指南及视频汇总
    网络——IPv6(一)
  • 原文地址:https://blog.csdn.net/weixin_64051859/article/details/126197939