ansible 002 连接被控端 inventory ansible.cfg ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型

ssh用普通用户连接被控端

配置主机清单 (/etc/hosts域名解析为前提)

[root@workstation ansible]# cat hosts
servera
serverb
[root@workstation ansible]# pwd
/etc/ansible
[root@workstation ansible]#

建立免密

[root@workstation ansible]# ssh-keygen

使被控端创建用户

[root@workstation ansible]# ansible all -m shell -a 'useradd ansible' -k
SSH password:
servera | CHANGED | rc=0 >>

serverb | CHANGED | rc=0 >>

[root@workstation ansible]# ansible all -m shell -a 'echo redhat | passwd --stdin ansible' -k
SSH password:
serverb | CHANGED | rc=0 >>
Changing password for user ansible.

passwd: all authentication tokens updated successfully.

servera | CHANGED | rc=0 >>
Changing password for user ansible.

passwd: all authentication tokens updated successfully.

配置与ansible用户的免密

[root@workstation ansible]# ssh-copy-id ansible@servera
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
ansible@servera's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'ansible@servera'"
and check to make sure that only the key(s) you wanted were added.

[root@workstation ansible]# ssh-copy-id ansible@serverb
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
ansible@serverb's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'ansible@serverb'"
and check to make sure that only the key(s) you wanted were added.

[root@workstation ansible]#

[root@workstation ansible]# ansible all -m shell -a 'pwd' -k -u ansible
SSH password:
servera | CHANGED | rc=0 >>
/home/ansible
serverb | CHANGED | rc=0 >>
/home/ansible
[root@workstation ansible]#

默认改为ansible用户连接

ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型
[root@workstation ansible]# ansible all -m shell -a 'pwd'
servera | CHANGED | rc=0 >>
/home/ansible
serverb | CHANGED | rc=0 >>
/home/ansible
[root@workstation ansible]#

设置被控端提权

[root@workstation ansible]# ansible all -m shell -a 'echo ansible ALL=\(ALL\) NOPASSWD: ALL > /etc/sudoers.d/ansible' -u root -k
SSH password:
servera | CHANGED | rc=0 >>

serverb | CHANGED | rc=0 >>

ansible这边并没有提权

[root@workstation ansible]# ansible all -m shell -a 'id'
servera | CHANGED | rc=0 >>
uid=1001(ansible) gid=1001(ansible) groups=1001(ansible) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
serverb | CHANGED | rc=0 >>
uid=1000(ansible) gid=1000(ansible) groups=1000(ansible) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[root@workstation ansible]#

修改配置文件

ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型

成功提权

[root@workstation ansible]# ansible all -m shell -a 'id'
servera | CHANGED | rc=0 >>
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
serverb | CHANGED | rc=0 >>
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[root@workstation ansible]#

[root@workstation ansible]# ansible all -m shell -a 'pwd'
servera | CHANGED | rc=0 >>
/home/ansible
serverb | CHANGED | rc=0 >>
/home/ansible
[root@workstation ansible]#

定义inventory

列出当前选择的主机

[root@workstation ansible]# ansible servera --list-hosts
hosts (1):
    servera

[root@workstation ansible]# ansible servera,serverb --list-hosts
hosts (2):
    servera
    serverb

[root@workstation ansible]# ansible httpd,mysql --list-hosts
hosts (3):
    servera
    serverb
    abc
[root@workstation ansible]# cat hosts
[httpd]
servera
serverb

[mysql]
abc

[root@workstation ansible]#
这里hosts为ini格式和那个yum差不多
不想加入组的用户得写在第一排。

[root@workstation ansible]# ansible ungrouped --list-hosts
hosts (1):
    servere
[root@workstation ansible]# head -n3 hosts
servere
[httpd]
servera
[root@workstation ansible]#
servere不属于任何组

组包含组

[root@workstation ansible]# vi hosts
[root@workstation ansible]# ansible web --list-hosts
hosts (3):
    servera
    serverb
    abc
[root@workstation ansible]# cat hosts
servere
[httpd]
servera
serverb

[mysql]
abc

[web:children]
httpd
mysql    #那么这里就只能写组,不可以写主机
[root@workstation ansible]#

[web:children]
httpd
mysql
[web]
fox        #这样才可以添加fox主机
ansible选择了两边主机,ansible会自动去重。

支持通配符
组和主机都通配

[root@workstation ansible]# ansible 'server*' --list-hosts
hosts (3):
    servere
    servera
    serverb
