我正在运行一个命令,之后 stdout 被捕获到一个变量中。命令输出列出了由制表符分隔的值。所以我有类似的东西
var.stdout_lines: [
"a1\tb1\tc1",
"a2\tb2\tc2",
"a3\tb3\tc3"
]
为了进一步处理,我想将这些行分成列表,例如
[
[a1, b1, c1],
[a2, b2, c2],
[a3, b3, c3]
]
我怎样才能做到这一点?
我正在运行一个命令,之后 stdout 被捕获到一个变量中。命令输出列出了由制表符分隔的值。所以我有类似的东西
var.stdout_lines: [
"a1\tb1\tc1",
"a2\tb2\tc2",
"a3\tb3\tc3"
]
为了进一步处理,我想将这些行分成列表,例如
[
[a1, b1, c1],
[a2, b2, c2],
[a3, b3, c3]
]
我怎样才能做到这一点?
我想使用 ansible 输出一些以制表符作为分隔符的文本
这是任务片段
- name: Create output file
blockinfile:
block: |
Some text\tmore text
path: '{{ playbook_dir }}/output.txt'
create: true
电流输出
# BEGIN ANSIBLE MANAGED BLOCK
Some text\tmore text
# END ANSIBLE MANAGED BLOCK
期望的
# BEGIN ANSIBLE MANAGED BLOCK
Some text more text
# END ANSIBLE MANAGED BLOCK
我想使用 ansible 来配置一个虚拟盒子。这个盒子需要做很多事情,但它需要安装 R 以及一些未包含在基础 R 中的特定软件包。
我使用下面提供的 vagrant 文件创建一个新的虚拟机,然后使用 yum 安装 ansible、epel-release 和 git。然后,在该虚拟机上,我使用下面提供的“Alma Linux 8”和“Docker”剧本,然后使用我认为有问题的剧本。
然后我运行这个剧本来安装 R
---
- name: "Setup R on the local machine"
hosts: localhost
connection: local
tasks:
# Update all installed packages
- name: Update all packages to their latest version
become: yes
ansible.builtin.package:
name: "*"
state: latest
# Install EPEL to get extra packages
- name: Install EPEL
become: yes
ansible.builtin.package:
name: epel-release
state: latest
# Enable the Code Ready Builder (CRB) repository
- name: Install utilities to enable package repositories
become: yes
ansible.builtin.package:
name: yum-utils
state: latest
- name: Enable the PowerTools repository
become: yes
shell: dnf config-manager --set-enabled powertools
# Install R
- name: Install R
become: yes
ansible.builtin.package:
name: R
state: latest
运行此剧本后,我可以运行命令
Rscript --slave --no-save --no-restore-history -e "print('Test')"
[1] "Test"
表明 R 至少在某种程度上发挥着作用。但是当我尝试使用它来安装软件包时,我收到此错误
Rscript --slave --no-save --no-restore-history -e "install.packages('tidyverse')"
将软件包安装到“/usr/lib64/R/library”(因为未指定“lib”) install.packages(“tidyverse”) 中出现警告:“lib =“/usr/lib64/R/library””不可写错误在 install.packages("tidyverse") 中:无法安装包执行停止
我尝试通过将这些行附加到上面 R 剧本的末尾来解决此问题
- name: Make directory writable
become: yes
file:
path: /usr/lib64/R/library
mode: '0777'
- name: Make directory writable
become: yes
file:
path: /usr/share/doc/R/html
mode: '0777'
手动使有问题的目录可读,但这不起作用。
Vagrant.configure("2") do |config|
config.vm.box = "almalinux/8"
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
vb.cpus = 2
end
end
---
- name: "Setup AlmaLinux 8 on the local machine"
hosts: localhost
connection: local
tasks:
# Install the base operating systemctl
- name: Update all packages to their latest versions
become: yes
ansible.builtin.package:
name: '*'
state: latest
- name: Install virtual machine support
become: yes
ansible.builtin.package:
name: open-vm-tools
state: latest
# Install EPEL to get extra packages
- name: Install EPEL
become: yes
ansible.builtin.package:
name: epel-release
state: latest
---
- name: "Setup Docker on the local machine"
hosts: localhost
connection: local
tasks:
# Install Docker
- name: Uninstall any older versions of Docker
become: yes
ansible.builtin.package:
name:
- docker
- docker-client
- docker-client-latest
- docker-common
- docker-latest
- docker-latest-logrotate
- docker-logrotate
- docker-engine
state: absent
- name: Install yum-utils to install a new package repository
become: yes
ansible.builtin.package:
name: yum-utils
state: latest
- name: Install the Docker Repository
become: yes
shell: yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
- name: Install Docker
become: yes
ansible.builtin.package:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
state: latest
- name: Enable the docker service
become: yes
shell: systemctl enable docker
- name: Enable the containerd service
become: yes
shell: systemctl enable containerd
- name: Start the docker service
become: yes
shell: systemctl start docker
- name: Start the containerd service
become: yes
shell: systemctl start containerd
- name: Ensure docker group exists
become: yes
ansible.builtin.group:
name: docker
state: present
- name: Add the vagrant user to the docker group
become: yes
ansible.builtin.user:
name: vagrant
groups: docker
append: yes
我继承了一个类似剧本的文件,thing.yaml
其中包含以下内容:
---
- include: one.yaml
- include: two.yaml
ansible-playbook
我通常使用one.yaml
and启动 yaml 文件two.yaml
,但thing.yaml
被拒绝:
ERROR! 'include' is not a valid attribute for a Play
The error appears to be in '.../thing.yaml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
---
- include: one.yaml
^ here
哪个命令行会吃掉包含这样的文件?
我有以下词典:
question:
first_run:
app:
- answer: null
name: first_name
question: What is your First name?
- answer: null
name: last_name
question: What is your Last name?
core:
- question: question1
- question: question2
answer:
first_name: John
last_name: Smith
我使用以下任务手动更新question.first_run.app下的答案值,效果很好
- name: Update question variable
set_fact:
question:
first_run:
app:
- name: first_name
question: "What is your First name?"
answer: "{{ answer.first_name }}"
- name: last_name
question: "What is your Last name?"
answer: "{{ answer.last_name }}"
我还测试了以下任务(对于这种情况,这将是更可取的方法)
- name: "TEST-1"
set_fact:
question: "{{ question | combine({'first_run': {'app': question.first_run.app | map('combine', {'answer': update }) | list }}, recursive=True) }}"
loop: "{{ question.first_run.app }}"
vars:
update: "{{ answer[item.name] if item.name in answer.keys() else item.answer }}"
- name: "TEST-2"
set_fact:
question: "{{ question | combine({'first_run': {'app': question.first_run.app | map('combine', {'answer': (answer[item.name])})}}, recursive=True) }}"
loop: "{{ question.first_run.app }}"
vars:c
update: "{{ answer | dict2items | selectattr('key', 'in', [item.name]) | map(attribute='value') | first }}"
# update: "{{ answer[item.name] }}" # THis also works
但是 TEST-1 和 TEST-2 的输出总是这样的:
question:
first_run:
app:
- answer: Smith
name: first_name
question: What is your First name?
- answer: Smith
name: last_name
question: What is your Last name?
core:
- question: question1
- question: question2
我测试的最后一个任务是:
它可以工作,但所需的结果保存在新的 var app_list 下
- name: "TEST-3"
set_fact:
question: "{{ question | combine({'first_run': {'app': app_list}}, recursive=True) }}"
vars:
app_list: []
loop: "{{ question.first_run.app }}"
set_fact:
app_list: "{{ app_list + [item | combine({'answer': answer[item.name]})] }}"
问题:
我需要更新任务“更新问题变量”,这样我就不必手动放置数组question.first_run.app中的所有这些键
我如何使用循环将question.first_run.app.name与answer.key匹配,以便输出看起来像这样?最好调整 TEST-1 或 TEST-2 或 TEST-3
question:
first_run:
app:
- answer: John
name: first_name
question: What is your First name?
- answer: Smith
name: last_name
question: What is your Last name?
core:
- question: question1
- question: question2
有这个ansible playbook。需要在没有事实的情况下更改它的角色。如何使用 in role/default/main.yml 为变量重写此代码
谢谢
---
- name: test my group
hosts: my-group
tasks:
- name: test1 fact set
set_fact:
var1: 2
var2: 3
when: hostname is test1
- name: test2 fact set
set_fact:
var1: 3
var2: 3
when: hostname is test2
- debug: var="var1 + var2"
当我尝试为该区域设置多个服务时,我看到了这个问题。
只设置了第一个,另一个总是出现以下错误:
TASK [Set services] *******************************************************************************************************************************************************************************************************************************
changed: [localhost] => (item=ssh)
failed: [localhost] (item= samba) => {"ansible_loop_var": "item", "changed": false, "item": " samba", "msg": "ERROR: Exception caught: org.fedoraproject.FirewallD1.Exception: INVALID_SERVICE: Zone 'work': ' samba' not among existing services Permanent operation, Services are defined by port/tcp relationship and named as they are in /etc/services (on most systems)"}
如果我切换服务并将 samba 作为第一个而不是为 ssh 生成相同的错误
主要剧本:
- name: Configure Firewalld
hosts: localhost
gather_facts: no
vars:
firewall:
- zone: work
service: ssh, samba
- zone: public
service: samba
tasks:
- name: Set services
include_tasks: ./service.yml
loop: "{{firewall | selectattr('service', 'defined') | list}}"
loop_control:
loop_var: service
服务剧本:
- set_fact:
zone: "{{service.zone}}"
- name: "Servcie name"
debug:
var: item
with_items: "{{service.service | split(',')}}"
- name: Set services
ansible.posix.firewalld:
zone: "{{zone}}"
service: "{{item}}"
state: enabled
permanent: yes
with_items: "{{service.service | split(',')}}"
register: result
任何建议如何解决这个问题?
我只想为projectA
下面剧本中的项目强制复制文件。
- name: Populate /usr/local/mywork/ binaries where necessary
copy:
src: "output/here/usr/local/mywork/{{item}}"
dest: "/usr/local/mywork/{{ item }}"
force: '{{ item == "projectA" }}'
with_items: "{{ server_files }}"
我补充道force: '{{ item == "projectA" }}'
。
但它不起作用。有些部分不正确?
我想使用 Ansible 将新用户的公钥推送到主机清单。为此,创建了一个剧本,如下例所示。
---
- name: vms1 - Authorize hosts with pub key
hosts: vms1
tasks:
- name: Copy ssh pub key to remote host
ansible.posix.authorized_key:
user: user1
state: present
key: "{{ lookup('file', '/home/controluser/.ssh/id_rsa.pub') }}"
- name: vms2 - Authorize hosts with pub key
hosts: vms2
tasks:
- name: Copy ssh pub key to remote host
ansible.posix.authorized_key:
user: user2
state: present
key: "{{ lookup('file', '/home/controluser/.ssh/id_rsa.pub') }}"
- name: vms3 - Authorize hosts with pub key
hosts: vms3
tasks:
- name: Copy ssh pub key to remote host
ansible.posix.authorized_key:
user: user3
state: present
key: "{{ lookup('file', '/home/controluser/.ssh/id_rsa.pub') }}"
库存是这样的。(有更多的主机)
[vms1]
192.168.7.211
192.168.7.212
192.168.7.213
[vms2]
192.168.7.21
192.168.7.22
192.168.7.23
[vms3]
192.168.7.111
192.168.7.112
192.168.7.113
每个游戏都有自己的主机组和自己的用户。可以使用以下命令启动 playbook。
ansible-playbook -k -i inventory playbook.yml
使用-k
选项时,它只询问一次 SSH 密码。我遇到的问题是每个组的密码都不同。因此,我想为每个游戏输入密码。
每次播放都需要密码吗?