ansible下playbook安装httpd

root@192 \~# ansible --version

ansible 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

python version = 3.6.8 (default, Nov 21 2019, 19:31:34) GCC 8.3.1 20190507 (Red Hat 8.3.1-4)

root@192 \~# vim /etc/hosts

root@192 \~# scp /etc/hosts root@192.168.1.191:/etc/hosts

The authenticity of host '192.168.1.191 (192.168.1.191)' can't be established.

ECDSA key fingerprint is SHA256:ZVd4fwX7zxHrxSVxcJNQiZ0fzmEi1a9a+HIbw4pFZeU.

Are you sure you want to continue connecting (yes/no/fingerprint)? yes

Warning: Permanently added '192.168.1.191' (ECDSA) to the list of known hosts.

root@192.168.1.191's password:

hosts 100% 197 1.4KB/s 00:00

root@192 \~# hostnamectl set-hostname node1

Could not set property: Connection timed out

root@192 \~# hostnamectl set-hostname node1

root@192 \~# su -

Last login: Mon Apr 15 04:32:25 PDT 2024 from 192.168.1.40 on pts/1

root@node1 etc# cd /ansible

-bash: cd: /ansible: No such file or directory

root@node1 etc# cd /etc/ansible

root@node1 ansible# ll

total 24

-rw-r--r--. 1 root root 19985 Jul 21 2020 ansible.cfg

-rw-r--r--. 1 root root 1016 Jul 21 2020 hosts

drwxr-xr-x. 2 root root 6 Jul 21 2020 roles

root@node1 ansible# vim ansible.cfg

root@node1 ansible# ansible node2 -m shell -a "hostnamectl set-hostname node2" -u root -k

SSH password:

WARNING: provided hosts list is empty, only localhost is available. Note that the

implicit localhost does not match 'all'

WARNING: Could not match supplied host pattern, ignoring: node2

^C ERROR: User interrupted execution

root@node1 ansible# ansible node2 -m shell -a "hostnamectl set-hostname node2" -u root -k

SSH password:

WARNING: provided hosts list is empty, only localhost is available. Note that the

implicit localhost does not match 'all'

WARNING: Could not match supplied host pattern, ignoring: node2

root@node1 ansible# vim hosts

root@node1 ansible# ansible node2 -m shell -a "hostnamectl set-hostname node2" -u root -k

SSH password:

node2 | CHANGED | rc=0 >>

root@node1 ansible# ansible node2 -m shell -a 'hostname' -u root -k

SSH password:

node2 | CHANGED | rc=0 >>

node2

root@node1 ansible# ansible node2 -m shell -a "useradd admin" -u root -k

SSH password:

node2 | CHANGED | rc=0 >>

root@node1 ansible# ansible node2 -m shell -a "echo abc123. |passwd --stdin admin" -u root -k

SSH password:

node2 | CHANGED | rc=0 >>

Changing password for user admin.

passwd: all authentication tokens updated successfully.

root@node1 ansible# ssh-keygen

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

SHA256:MZuuc5+CUB8q0cLoskqbAjzPVW77mbWEAGF5mSJ2vIA root@node1

The key's randomart image is:

+---RSA 3072----+

| . .o. o |

| E +.=.+ |

| .o+o+ o |

| . +.+..= |

|.. +o+S. |

|oo. o..+o . |

|.++ .o..o. o |

|+ oo ooo =.. |

|+o .o.*o. |

+----SHA256-----+

root@node1 ansible# ssh-copy-id admin@node2

/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

admin@node2's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'admin@node2'"

and check to make sure that only the key(s) you wanted were added.

root@node1 ansible# ansible node2 -m shell -a 'echo admin "ALL=(ALL) NOPASSWD:ALL">>/etc/sudoers.d/admin' -u root -k

