ansible 002 連接被控端 inventory ansible.cfg ansible-adhoc ansible原理
- 2022 年 9 月 1 日
- 筆記
- ansible 自動化運維
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用戶連接
[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]#
修改配置文件
成功提權
[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也可以連續定義
[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
正則表達式
有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 列出所選的主機
yaml格式定義主機清單
比較完整的yaml寫法
ini格式轉換yaml
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.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指令
官方文檔
//docs.ansible.com/
搜索模塊時搜索builtin 內置模塊
shell模塊
優點:功能強大
缺點:無法保證冪等性
ansible servera -m shell -a ‘命令’
來自官方文檔的教誨(狗頭)
[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 //galaxy.ansible.com/api/
Starting collection install process
Installing 'ansible.posix:1.4.0' to '/root/.ansible/collections/ansible_collections/ansible/posix'
Downloading //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
[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'
參考文檔://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
但是可以顯示出參考文檔的重要性
那麼遇到問題,可以直接去尋找官方的英文文檔,會更有效率。
以下為轉載
//cloud.tencent.com/developer/news/327468
ansible原理
Ansible 是一個模型驅動的配置管理器,支持多節點發佈、遠程任務執行。默認使用 SSH 進行遠程連接。無需在被管理節點上安裝附加軟件,可使用各種編程語言進行擴展。
一、Ansible基本架構
上圖為ansible的基本架構,從上圖可以了解到其由以下部分組成:
核心:ansible
核心模塊(Core Modules):這些都是ansible自帶的模塊
擴展模塊(Custom Modules):如果核心模塊不足以完成某種功能,可以添加擴展模塊
插件(Plugins):完成模塊功能的補充
劇本(Playbooks):ansible的任務配置文件,將多個任務定義在劇本中,由ansible自動執行
連接插件(Connectior Plugins):ansible基於連接插件連接到各個主機上,雖然ansible是使用ssh連接到各個主機的,但是它還支持其他的連接方法,所以需要有連接插件
主機群(Host Inventory):定義ansible管理的主機
二、Ansible工作原理
以上是從網上找到的兩張ansible工作原理圖,兩張圖基本都是在架構圖的基本上進行的拓展。從上面的圖上可以了解到:
1、管理端支持local 、ssh、zeromq 三種方式連接被管理端,默認使用基於ssh的連接---這部分對應基本架構圖中的連接模塊;
2、可以按應用類型等方式進行Host Inventory(主機群)分類,管理節點通過各類模塊實現相應的操作---單個模塊,單條命令的批量執行,我們可以稱之為ad-hoc;
3、管理節點可以通過playbooks 實現多個task的集合實現一類功能,如web服務的安裝部署、數據庫服務器的批量備份等。playbooks我們可以簡單的理解為,系統通過組合多條ad-hoc操作的配置文件 。