[root@workstation ansible]#

hosts也可以连续定义

ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型
[root@workstation ansible]# ansible 'server*,!*server1' --list-hosts
hosts (14):
    server2
    server3
    server4
    server5
    server6
    server7
    server8
    server9
    server10
    server11
    server12
    servere
    servera
    serverb
[root@workstation ansible]#

唯独不要server1

[root@workstation ansible]# ansible 'httpd,&mysql' --list-hosts
hosts (1):
    server10
[root@workstation ansible]# cat hosts
server[1:12]

servere
[httpd]
servera
serverb
server10
[mysql]
abc
server10
[web:children]
httpd
mysql
[root@workstation ansible]#

既属于web又属于httpd

boston,londor,&prod,!lb
在boston与londor同时也在prod但是去除lb

正则表达式

ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型
有s或h字母 尾巴为example.com的
没带^就不是开头为s或h

另外指定新的主机清单。读新的hosts

[root@workstation ansible]# echo  servera  > file
[root@workstation ansible]# ansible servera -i file --list-hosts
  hosts (1):
    servera
[root@workstation ansible]#

有关ansible常用参数
-m 指定模块
-a 指定模块参数
-u 指定被控端的连接用户2
-k 密码验证,不指定就是秘钥验证
-i 指定主机清单 ansible servera -i file –list-hosts
–list-hosts 列出所选的主机

ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型

yaml格式定义主机清单

ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型

比较完整的yaml写法

ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型

ini格式转换yaml

ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型

yaml语法对程序更友好

配置文件

默认配置文件位置
[root@workstation ansible]# pwd
/etc/ansible
[root@workstation ansible]# ls
ansible.cfg  file  file.yaml  hosts  roles
[root@workstation ansible]#

配置文件有优先级读取顺序
ANSIBLE_CONFIG = /tmp/ansible.cfg
当前目录下的ansible.cfg  ./
家目录   ~/.ansible.cfg
/etc/ansible/ansible.cfg

ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型

更改运行主机清单的路径

ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型

ansible.cfg的参数

inventory      = ./hosts
#library        = /usr/share/my_modules/
#module_utils   = /usr/share/my_module_utils/
#remote_tmp     = ~/.ansible/tmp
#local_tmp      = ~/.ansible/tmp
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks          = 5
inventory      = ./hosts
#library        = /usr/share/my_modules/
#module_utils   = /usr/share/my_module_utils/
#remote_tmp     = ~/.ansible/tmp   被控端路径  py的临时运行目录的位置
#local_tmp      = ~/.ansible/tmp   主控端临时存储目录
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks          = 5       并发数  一次性连5台,再连5台
#poll_interval  = 15      探测任务执行如何  每15秒探测
#ask_pass      = True    密码验证   -k 默认false
#remote_port    = 22    被控端,端口号
remote_user = ansible    远程主机用什么连
[privilege_escalation]
become=True          要提权
become_method=sudo
become_user=root     提权用户
become_ask_pass=False   不问提权密码
#host_key_checking = False     自动接受公钥  (好用)

log_path=/var/log/ansible.log   普通用户得改这个路径
普通用户写不了var/log
module_name = command  不指定模块默认为command模块

ad-hoc指令

官方文档
https://docs.ansible.com/

ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型
搜索模块时搜索builtin 内置模块

shell模块
优点:功能强大
缺点:无法保证幂等性
ansible servera -m shell -a ‘命令’

ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型
来自官方文档的教诲(狗头)
[root@workstation maosible]# ansible  servera -m shell -a 'chdir=/tmp pwd'
servera | CHANGED | rc=0 >>
/tmp
[root@workstation maosible]#

[root@workstation maosible]# ansible  servera -m shell -a 'creates=/tmp/file pwd'
servera | SUCCESS | rc=0 >>
skipped, since /tmp/file exists
[root@workstation maosible]#
文件存在,则不执行pwd

removes相反

command模块为默认模块
ansible servera -a ‘pwd’
command不允许 > < | 之类。 他会当成字符串

raw模块就是被削弱的shell

script模块
让脚本在被控端执行
这个脚本可以不需要执行权限,因为他会被解析成py文件,被控端通过执行py文件执行脚本

其他常用模块

authorized_keys 分发公钥

