一、前言:
ansible与ansible-core的对应关系
ansible==8.0.0对应的[core 2.15.0]
二、python版本确认:
step 1:确认是否已安装python3和pip3
python -V
pip3 -V
step 2:安装pip3,修改国内源
dnf install -y python3-pip
pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
step 3:更新pip
pip3 install --upgrade pip
step 4:安装ansible
pip3 install ansible==8.0.0
step 5:验证是否安装成功
ansible --version
step 5:老版本的离线安装
首先安装python2,Python 2.7.18(last release of Python 2)下载地址https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tgz
tar -xzvf ansible_v2.9.27_install.tar.gz #解压安装包,不同版本替换为不同的安装包;
cd ansible_v2.9.27_install
chmod +x ansible_v2.9.0_install.sh
sh ansible_v2.9.0_install.sh
通过网盘分享的文件:ansible_v2.9.27_install.zip
链接: https://pan.baidu.com/s/1WHTV0PqonTpwa7D0MwrARQ?pwd=yu99 提取码: yu99
三、ansible基础配置
step 6:配置ssh免登录
ssh-keygen -t rsa
ssh-copy-id root@192.168.71.112
step 7:配置主机清单hosts
mkdir /etc/ansible
vim /etc/ansible/hosts
[test]
192.168.71.112
查看已经已有的hosts清单
ansible all --list
/etc/ansible/ansible.cfg模板
# config file for ansible -- https://ansible.com/
# ===============================================
# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first
[defaults]
# some basic default values...
# 默认库文件位置,hosts中存放所有主机信息
#inventory = /etc/ansible/hosts
# Ansible 默认搜寻模块的位置
#library = /usr/share/my_modules/
#module_utils = /usr/share/my_module_utils/
# Ansible 通过远程传输模块到远程主机,然后远程执行,执行后在清理现场.
#remote_tmp = ~/.ansible/tmp
#local_tmp = ~/.ansible/tmp
# 主机通信时的默认并行进程数,默认值比较保守,可调大
#forks = 5
# 异步执行任务时,每15s 回查任务状态
#poll_interval = 15
# 提权
#sudo_user = root
# 用来控制Ansible playbook 在执行sudo之前是否询问sudo密码.默认为no
#ask_sudo_pass = True
# Ansible 剧本playbook 是否会自动默认弹出弹出密码.默认为no
#ask_pass = True
#transport = smart
# 远程端口号
#remote_port = 22
# 默认模块和系统之间通信的计算机语言,默认为’C’语言
#module_lang = C
#module_set_locale = False
# plays will gather facts by default, which contain information about
# the remote system.
#
# smart - gather by default, but don't regather if already gathered
# implicit - gather by default, turn off with gather_facts: False
# explicit - do not gather by default, must say gather_facts: True
#gathering = implicit
# This only affects the gathering done by a play's gather_facts directive,
# by default gathering retrieves all facts subsets
# all - gather all subsets
# network - gather min and network facts
# hardware - gather hardware facts (longest facts to retrieve)
# virtual - gather min and virtual facts
# facter - import facts from facter
# ohai - import facts from ohai
# You can combine them using comma (ex: network,virtual)
# You can negate them using ! (ex: !hardware,!facter,!ohai)
# A minimal set of facts is always gathered.
#gather_subset = all
# some hardware related facts are collected
# with a maximum timeout of 10 seconds. This
# option lets you increase or decrease that
# timeout to something more suitable for the
# environment.
# gather_timeout = 10
# additional paths to search for roles in, colon separated
roles_path = /etc/ansible/roles:/usr/share/ansible/roles
# uncomment this to disable SSH key host checking
#host_key_checking = False
# change the default callback, you can only have one 'stdout' type enabled at a time.
#stdout_callback = skippy
## Ansible ships with some plugins that require whitelisting,
## this is done to avoid running all of a type by default.
## These setting lists those that you want enabled for your system.
## Custom plugins should not need this unless plugin author specifies it.
# enable callback plugins, they can output to stdout but cannot be 'stdout' type.
#callback_whitelist = timer, mail
# Determine whether includes in tasks and handlers are "static" by
# default. As of 2.0, includes are dynamic by default. Setting these
# values to True will make includes behave more like they did in the
# 1.x versions.
#task_includes_static = True
#handler_includes_static = True
# Controls if a missing handler for a notification event is an error or a warning
#error_on_missing_handler = True
# change this for alternative sudo implementations
#sudo_exe = sudo
# What flags to pass to sudo
# WARNING: leaving out the defaults might create unexpected behaviours
#sudo_flags = -H -S -n
# SSH 连接超时时间
# SSH timeout
#timeout = 10
# playbook 默认远程用户
# default user to use for playbooks if user is not specified
# (/usr/bin/ansible will use current user as default)
#remote_user = root
# Ansible 日志路径
# logging is off by default unless this path is defined
# if so defined, consider logrotate
#log_path = /var/log/ansible.log
# ansible 默认使用的 command 模块
# default module name for /usr/bin/ansible
#module_name = command
# use this shell for commands executed under sudo
# you may need to change this to bin/bash in rare instances
# if sudo is constrained
#executable = /bin/sh
# if inventory variables overlap, does the higher precedence one win
# or are hash values merged together? The default is 'replace' but
# this can also be set to 'merge'.
#hash_behaviour = replace
# by default, variables from roles will be visible in the global variable
# scope. To prevent this, the following option can be enabled, and only
# tasks and handlers within the role will see the variables there
#private_role_vars = yes
# list any Jinja2 extensions to enable here:
#jinja2_extensions = jinja2.ext.do,jinja2.ext.i18n
# if set, always use this private key file for authentication, same as
# if passing --private-key to ansible or ansible-playbook
#private_key_file = /path/to/file
# If set, configures the path to the Vault password file as an alternative to
# specifying --vault-password-file on the command line.
#vault_password_file = /path/to/vault_password_file
# format of string {
{ ansible_managed }} available within Jinja2
# templates indicates to users editing templates files will be replaced.
# replacing {file}, {host} and {uid} and strftime codes with proper values.
#ansible_managed = Ansible managed: {file} modified on %Y-%m-%d %H:%M:%S by {uid} on {host}
# {file}, {host}, {uid}, and the timestamp can all interfere with idempotence
# in some situations so the default is a static string:
#ansible_managed = Ansible managed
# by default, ansible-playbook will display "Skipping [host]" if it determines a task
# should not be run on a host. Set this to "False" if you don't want to see these "Skipping"
# messages. NOTE: the task header will still be shown regardless of whether or not the
# task is skipped.
#display_skipped_hosts = True
# by default, if a task in a playbook does not include a name: field then
# ansible-playbook will construct a header that includes the task's action but
# not the task's args. This is a security feature because ansible cannot know
# if the *module* considers an argument to be no_log at the time that the
# header is printed. If your environment doesn't have a problem securing
# stdout from ansible-playbook (or you have manually specified no_log in your
# playbook on all of the tasks where you have secret information) then you can
# safely set this to True to get more informative messages.
#display_args_to_stdout = False
# by default (as of 1.3), Ansible will raise errors when attempting to dereference
# Jinja2 variables that are not set in templates or action lines. Uncomment this line
# to revert the behavior to pre-1.3.
#error_on_undefined_vars = False
# by default (as of 1.6), Ansible may display warnings based on the configuration of the
# system running ansible itself. This may include warnings about 3rd party packages or
# other conditions that should be resolved if possible.
# to disable these warnings, set the following value to False:
#system_warnings = True
# by default (as of 1.4), Ansible may display deprecation warnings for language
# features that should no longer be used and will be removed in future versions.
# to disable these warnings, set the following value to False:
#deprecation_warnings = True
# (as of 1.8), Ansible can optionally warn when usage of the shell and
# command module appear to be simplified by using a default Ansible module
# instead. These warnings can be silenced by adjusting the following
# setting or adding warn=yes or warn=no to the end of the command line
# parameter string. This will for example suggest using the git module
# instead of shelling out to the git command.
# command_warnings = False
# set plugin path directories here, separate with colons
#action_plugins = /usr/share/ansible/plugins/action
#cache_plugins = /usr/share/ansible/plugins/cache
#callback_plugins = /usr/share/ansible/plugins/callback
#connection_plugins = /usr/share/ansible/plugins/connection
#lookup_plugins = /usr/share/ansible/plugins/lookup
#inventory_plugins = /usr/share/ansible/plugins/inventory
#vars_plugins = /usr/share/ansible/plugins/vars
#filter_plugins