通过ansible读取文本文件中的每一行
Read every line from a text file through ansible
我必须从文本文件中读取每一行并将其添加到 yaml 中。这是我的剧本
- hosts: localhost
tasks:
- name: Register a file content as a variable
ansible.builtin.shell: cat /home/packages.txt
register: result
- name: Print the transformed variable
ansible.builtin.debug:
msg: '{{ item }}'
loop: '{{ result.stdout | list }}'
- name: install
shell: yum install '{{ item }}'
loop: '{{ result.stdout | list }}'
packages.txt 包含
nginx
vim
grafana
当我 运行 这样做时,软件包没有安装。这是控制台输出,
TASK [Register a file content as a variable] **********************************************************************************
changed: [localhost]
TASK [Print the transformed variable] *****************************************************************************************
TASK [install] ****************************************************************************************************************
PLAY RECAP ********************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
要求是通过这个 ansible 剧本从文本文件安装这些包
给定文件
shell> cat packages.txt
nginx
vim
grafana
在本地主机上,简单地迭代列表,例如下面的剧本
shell> cat test.yml
- hosts: localhost
tasks:
- name: install
package: "{{ item }}"
loop: "{{ lookup('file', 'packages.txt').split('\n') }}"
给予
shell> ansible-playbook test.yml --check
PLAY [localhost] *****************************************************
TASK [install] *******************************************************
ok: [localhost] => (item=nginx)
ok: [localhost] => (item=vim)
ok: [localhost] => (item=grafana)
PLAY RECAP ***********************************************************
localhost: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
在远程主机上(查找仅适用于本地主机),迭代stdout_lines,例如
- name: Register a file content as a variable
command: cat packages.txt
register: result
- name: install
package: "{{ item }}"
loop: "{{ result.stdout_lines }}"
可选,如果你想避免命令或shell模块使用fetch。例如,在下面的剧本中
shell> cat test.yml
- hosts: test_11
tasks:
- name: Create directory files
file:
state: directory
path: files
- name: Get file
fetch:
src: packages.txt
dest: files
- name: install
package:
name: "{{ item }}"
loop: "{{ lookup('file', path).split('\n') }}"
vars:
path: 'files/{{ inventory_hostname }}/packages.txt'
创建目录 files 并首先从远程主机获取文件。这将创建文件
shell> cat files/test_11/packages.txt
nginx
vim
grafana
然后迭代文件中的行
TASK [install] ***************************************************
changed: [test_11] => (item=nginx)
changed: [test_11] => (item=vim)
changed: [test_11] => (item=grafana)
我必须从文本文件中读取每一行并将其添加到 yaml 中。这是我的剧本
- hosts: localhost
tasks:
- name: Register a file content as a variable
ansible.builtin.shell: cat /home/packages.txt
register: result
- name: Print the transformed variable
ansible.builtin.debug:
msg: '{{ item }}'
loop: '{{ result.stdout | list }}'
- name: install
shell: yum install '{{ item }}'
loop: '{{ result.stdout | list }}'
packages.txt 包含
nginx
vim
grafana
当我 运行 这样做时,软件包没有安装。这是控制台输出,
TASK [Register a file content as a variable] **********************************************************************************
changed: [localhost]
TASK [Print the transformed variable] *****************************************************************************************
TASK [install] ****************************************************************************************************************
PLAY RECAP ********************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
要求是通过这个 ansible 剧本从文本文件安装这些包
给定文件
shell> cat packages.txt
nginx
vim
grafana
在本地主机上,简单地迭代列表,例如下面的剧本
shell> cat test.yml
- hosts: localhost
tasks:
- name: install
package: "{{ item }}"
loop: "{{ lookup('file', 'packages.txt').split('\n') }}"
给予
shell> ansible-playbook test.yml --check
PLAY [localhost] *****************************************************
TASK [install] *******************************************************
ok: [localhost] => (item=nginx)
ok: [localhost] => (item=vim)
ok: [localhost] => (item=grafana)
PLAY RECAP ***********************************************************
localhost: ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
在远程主机上(查找仅适用于本地主机),迭代stdout_lines,例如
- name: Register a file content as a variable
command: cat packages.txt
register: result
- name: install
package: "{{ item }}"
loop: "{{ result.stdout_lines }}"
可选,如果你想避免命令或shell模块使用fetch。例如,在下面的剧本中
shell> cat test.yml
- hosts: test_11
tasks:
- name: Create directory files
file:
state: directory
path: files
- name: Get file
fetch:
src: packages.txt
dest: files
- name: install
package:
name: "{{ item }}"
loop: "{{ lookup('file', path).split('\n') }}"
vars:
path: 'files/{{ inventory_hostname }}/packages.txt'
创建目录 files 并首先从远程主机获取文件。这将创建文件
shell> cat files/test_11/packages.txt
nginx
vim
grafana
然后迭代文件中的行
TASK [install] ***************************************************
changed: [test_11] => (item=nginx)
changed: [test_11] => (item=vim)
changed: [test_11] => (item=grafana)