Ansible playbook 不 运行 任务来自角色
Ansible playbook does not run tasks from roles
我尝试为服务器的基本设置编写一个小的 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'"
当我尝试 运行 剧本时,我得到了这个输出:
$ 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
有人可以帮助我了解我做错了什么吗?
你的剧本很好。问题出在您的命令和清单文件中。
我从未见过它可以为 hosts: 字段指定 IP 地址。
从技术上讲,您目前正在做的是:
- hosts: '{{ target }}' # 192.168.1.228, comes from command line variable
become: yes
remote_user: root
您应该做的是拥有一个将 IP 地址映射到名称的清单文件 (inventory.ini),语法如下:
[your_host]
192.168.1.228
剧本保持不变,但您在命令行上执行:
$ ansible-playbook install/setup.yml --ask-pass --user=root --extra-vars "target=your_host" -vvv
哪个可行。
它还将使用主机名。 (完整的主机名在 --extra-vars)
我不确定为什么他们在文档中有 IP 地址示例。
我尝试为服务器的基本设置编写一个小的 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'"
当我尝试 运行 剧本时,我得到了这个输出:
$ 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
有人可以帮助我了解我做错了什么吗?
你的剧本很好。问题出在您的命令和清单文件中。
我从未见过它可以为 hosts: 字段指定 IP 地址。
从技术上讲,您目前正在做的是:
- hosts: '{{ target }}' # 192.168.1.228, comes from command line variable
become: yes
remote_user: root
您应该做的是拥有一个将 IP 地址映射到名称的清单文件 (inventory.ini),语法如下:
[your_host]
192.168.1.228
剧本保持不变,但您在命令行上执行:
$ ansible-playbook install/setup.yml --ask-pass --user=root --extra-vars "target=your_host" -vvv
哪个可行。 它还将使用主机名。 (完整的主机名在 --extra-vars)
我不确定为什么他们在文档中有 IP 地址示例。