[root@workstation .ssh]# ansible-galaxy collection install ansible.posix -vvv
ansible-galaxy 2.9.11
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible-galaxy
python version = 3.6.8 (default, Mar 18 2021, 08:58:41) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]
Using /etc/ansible/ansible.cfg as config file
Process install dependency map
Opened /root/.ansible/galaxy_token
Processing requirement collection 'ansible.posix'
Collection 'ansible.posix' obtained from server default https://galaxy.ansible.com/api/
Starting collection install process
Installing 'ansible.posix:1.4.0' to '/root/.ansible/collections/ansible_collections/ansible/posix'
Downloading https://galaxy.ansible.com/download/ansible-posix-1.4.0.tar.gz to /root/.ansible/tmp/ansible-local-5179_oikgerz/tmpqxvizmuo

2.9没有此内置模块
那么使用galaxy从网上下载

通过官方文档发现名字为ansible.posix.authorized_key

ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型
[root@workstation modules]#  ansible all -m ansible.posix.authorized_key -a 'user=root key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCkQdrj0fMPRQiC7f+1I4N23k/OdwAqt0ONpNDmChbD/ehrJ5lrEspinVtolwBdR5lKnhnWpb9iC29QlR4epd0EdLrId1wRwZ1pMteZuAwR7IlfCCzzSo2ND6gBl1KSIPV4aZhigspFC1JyGAuoB4HIjeZ9NI6w1XP+U/hoGNLjKZtEhPK+H5ijXpb9pVMPvCa0uLYta0qqIMSpIkLlNFUQ1hNd4g4b+aj2y+BzBG/+kYS/7+vDuiBw0GoZ18zmY0ueQjeafg00RNLM/qU90soo29T9tRPc67PozFw20RB8z4LH8Iwe3jzOzGEOWFQ0frJyOg8CgOwDoqMTk4oNjwx4HEOSjv9SsaWYQGZxOkJ5iVZ3MLQt1MkEzhJjibCTMIDlQQ+Dj16hFTMRmM7EXc4AHq1gwURqRv96e0pvmC7RIAFWiPd9IvSSmt4HJB/qGmQjCmvvy84FAGddbEiYGOH2YShzoppBVpxQEsCbHxvZQXJbpwb0uAvn22Pxd5AsH6M= root@workstation" state=present'

参考文档:https://docs.ansible.com/ansible/latest/collections/ansible/posix/authorized_key_module.html

可是2.9拥有authorized_key

[root@workstation modules]#  ansible all -m authorized_key -a 'user=root key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCkQdrj0fMPRQiC7f+1I4N23k/OdwAqt0ONpNDmChbD/ehrJ5lrEspinVtolwBdR5lKnhnWpb9iC29QlR4epd0EdLrId1wRwZ1pMteZuAwR7IlfCCzzSo2ND6gBl1KSIPV4aZhigspFC1JyGAuoB4HIjeZ9NI6w1XP+U/hoGNLjKZtEhPK+H5ijXpb9pVMPvCa0uLYta0qqIMSpIkLlNFUQ1hNd4g4b+aj2y+BzBG/+kYS/7+vDuiBw0GoZ18zmY0ueQjeafg00RNLM/qU90soo29T9tRPc67PozFw20RB8z4LH8Iwe3jzOzGEOWFQ0frJyOg8CgOwDoqMTk4oNjwx4HEOSjv9SsaWYQGZxOkJ5iVZ3MLQt1MkEzhJjibCTMIDlQQ+Dj16hFTMRmM7EXc4AHq1gwURqRv96e0pvmC7RIAFWiPd9IvSSmt4HJB/qGmQjCmvvy84FAGddbEiYGOH2YShzoppBVpxQEsCbHxvZQXJbpwb0uAvn22Pxd5AsH6M= root@workstation" state=present'

所以没必要去下载ansible.posix.authorized_key
但是可以显示出参考文档的重要性
那么遇到问题,可以直接去寻找官方的英文文档,会更有效率。

以下为转载

https://cloud.tencent.com/developer/news/327468

ansible原理

Ansible 是一个模型驱动的配置管理器,支持多节点发布、远程任务执行。默认使用 SSH 进行远程连接。无需在被管理节点上安装附加软件,可使用各种编程语言进行扩展。

一、Ansible基本架构

ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型
上图为ansible的基本架构,从上图可以了解到其由以下部分组成:

核心:ansible

核心模块(Core Modules):这些都是ansible自带的模块

扩展模块(Custom Modules):如果核心模块不足以完成某种功能,可以添加扩展模块