-bash: syntax error near unexpected token `('

root@node1 ansible# ansible node2 -m shell -a 'echo admin "ALL=(ALL) NOPASSWD:ALL">>/etc/sudoers.d/admin' -u root -k

SSH password:

node2 | UNREACHABLE! => {

"changed": false,

"msg": "Failed to connect to the host via ssh: root@node2: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",

"unreachable": true

}

root@node1 ansible# ansible node2 -m shell -a 'echo admin "ALL=(ALL) NOPASSWD:ALL">>/etc/sudoers.d/admin' -u root -k

SSH password:

node2 | CHANGED | rc=0 >>

root@node1 ansible# ansible node2 -m shell -a "ls /root"

node2 | CHANGED | rc=0 >>

anaconda-ks.cfg

Desktop

Documents

Downloads

Music

original-ks.cfg

Pictures

Public

QQ_3.2.7_240401_x86_64_01.rpm

Templates

Videos

root@node1 ansible#

root@node1 ansible#

root@node1 ansible# ll

total 24

-rw-r--r--. 1 root root 20006 Apr 15 04:53 ansible.cfg

-rw-r--r--. 1 root root 1021 Apr 15 04:56 hosts

drwxr-xr-x. 2 root root 6 Jul 21 2020 roles

root@node1 ansible# vim inventory

root@node1 ansible# ssh-copy-id root@192.168.1.191

/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

root@192.168.1.191's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'root@192.168.1.191'"

and check to make sure that only the key(s) you wanted were added.

root@node1 ansible# ansible httpd -m file -a 'path=/etc/yum.repos.d state=absent'

WARNING: Could not match supplied host pattern, ignoring: httpd

WARNING: No hosts matched, nothing to do

root@node1 ansible# ansible node2 -m file -a 'path=/etc/yum.repos.d state=absent'

node2 | CHANGED => {

"ansible_facts": {

"discovered_interpreter_python": "/usr/libexec/platform-python"

},

"changed": true,

"path": "/etc/yum.repos.d",

"state": "absent"

}

root@node1 ansible# vim install.yml


  • name: config httpd

hosts: all

tasks:

  • name: config repo1

yum_repository:

name: BaseOS

description: BaseOS_repo

baseurl: file:///media/BaseOS

gpgcheck: no

enabled: yes

  • name: config repo2

yum_repository:

name: AppStream

description: AppStream_repo

baseurl: file:///media/AppStream

gpgcheck: no

enabled: yes

  • name: mount

command: mount /dev/sr0 /media

  • name: install httpd

yum:

name: httpd

state: present

  • name: index.html

copy:

content: "hello world!"

dest: /var/www/html/index.html

  • name: start httpd

service:

name: httpd

state: restarted

enabled: yes

  • name: close firewalld

service:

name: firewalld

state: stopped

enabled: no

root@node1 ansible# ansible-playbook install.yml

PLAY config httpd ***********************************************************************************************************************************************************************************************

TASK Gathering Facts ********************************************************************************************************************************************************************************************

ok: node2

TASK config repo1 ***********************************************************************************************************************************************************************************************

ok: node2

TASK config repo2 ***********************************************************************************************************************************************************************************************

ok: node2

TASK mount ******************************************************************************************************************************************************************************************************

WARNING: Consider using the mount module rather than running 'mount'. If you need to use command because mount is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False'

in ansible.cfg to get rid of this message.

changed: node2

TASK install httpd **********************************************************************************************************************************************************************************************

changed: node2

TASK index.html *************************************************************************************************************************************************************************************************

changed: node2

TASK start httpd ************************************************************************************************************************************************************************************************

changed: node2

TASK close firewalld ********************************************************************************************************************************************************************************************

changed: node2

PLAY RECAP ********************************************************************************************************************************************************************************************************

node2 : ok=8 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

相关推荐
计算机编程指导师9 分钟前
计算机大数据项目推荐:基于Hadoop+Django的人工智能社交媒体情绪数据分析可视化系统源码 毕业设计 选题推荐 毕设选题 数据分析 机器学习
大数据·hadoop·python·计算机·spark·毕业设计·社交媒体
青春逝如流水10 分钟前
Java 线程与操作系统、硬件的关系:状态、映射与差异
java·开发语言·多线程·ai文章
谢亮_vipxieliang14 分钟前
Go gRPC基础与实战
开发语言·后端·golang
Aurorar0rua36 分钟前
CS50 x 2024 Notes Data structures - 08
c语言·开发语言·学习方法
高级程序源39 分钟前
springboot荆州旅游推介平台24175-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·django·课程设计
企业数字化笔记41 分钟前
新租户注册成功却不能用?开通状态、初始化恢复与可用性验收
开发语言·python
Guarding and trust43 分钟前
项目拆解(二):模型输出是‘野‘的——三层兜底+重分析,把AI变成可靠零件
java·人工智能·python·大模型·fastapi·ollama·错误兜底
瑶山1 小时前
Windows + WSL2 Ubuntu + VS Code配置C++开发环境
开发语言·c++·windows·vscode·wsl
用户298698530141 小时前
Python 实战:将 HTML 内容保存为图像文件
后端·python·html
for_ever_love__1 小时前
LangChain MultiVectorRetriever:一个文档存多个向量
人工智能·python·自然语言处理·langchain·ai大模型·多向量·摘要检索