我尝试编写一个小的ansible-playbook来进行服务器的基本设置,但我的角色任务拒绝运行:))
Playbook具有以下目录结构:
└── install
├── group_vars
│ └── all.yml
├── roles
│ ├── basic_setup
│ │ └── tasks
│ │ └── main.yml
│ └── user_management
│ └── tasks
│ └── main.yml
└── setup.yml
setup.yml看起来像:
---
- hosts: '{{ target }}'
become: yes
remote_user: root
roles:
- { role: basic_setup }
- { role: user_management }
例如,我的install/roles/basic_setup/tasks/main.yml
看起来像:
---
- name: Install python2.7
raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)
when: ansible_os_family == "Debian"
- name: 'test this playbook'
shell: "echo 'hello world'"
当我尝试运行playbook时,我得到了这个输出:
$ ansible-playbook install/setup.yml --ask-pass --user=root --extra-vars "target=192.168.1.228" -vvv
Using /etc/ansible/ansible.cfg as config file
SSH password:
_____________________
< PLAYBOOK: setup.yml >
---------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
1 plays in install/setup.yml
____________
< PLAY RECAP >
------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
文件install/roles/user_management/tasks/main.ym
l看起来像这样:
---
- name: Ensure bogdan user
user:
name: admin
state: present
- name: Ensure ssh key access
authorized_key:
key: "{{ lookup('file', key_path) }}"
user: admin
state: present
- name: Ensure admin user user is sudoer
lineinfile:
dest: /etc/sudoers
line: "admin ALL=(ALL) NOPASSWD:ALL"
regexp: '^admin ALL\='
state: present
validate: "visudo -cf %s"
- name: Create deployer user
user:
name: deployer
state: present
- name: Ensure ssh key access
authorized_key:
key: "{{ lookup('file', key_path) }}"
user: deployer
state: present
有人可以帮我理解我做错了什么吗?
答案 0 :(得分:1)
你的剧本很好。问题在您的命令和库存文件中。
我从未见过为 hosts:字段指定IP地址。
从技术上讲,您目前正在做的是:
- hosts: '{{ target }}' # 192.168.1.228, comes from command line variable
become: yes
remote_user: root
你应该做的是有一个库存文件(inventory.ini),它将ip地址映射到一个名称,语法如下:
[your_host]
192.168.1.228
该剧本保持不变,但在命令行上你可以:
$ ansible-playbook install/setup.yml --ask-pass --user=root --extra-vars "target=your_host" -vvv
哪个会奏效。 它也适用于使用主机名。 (--extra-vars中的完整主机名)
我不确定为什么他们在docs中有IP地址示例。