插件(Plugins):完成模块功能的补充

剧本(Playbooks):ansible的任务配置文件,将多个任务定义在剧本中,由ansible自动执行

连接插件(Connectior Plugins):ansible基于连接插件连接到各个主机上,虽然ansible是使用ssh连接到各个主机的,但是它还支持其他的连接方法,所以需要有连接插件

主机群(Host Inventory):定义ansible管理的主机

二、Ansible工作原理

ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型
ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型

以上是从网上找到的两张ansible工作原理图,两张图基本都是在架构图的基本上进行的拓展。从上面的图上可以了解到:

1、管理端支持local 、ssh、zeromq 三种方式连接被管理端,默认使用基于ssh的连接---这部分对应基本架构图中的连接模块;

2、可以按应用类型等方式进行Host Inventory(主机群)分类,管理节点通过各类模块实现相应的操作---单个模块,单条命令的批量执行,我们可以称之为ad-hoc;

3、管理节点可以通过playbooks 实现多个task的集合实现一类功能,如web服务的安装部署、数据库服务器的批量备份等。playbooks我们可以简单的理解为,系统通过组合多条ad-hoc操作的配置文件 。

控制执行顺序,优化,事实变量

&#x6B63;&#x5E38;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;playbook&#x4ECE;&#x4E0A;&#x5230;&#x4E0B;&#x4F9D;&#x6B21;&#x6267;&#x884C;
&#x6709;role&#x5148;&#x6267;&#x884C;role&#x5C31;&#x7B97;role&#x5728;&#x4EFB;&#x52A1;&#x6700;&#x4E0B;&#x9762;

- name: import roles
  include_role:            #import_role&#x4E5F;&#x53EF;&#x4EE5;
     name: httpd
&#x8FD9;&#x6837;&#x7684;&#x8BDD;&#x5C31;&#x662F;&#x4ECE;&#x4E0A;&#x5F80;&#x4E0B;

pre_task&#x8DD1;&#x5728;role&#x4E4B;&#x524D;

- name: install httpd
  hosts: webservers

  tasks:
  - name: install httpd......

    yum:
      name: httpd
      state: present
    notify: debug yum
    changed_when: true

  handlers:
  - name: debug yum1
    debug:
      msg: "I am handler for  tasks.."
    listen: debug yum

  - name: debug yum2
    debug:
      msg: "I am handler for  tasks......................"
    listen: debug yum

4~ &#x5728;Playbook&#x4E2D;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;Order&#x5173;&#x952E;&#x5B57;&#x6765;&#x5B9A;&#x4E49;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#x7684;&#x4E3B;&#x673A;&#x7684;&#x5148;&#x540E;&#x987A;&#x5E8F;
forks = 1 &#x5E76;&#x53D1;&#x4E3A;1
&#x5C0F;&#x4E8E;&#x5E76;&#x53D1;&#x65F6;&#xFF0C;&#x624D;&#x4F1A;&#x6709;&#x5148;&#x540E;&#x987A;&#x5E8F;
order: inventory
&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#x4E3A;inventory.

reverse_inventory
sorted
reverse_sorted
shuffle

5~ &#x4F18;&#x5316;&#x6267;&#x884C;&#x901F;&#x5EA6;
&#x6267;&#x884C;&#x5267;&#x672C;&#x65F6;&#xFF0C;&#x5267;&#x672C;&#x5199;&#x597D;&#xFF0C;&#x7981;&#x6B62;&#x4E8B;&#x5B9E;&#x53D8;&#x91CF;&#x6536;&#x96C6;(get_fact)
Gethering Facts&#x6536;&#x96C6;&#x4E8B;&#x5B9E;&#x53D8;&#x91CF;
&#x63D0;&#x5347;forks&#x5E76;&#x53D1;&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A;5  (controller cpu&#x6027;&#x80FD;&#x597D;&#x53EF;&#x4EE5;&#x52A0;&#x5927;)
ansible-playbook  -f  8
&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x6307;&#x5B9A;&#x5E76;&#x53D1;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x66F4;&#x6539;&#x914D;&#x7F6E;&#x6587;&#x4EF6;ansible.cfg
&#x6A21;&#x5757;&#x5904;&#x7406;
yum&#x88C5;&#x5305;&#x4E0D;&#x4F7F;&#x7528;&#x5FAA;&#x73AF;loop&#xFF0C;&#x4F1A;&#x66F4;&#x5FEB;
&#x4E0D;&#x7528;&#x5FAA;&#x73AF;&#x5C31;&#x662F;&#x4E00;&#x6B21;&#x6027;&#x88C5;5&#x4E2A;&#x5305;&#xFF0C;&#x5FAA;&#x73AF;&#x4F60;&#x5F97;&#x4E00;&#x4E2A;&#x4E2A;&#x6765;
&#x62F7;&#x8D1D;&#x76EE;&#x5F55;
copy&#x6A21;&#x5757;&#x62F7;&#x8D1D;&#x90A3;&#x4E9B;&#x6587;&#x4EF6;&#x6570;&#x91CF;&#x591A;&#x4E14;&#x5C0F;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F1A;&#x5F88;&#x6162;
&#x4F7F;&#x7528;synchronize&#x4F1A;&#x66F4;&#x5FEB;(rync)
synchronize:
   src: files
   dest: /root/files
ssh&#x8FDE;&#x63A5;
&#x65E0;&#x8BBA;&#x591A;&#x5C11;&#x4E2A;&#x5267;&#x672C;&#x6267;&#x884C;&#xFF0C;&#x548C;&#x5BF9;&#x65B9;&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;&#x65F6;&#xFF0C;&#x53EA;&#x4F1A;&#x5171;&#x7528;&#x4E00;&#x4E2A;&#x901A;&#x9053;
&#x5F53;&#x6211;&#x5267;&#x672C;&#x5B8C;&#x6210;&#x540E;&#xFF0C;&#x6211;&#x548C;&#x88AB;&#x63A7;&#x8282;&#x70B9;&#x7684;&#x8FDE;&#x63A5;(ssh)&#x4E0D;&#x4F1A;&#x7ACB;&#x5373;&#x65AD;&#x5F00;&#xFF0C;&#x4F1A;&#x6709;60&#x79D2;&#x7684;&#x901A;&#x9053;&#x6301;&#x7EED;&#x65F6;&#x95F4;
&#x8FD9;&#x662F;&#x9ED8;&#x8BA4;&#x503C; Controlpersist=60s
lsof -i:22 &#x53EF;&#x4EE5;&#x67E5;&#x770B;22&#x7AEF;&#x53E3;
&#x8FD9;&#x4E2A;&#x503C;&#x662F;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x914D;&#x7F6E;&#x6765;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x7684;
[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist = 60s
&#x4E0B;&#x6B21;&#x6267;&#x884C;ansible&#x65F6;&#x5019;&#x4F1A;&#x7528;&#x8FD9;&#x4E2A;&#x672A;&#x65AD;&#x5F00;&#x7684;&#x901A;&#x9053;
&#x5EFA;&#x7ACB;&#x901A;&#x9053;&#x9700;&#x8981;&#x5F00;&#x9500;&#xFF0C;tcp&#x7684;&#x4E09;&#x6B21;&#x63E1;&#x624B;&#x4EC0;&#x4E48;&#x7684;
&#x56DE;&#x8C03;&#x63D2;&#x4EF6;
callback_whitelist=timer,profile_tasks,profile_roles,cgroup_perf_recap
&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x6267;&#x884C;&#x540E;&#x7684;&#x65F6;&#x95F4;&#xFF0C;&#x8FD9;&#x4E09;&#x4E2A;&#x770B;&#x65F6;&#x95F4;&#x7684;&#x53C2;&#x6570;&#x4E00;&#x8D77;&#xFF0C;&#x4F1A;&#x66F4;&#x8BE6;&#x7EC6;
cgroup_perf_recap&#x8FD9;&#x4E2A;&#x56DE;&#x8C03;&#x63D2;&#x4EF6;&#x53EF;&#x4EE5;&#x770B;&#x6BCF;&#x4E2A;&#x4EFB;&#x52A1;&#x5360;&#x7528;&#x591A;&#x5C11;cpu&#x548C;&#x5185;&#x5B58;
&#x5355;&#x72EC;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x63D2;&#x4EF6;&#x5219;&#x6CA1;&#x7528;&#xFF0C;&#x5F97;&#x8BBE;&#x7F6E;cgroup&#xFF0C;&#x5E76;&#x4E14;&#x6DFB;&#x52A0;&#x4E24;&#x884C;
[callback_cgroup_perf_recap]
control_group=ansible_profile
cgroup&#x6536;&#x96C6;&#x7684;&#x662F;controller&#x7684;&#x4FE1;&#x606F;
&#x8BBE;&#x7F6E;cgroup
[root@workstation AppStream]# cgcreate -a student:student -t student:student -g cpuacct,memory,pids:ansible_profile

6~ &#x4E8B;&#x5B9E;&#x53D8;&#x91CF;
gathering = explicit/implicit &#x9ED8;&#x8BA4;&#x503C;&#x4E3A;&#xFF1A;implict
implict: &#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x662F;&#x6536;&#x96C6;&#x4E8B;&#x5B9E;&#x53D8;&#x91CF;&#x7684;&#x3002;&#x4F60;&#x7684;Play&#x60F3;&#x8981;&#x5173;&#x95ED;&#x4E8B;&#x5B9E;&#x6536;&#x96C6;&#x5C31;&#x9700;&#x8981;&#x5728;play&#x4E2D;
gather_facts: False
&#x4E0D;&#x6536;&#x96C6;&#x4E8B;&#x5B9E;&#x53D8;&#x91CF;&#x4F60;&#x5C31;&#x4E0D;&#x80FD;&#x8C03;&#x7528;&#x4ED6;
explicit:&#x9ED8;&#x8BA4;&#x4E0D;&#x662F;&#x6536;&#x96C6;&#x4E8B;&#x5B9E;&#x53D8;&#x91CF;&#xFF0C;&#x60F3;&#x6536;&#x96C6;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x5728;play&#x4E2D;&#x5B9A;&#x4E49;gather_facts: True
tower&#x53EF;&#x4EE5;&#x7F13;&#x5B58;&#x4E8B;&#x5B9E;&#x53D8;&#x91CF;&#xFF0C;&#x4EE5;&#x63D0;&#x9AD8;&#x83B7;&#x53D6;&#x4E8B;&#x5B9E;&#x53D8;&#x91CF;&#x7684;&#x901F;&#x5EA6;

在playbook里标记tags

always 标签只要运行playbook指定tags那么always一定会跑(尽管tags不匹配)

ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型
这个role被打上了两个tags,指定这两个其中一个role都会执行
ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型
除非–skip-tags always 跳过这个标签
never playbook运行时,不带任何–tags则never标签永远不会执行

tags可以帮助我们对任务进行’打标签’的操作,当任务存在标签以后,我们就可以在执行playbook时,借助标签,指定执行哪些任务,或者指定不执行哪些任务。在实际的使用中,我们应该让tags的值能够见名知义。

当指定标签后,只有标签对应的任务会被执行,其他任务都不会被执行

判断变量类型

1> &#x5224;&#x5B9A;&#x53D8;&#x91CF;&#x7684;&#x7C7B;&#x578B;
&#x901A;&#x8FC7;type_debug&#x63D2;&#x4EF6;&#xFF0C;&#x6765;&#x5224;&#x5B9A;&#x53D8;&#x91CF;&#x7684;&#x7C7B;&#x578B;
&#x2022; Strings (a sequence of characters)
&#x2022; Numbers (a numeric value)
&#x2022; Booleans (true/false values)
&#x2022; Dates (ISO-8601 calendar date)
&#x2022; Null (sets the variable to undefined the variable)
&#x2022; Lists or Arrays (a sorted collection of values)
&#x2022; Dictionaries (a collection of key-value pairs)

[student@workstation ansible]$ cat 4.yml
- name: ddd
  hosts: webservers
  vars:
    username: kevin

    user_list1:
    - user11,user22,user33
    - { username: "kevin" , password: "redhat" }
    - user3

    user_list2: [ ["user22","user33","user44"] ,"user4","user5"]

    user_info1:
      username: kevin
      password: redhat
      shell_type: bash
      uid: 5000

    user_info2: {"username":["user1","user2","user3","user4","user5"],"password":{"redhat":"redhat2"}}

    user_num: 1
    check: 0
    time1: 2022-10-15T16:43:52+08:00
    time2: 2022-10-15
    time3: 2022-10-15 16:44:49
    user_name: null

  tasks:
  - name: debug var type
    debug:
      msg: "{{ user_list1 | type_debug }}"

ansible 002 连接被控端 inventory ansible.cfg  ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型

Original: https://www.cnblogs.com/supermao12/p/16646066.html
Author: supermao12
Title: ansible 002 连接被控端 inventory ansible.cfg ansible-adhoc ansible原理 控制执行顺序,优化,事实变量 tags 变量类型

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/803642/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球